Last active
October 25, 2017 03:30
-
-
Save jayrambhia/9f317e0edba1f28e5553 to your computer and use it in GitHub Desktop.
Multiple button clicks using RxAndroid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void setTextObservable(final EditText editText) { | |
Subscription editTextSubscription = Observable.create(new Observable.OnSubscribe<String>() { | |
@Override | |
public void call(final Subscriber<? super String> subscriber) { | |
editText.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) {} | |
@Override | |
public void afterTextChanged(Editable s) { | |
subscriber.onNext(s.toString()); | |
} | |
}); | |
} | |
}).buffer(1200, TimeUnit.MILLISECONDS) | |
.flatMap(new Func1<List<String>, Observable<Boolean>>() { | |
@Override | |
public Observable<Boolean> call(final List<String> strings) { | |
return Observable.create(new Observable.OnSubscribe<Boolean>() { | |
@Override | |
public void call(Subscriber<? super Boolean> subscriber) { | |
if (strings.size() > 1) { | |
Log.i(TAG, "strings size: " + String.valueOf(strings.size())); | |
subscriber.onNext(false); | |
} else { | |
if (editText.getText().length() > 0) { | |
subscriber.onNext(true); | |
} | |
Log.e(TAG, "string size is 0"); | |
} | |
} | |
}); | |
} | |
}) | |
.subscribe(new Action1<Object>() { | |
@Override | |
public void call(Object o) { | |
if ((Boolean) o) { | |
Log.i(TAG, "perform some operation"); | |
// Some method call here | |
} else { | |
Log.e(TAG, "still editing"); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment