Skip to content

Instantly share code, notes, and snippets.

@rahulmmohan
Created March 16, 2017 11:09
Subscriber only called when the validity changes. For this, we’d need to call distinctUntilChanged() method on our Observables.
isUsernameValid.distinctUntilChanged().doOnNext(new Consumer<Boolean>() {
@Override
public void accept(Boolean valid) throws Exception {
Log.d(TAG, "Username " + (valid ? "Valid" : "Invalid"));
}
}).map(new Function<Boolean, Integer>() {
@Override
public Integer apply(Boolean valid) throws Exception {
return valid?Color.WHITE:Color.RED;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer color) throws Exception {
mUsernameEditText.setTextColor(color);
}
});
isEmailValid.distinctUntilChanged().doOnNext(new Consumer<Boolean>() {
@Override
public void accept(Boolean valid) throws Exception {
Log.d(TAG, "Email " + (valid ? "Valid" : "Invalid"));
}
}).map(new Function<Boolean, Integer>() {
@Override
public Integer apply(Boolean valid) throws Exception {
return valid?Color.WHITE:Color.RED;
}
}).subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer color) throws Exception {
mEmailEditText.setTextColor(color);
}
});
registerButtonObservable.distinctUntilChanged().subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
mRegisterButton.setEnabled(aBoolean);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment