Search in sources :

Example 1 with BoostedSearchResult

use of gov.whitehouse.data.model.BoostedSearchResult 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);
    }
}
Also used : BoostedSearchResult(gov.whitehouse.data.model.BoostedSearchResult) Observer(rx.Observer) ArrayList(java.util.ArrayList) BoostedSearchResult(gov.whitehouse.data.model.BoostedSearchResult) SearchResult(gov.whitehouse.data.model.SearchResult) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

BoostedSearchResult (gov.whitehouse.data.model.BoostedSearchResult)1 SearchResult (gov.whitehouse.data.model.SearchResult)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Observer (rx.Observer)1