예제 #1
0
  private void renderView() {
    // ****************************************
    // RxAndroid + Lambda + method references approach
    // ****************************************
    mEdittextLambdaObservable = RxTextView.textChangeEvents(mLambdaEditText);

    mCompositeSubscription.add(
        mEdittextLambdaObservable
            .filter(event -> event.text().toString().endsWith("test"))
            .map(event -> event.text().toString())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(mLambdaTextView::setText));

    // ****************************************
    // RxAndroid approach
    // ****************************************
    mEdittextRxAndroidObservable = RxTextView.textChangeEvents(mRxAndroidEditText);

    mCompositeSubscription.add(
        mEdittextRxAndroidObservable
            .filter(
                new Func1<TextViewTextChangeEvent, Boolean>() {
                  @Override
                  public Boolean call(TextViewTextChangeEvent onTextChangeEvent) {
                    return onTextChangeEvent.text().toString().endsWith("test");
                  }
                })
            .map(
                new Func1<TextViewTextChangeEvent, String>() {
                  @Override
                  public String call(TextViewTextChangeEvent onTextChangeEvent) {
                    return onTextChangeEvent.text().toString();
                  }
                })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                new Action1<String>() {
                  @Override
                  public void call(String filteredString) {
                    mRxAndroidTextView.setText(filteredString);
                  }
                }));
  }
  private void combineLatestEvents() {

    userNameChangeObservable = RxTextView.textChangeEvents(userNameEt).skip(1);
    passwordChangeObservable = RxTextView.textChangeEvents(passwordEt).skip(1);
    mobileChangeObservable = RxTextView.textChangeEvents(mobileEt).skip(1);
    codeChangeObservable = RxTextView.textChangeEvents(codeEt).skip(1);

    Observable<TextViewTextChangeEvent> mobileChangeShareObservable =
        mobileChangeObservable.share();

    compositeSubscription.add(
        mobileChangeShareObservable
            .publish(
                new Func1<
                    Observable<TextViewTextChangeEvent>, Observable<TextViewTextChangeEvent>>() {
                  @Override
                  public Observable<TextViewTextChangeEvent> call(
                      Observable<TextViewTextChangeEvent> textChangeEventObservable) {
                    return textChangeEventObservable.debounce(
                        Constants.MILLISECONDS_300, TimeUnit.MILLISECONDS);
                  }
                })
            .doOnSubscribe(
                new Action0() {
                  @Override
                  public void call() {
                    codeBtn.setEnabled(false);
                  }
                })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                new Action1<TextViewTextChangeEvent>() {
                  @Override
                  public void call(TextViewTextChangeEvent textViewTextChangeEvents) {
                    codeBtn.setEnabled(
                        !TextUtils.isEmpty(textViewTextChangeEvents.text().toString()));
                  }
                }));

    /* compositeSubscription.add(mobileChangeObservable.doOnSubscribe(new Action0() {
      @Override public void call() {
        codeBtn.setEnabled(false);
      }
    })
        .debounce(Constants.MILLISECONDS_300, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<TextViewTextChangeEvent>() {
          @Override public void call(TextViewTextChangeEvent textEvent) {
            codeBtn.setEnabled(!TextUtils.isEmpty(textEvent.text().toString()));
          }
        }));*/

    compositeSubscription.add(
        Observable.combineLatest(
                userNameChangeObservable,
                passwordChangeObservable,
                mobileChangeObservable,
                codeChangeObservable,
                new Func4<
                    TextViewTextChangeEvent,
                    TextViewTextChangeEvent,
                    TextViewTextChangeEvent,
                    TextViewTextChangeEvent,
                    Boolean>() {
                  @Override
                  public Boolean call(
                      TextViewTextChangeEvent userNameEvent,
                      TextViewTextChangeEvent passwordEvent,
                      TextViewTextChangeEvent mobileEvent,
                      TextViewTextChangeEvent codeEvent) {

                    userName = userNameEvent.text().toString();
                    password = passwordEvent.text().toString();
                    mobile = mobileEvent.text().toString();
                    code = codeEvent.text().toString();

                    boolean userNameValid = !TextUtils.isEmpty(userName);
                    boolean passwordValid = !TextUtils.isEmpty(password);
                    boolean mobileValid = !TextUtils.isEmpty(mobile) /*&& Utils.isMobile(mobile)*/;
                    boolean codeValid = !TextUtils.isEmpty(code);

                    return userNameValid && passwordValid && mobileValid && codeValid;
                  }
                })
            .debounce(Constants.MILLISECONDS_300, TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .startWith(false)
            .subscribe(
                new Action1<Boolean>() {
                  @Override
                  public void call(Boolean aBoolean) {

                    registerBtn.setEnabled(aBoolean);
                  }
                }));
  }