Created
February 2, 2016 03:21
-
-
Save vuhung3990/5f3d831749ba2ed5fc25 to your computer and use it in GitHub Desktop.
rxJava combine (form validation)
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
Observable<CharSequence> observable2 = Observable.create(new Observable.OnSubscribe<CharSequence>() { | |
@Override | |
public void call(final Subscriber<? super CharSequence> subscriber) { | |
txt2.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) { | |
subscriber.onNext(s); | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
} | |
}); | |
} | |
}); | |
Observable<CharSequence> observable3 = Observable.create(new Observable.OnSubscribe<CharSequence>() { | |
@Override | |
public void call(final Subscriber<? super CharSequence> subscriber) { | |
txt3.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) { | |
subscriber.onNext(s); | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
} | |
}); | |
} | |
}); | |
Observable.combineLatest(observable2, observable3, new Func2<CharSequence, CharSequence, Boolean>() { | |
@Override | |
public Boolean call(CharSequence charSequence, CharSequence charSequence2) { | |
boolean isGreater10 = Integer.parseInt(txt2.getText().toString()) > 10; | |
if (!isGreater10) txt2.setError("must be greater 10"); | |
boolean isLengthGreater5 = txt3.getText().toString().length() > 5; | |
if (!isLengthGreater5) txt3.setError("length must be greater than 5"); | |
return isGreater10 && isLengthGreater5; | |
} | |
}).subscribe(new Subscriber<Boolean>() { | |
@Override | |
public void onCompleted() { | |
Log.d(TAG, "onCompleted: "); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Log.d(TAG, "onError: "); | |
} | |
@Override | |
public void onNext(Boolean aBoolean) { | |
Log.d(TAG, "return: " + aBoolean); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment