use of rx.Observer in project wh-app-android by WhiteHouse.
the class SearchFragment method submitQuery.
public void submitQuery(String query) {
showList(false);
showProgress(true);
if (mSearchSub != null) {
mSearchSub.unsubscribe();
}
if (!NetworkUtils.checkNetworkAvailable(getActivity())) {
showProgress(false);
Toast.makeText(getActivity(), R.string.no_network, Toast.LENGTH_SHORT).show();
return;
}
mSearchSub = AndroidObservable.bindFragment(this, SearchManager.get().search(query).first().map(searchResults -> {
List<SearchResult> list = new ArrayList<>();
SearchResult result;
if (searchResults.boosted_results() != null) {
for (BoostedSearchResult b : searchResults.boosted_results()) {
result = SearchResult.create(b.description(), b.title(), b.url());
list.add(result);
}
}
if (searchResults.results() != null) {
list.addAll(searchResults.results());
}
return list;
}).subscribeOn(Schedulers.newThread())).subscribe(new Observer<List<SearchResult>>() {
@Override
public void onCompleted() {
showProgress(false);
showList(true);
}
@Override
public void onError(Throwable e) {
Toast.makeText(getActivity(), "An error occurred while trying to search", Toast.LENGTH_SHORT).show();
Timber.w(e, "Error searching for query '%s'", query);
}
@Override
public void onNext(List<SearchResult> searchResults) {
getAdapter().clear();
if (searchResults != null) {
getAdapter().addAll(searchResults);
}
}
});
bindSubscription(mSearchSub);
if (getTracker() != null) {
getTracker().set("searchQuery", query);
}
}
Aggregations