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);
                  }
                }));
  }
  public Observable<TranslationTask> getTranslationTaskObservable() {
    Observable<String> originalTextObservable =
        RxTextView.textChanges(mOriginalTextInput).map(CharSequence::toString);
    Observable<TranslationDirection> languageDirectionObservable = mLanguagePicker.getObservable();

    return Observable.combineLatest(
        languageDirectionObservable,
        originalTextObservable,
        (direction, text) -> new TranslationTask(text, direction));
  }
  private void manageEditTexts() {
    EditText usualApproachEditText, reactiveApproachEditText;

    usualApproachEditText = (EditText) findViewById(R.id.editTextUsualApproach);

    usualApproachEditText.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) {
            onNewTextChanged(s);
          }

          @Override
          public void afterTextChanged(Editable s) {}
        });

    /** Here, we see how it can be easy to manage text changes with RxBindings */
    reactiveApproachEditText = (EditText) findViewById(R.id.editTextReactiveApproach);
    RxTextView.textChanges(reactiveApproachEditText).subscribe(this::onNewTextChanged);
  }
Beispiel #4
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    setTitle("");

    searchView = (EditText) findViewById(R.id.searchView);

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabStrip);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    searchReposFragment = SearchReposFragment.newInstance(null);
    searchUsersFragment = SearchUsersFragment.newInstance(null);

    List<Fragment> listFragments = new ArrayList<>();
    listFragments.add(searchReposFragment);
    listFragments.add(searchUsersFragment);

    viewPager.setAdapter(
        new NavigationPagerAdapter(getSupportFragmentManager(), getResources(), listFragments));
    tabLayout.setupWithViewPager(viewPager);

    searchView.setOnEditorActionListener(
        new TextView.OnEditorActionListener() {
          @Override
          public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
            if (textView.length() > 0) {
              switch (actionId) {
                case EditorInfo.IME_ACTION_DONE:
                case EditorInfo.IME_ACTION_SEARCH:
                case EditorInfo.IME_ACTION_SEND:
                case EditorInfo.IME_ACTION_NEXT:
                case EditorInfo.IME_ACTION_GO:
                  if (textView.getText() != null) {
                    search(textView.getText().toString());
                  }
                  break;
              }
            }
            return false;
          }
        });

    subscription =
        RxTextView.textChanges(searchView)
            .filter(
                new Func1<CharSequence, Boolean>() {
                  @Override
                  public Boolean call(CharSequence s) {
                    return s.length() >= 3;
                  }
                })
            .throttleLast(100, TimeUnit.MILLISECONDS)
            .debounce(250, TimeUnit.MILLISECONDS)
            .subscribeOn(AndroidSchedulers.mainThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                new Subscriber<CharSequence>() {
                  @Override
                  public void onCompleted() {}

                  @Override
                  public void onError(Throwable e) {}

                  @Override
                  public void onNext(CharSequence charSequence) {
                    search(charSequence.toString());
                  }
                });
  }
  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);
                  }
                }));
  }