Skip to content

Instantly share code, notes, and snippets.

@followthemoney1
Created February 15, 2018 15:46
Show Gist options
  • Select an option

  • Save followthemoney1/b46b529f052cdb2d8e18d5a53490a317 to your computer and use it in GitHub Desktop.

Select an option

Save followthemoney1/b46b529f052cdb2d8e18d5a53490a317 to your computer and use it in GitHub Desktop.
simple rxbinding
private void createEditTextSearch() {
    RxTextView.afterTextChangeEvents(placeName)
            .debounce(500, TimeUnit.MILLISECONDS)
            .filter(new Predicate<TextViewAfterTextChangeEvent>() {
                @Override
                public boolean test(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Exception {
                    return isFocused;
                }
            })
            .filter(new Predicate<TextViewAfterTextChangeEvent>() {
                @Override
                public boolean test(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Exception {
                    return currentLocation != null;
                }
            })
            .flatMap(new Function<TextViewAfterTextChangeEvent, ObservableSource<? extends PlacesResult>>() {
                @Override
                public ObservableSource<? extends PlacesResult> apply(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Exception {
                    return placesLoader.find(textViewAfterTextChangeEvent.editable().toString(), currentLocation);
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);
}

Observer<PlacesResult> subscriber = new Observer<PlacesResult>() {
    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(PlacesResult placesResult) {
        Log.d("onNext", "");
        refreshRecycleViewData(placesResult);
    }

    @Override
    public void onError(Throwable e) {
        Log.d("onError", e.toString());
        createEditTextSearch();
    }

    @Override
    public void onComplete() {
        Log.d("onComplete", "");
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment