Skip to content

Instantly share code, notes, and snippets.

@FilipeLipan
Created December 13, 2017 02:01
Show Gist options
  • Save FilipeLipan/b67f9305d0d98f5f2712cdc79b62c1f6 to your computer and use it in GitHub Desktop.
Save FilipeLipan/b67f9305d0d98f5f2712cdc79b62c1f6 to your computer and use it in GitHub Desktop.
SearchView searchView;
CompositeDisposable compositeDisposable = new CompositeDisposable();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchView = (SearchView) findViewById(R.id.search_view);
compositeDisposable.add(fromSearchView(searchView)
.debounce(300, TimeUnit.MILLISECONDS)
.filter(new Predicate<String>() {
@Override
public boolean test(String text) throws Exception {
if (text.isEmpty()) {
return false;
} else {
return true;
}
}
})
.distinctUntilChanged()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
funToast(s);
}
}));
}
public static Observable<String> fromSearchView(SearchView searchView) {
final PublishSubject<String> subject = PublishSubject.create();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
subject.onComplete();
return true;
}
@Override
public boolean onQueryTextChange(String text) {
subject.onNext(text);
return true;
}
});
return subject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment