use of com.jakewharton.rxbinding.widget.TextViewTextChangeEvent in project realm-java by realm.
the class ThrottleSearchActivity method onResume.
@Override
protected void onResume() {
super.onResume();
// Listen to key presses and only start search after user paused to avoid excessive redrawing on the screen.
subscription = RxTextView.textChangeEvents(searchInputView).debounce(200, // default Scheduler is Schedulers.computation()
TimeUnit.MILLISECONDS).observeOn(// Needed to access Realm data
AndroidSchedulers.mainThread()).flatMap(new Func1<TextViewTextChangeEvent, Observable<RealmResults<Person>>>() {
@Override
public Observable<RealmResults<Person>> call(TextViewTextChangeEvent event) {
// Realm currently doesn't support the standard Schedulers.
return realm.where(Person.class).beginsWith("name", event.text().toString()).findAllSortedAsync("name").asObservable();
}
}).filter(new Func1<RealmResults<Person>, Boolean>() {
@Override
public Boolean call(RealmResults<Person> persons) {
// RealmObservables will emit the unloaded (empty) list as its first item
return persons.isLoaded();
}
}).subscribe(new Action1<RealmResults<Person>>() {
@Override
public void call(RealmResults<Person> persons) {
searchResultsView.removeAllViews();
for (Person person : persons) {
TextView view = new TextView(ThrottleSearchActivity.this);
view.setText(person.getName());
searchResultsView.addView(view);
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
throwable.printStackTrace();
}
});
}
Aggregations