use of com.safframework.study.rxbinding.domain.ValidationResult in project RxJavaInAction by fengzhizi715.
the class TestEditTextActivity method initViews.
private void initViews() {
Observable<CharSequence> ObservablePhone = RxTextView.textChanges(phone);
Observable<CharSequence> ObservablePassword = RxTextView.textChanges(password);
Observable.combineLatest(ObservablePhone, ObservablePassword, new BiFunction<CharSequence, CharSequence, ValidationResult>() {
@Override
public ValidationResult apply(@NonNull CharSequence o1, @NonNull CharSequence o2) throws Exception {
if (o1.length() > 0 || o2.length() > 0) {
login.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_login_pressed));
} else {
login.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_login_normal));
}
ValidationResult result = new ValidationResult();
if (o1.length() == 0) {
result.flag = false;
result.message = "手机号码不能为空";
} else if (o1.length() != 11) {
result.flag = false;
result.message = "手机号码需要11位";
} else if (o1 != null && !AppUtils.isPhoneNumber(o1.toString())) {
result.flag = false;
result.message = "手机号码需要数字";
} else if (o2.length() == 0) {
result.flag = false;
result.message = "密码不能为空";
}
return result;
}
}).subscribe(new Consumer<ValidationResult>() {
@Override
public void accept(@NonNull ValidationResult r) throws Exception {
result = r;
}
});
RxView.clicks(login).compose(RxUtils.useRxViewTransformer(TestEditTextActivity.this)).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
if (result == null)
return;
if (result.flag) {
Toast.makeText(TestEditTextActivity.this, "模拟登录成功", Toast.LENGTH_SHORT).show();
} else if (Preconditions.isNotBlank(result.message)) {
Toast.makeText(TestEditTextActivity.this, result.message, Toast.LENGTH_SHORT).show();
}
}
});
}
Aggregations