4

I have implemented rxjava in my project with butterknife like this:

@BindView(R.id.login_et)
EditText mLoginField;

@BindView(R.id.password_et)
EditText mPassField;

@BindView(R.id.login_bt)
Button mLoginButton;

@BindDrawable(R.drawable.error)
Drawable mInvalidField;

@BindDrawable(R.drawable.check_green)
Drawable mValidField;

and validated my input fields and button like this:

rx.Observable<CharSequence> loginObservable = RxTextView.textChanges(mLoginField);
    loginObservable.map(this::isValidLogin)
            .subscribe(isValid -> mLoginField.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, (isValid? mValidField : mInvalidField), null));

    rx.Observable<CharSequence> passwordObservable = RxTextView.textChanges(mPassField);
    passwordObservable.map(this::isValidPassword)
            .subscribe(isValid -> mPassField.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, (isValid ? mValidField : mInvalidField), null));

    rx.Observable<Boolean> combinedObservables = rx.Observable.combineLatest(loginObservable, passwordObservable, (o1, o2) -> isValidLogin(o1) && isValidPassword(o2));
    combinedObservables.subscribe(isColorChanged -> mLoginButton.setBackgroundColor(isColorChanged ? getResources().getColor(R.color.red): getResources().getColor(R.color.green)));

I have a radiogroup and three spinners in my activity layout, I am confused how to do RxBindings on the radiogroup and spinner for form validation.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Anand Rajput
  • 469
  • 1
  • 8
  • 21

1 Answers1

2

I am confused how to do RxBindings on the radiogroup and spinner

This is how you listen CheckBox changes:

 RxCompoundButton.checkedChanges(checkbox)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean isChecked) throws Exception {

                    }
                });

This is how you can listen Spinner selection: RxBindings For Spinner?

Johnny Five
  • 987
  • 1
  • 14
  • 29