Search in sources :

Example 6 with Observer

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);
    }
}
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

Observer (rx.Observer)5 Nullable (android.support.annotation.Nullable)3 TextUtils (android.text.TextUtils)3 Log (android.util.Log)3 Reddit (com.winsonchiu.reader.data.reddit.Reddit)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Inject (javax.inject.Inject)3 Activity (android.app.Activity)2 Bundle (android.os.Bundle)2 AppBarLayout (android.support.design.widget.AppBarLayout)2 CoordinatorLayout (android.support.design.widget.CoordinatorLayout)2 TabLayout (android.support.design.widget.TabLayout)2 PagerAdapter (android.support.v4.view.PagerAdapter)2 ViewPager (android.support.v4.view.ViewPager)2 NestedScrollView (android.support.v4.widget.NestedScrollView)2 Toolbar (android.support.v7.widget.Toolbar)2 Html (android.text.Html)2 TypedValue (android.util.TypedValue)2 LayoutInflater (android.view.LayoutInflater)2