Skip to content

Instantly share code, notes, and snippets.

@rohmanhakim
Last active January 5, 2017 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohmanhakim/ab25760381c20b6c6719f7634f2b4ae0 to your computer and use it in GitHub Desktop.
Save rohmanhakim/ab25760381c20b6c6719f7634f2b4ae0 to your computer and use it in GitHub Desktop.
Observable<Boolean> emptyFieldStream = Observable.combineLatest(
RxTextView.textChanges(etEmail)
.map(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
return TextUtils.isEmpty(charSequence);
}
}),
RxTextView.textChanges(etPassword)
.map(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
return TextUtils.isEmpty(charSequence);
}
}),
RxTextView.textChanges(etPasswordConfirmation)
.map(new Func1<CharSequence, Boolean>() {
@Override
public Boolean call(CharSequence charSequence) {
return TextUtils.isEmpty(charSequence);
}
}),
new Func3<Boolean, Boolean, Boolean, Boolean>() {
@Override
public Boolean call(Boolean emailEmpty, Boolean passwordEmpty, Boolean passwordConfirmationEmpty) {
return emailEmpty || passwordEmpty || passwordConfirmationEmpty;
}
}
);
Observable<Boolean> invalidFieldsStream = Observable.combineLatest(
emailStream,
passwordStream,
passwordConfirmationStream,
emptyFieldStream, new Func4<Boolean, Boolean, Boolean, Boolean, Boolean>() {
@Override
public Boolean call(Boolean emailInvalid, Boolean passwordInvalid, Boolean passwordConfirmationInvalid, Boolean emptyFieldExist) {
return !emailInvalid && !passwordInvalid && !passwordConfirmationInvalid && !emptyFieldExist;
}
});
Observer<Boolean> invalidFieldsObserver = new Observer<Boolean>() {
@Override
public void onCompleted() {
Log.d("rx","All field valid stream completed");
}
@Override
public void onError(Throwable e) {
Log.d("rx",e.getMessage());
}
@Override
public void onNext(Boolean invalidFieldExist) {
Log.d("invalidFieldsStream",String.valueOf(invalidFieldExist.booleanValue()));
btnSubmit.setEnabled(invalidFieldExist);
}
};
invalidFieldsStream.subscribe(invalidFieldsObserver);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment