Search in sources :

Example 1 with TextViewTextChangeEvent

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();
        }
    });
}
Also used : Action1(rx.functions.Action1) TextViewTextChangeEvent(com.jakewharton.rxbinding.widget.TextViewTextChangeEvent) TextView(android.widget.TextView) RxTextView(com.jakewharton.rxbinding.widget.RxTextView) Func1(rx.functions.Func1) Person(io.realm.examples.rxjava.model.Person) RealmResults(io.realm.RealmResults)

Aggregations

TextView (android.widget.TextView)1 RxTextView (com.jakewharton.rxbinding.widget.RxTextView)1 TextViewTextChangeEvent (com.jakewharton.rxbinding.widget.TextViewTextChangeEvent)1 RealmResults (io.realm.RealmResults)1 Person (io.realm.examples.rxjava.model.Person)1 Action1 (rx.functions.Action1)1 Func1 (rx.functions.Func1)1