@Override
  public void textChangedOnEditView(Observable<? extends String> observable) {
    Log.d(TAG, "textChangedOnEditView");
    observable
        .filter(text -> text.length() >= 3)
        .debounce(1, TimeUnit.SECONDS)
        .map(text -> "RxJava " + text)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
            new Subscriber<String>() {
              @Override
              public void onCompleted() {
                Log.d(TAG, "onCompleted");
              }

              @Override
              public void onError(Throwable e) {
                Log.d(TAG, "onError");
                e.printStackTrace();
              }

              @Override
              public void onNext(String s) {
                Log.d(TAG, "onNext");
                mView.setTextToTextView(s);
              }
            });
  }
示例#2
0
文件: Echo.java 项目: Dexels/navajo
 @Override
 public Observable<NavajoStreamEvent> call(Observable<NavajoStreamEvent> input) {
   return input.filter(
       e ->
           e.type() != NavajoEventTypes.NAVAJO_DONE
               && e.type() != NavajoEventTypes.NAVAJO_STARTED);
 }
  @Override
  public Observable<T> call(Observable<DataEvent> observable) {
    if (type != null) {
      observable =
          observable.filter(
              new Func1<DataEvent, Boolean>() {
                @Override
                public Boolean call(DataEvent dataEvent) {
                  return dataEvent.getType() == type;
                }
              });
    }

    if (path != null) {
      observable =
          observable.filter(
              new Func1<DataEvent, Boolean>() {
                @Override
                public Boolean call(DataEvent dataEvent) {
                  if (isPrefix) {
                    return dataEvent.getDataItem().getUri().getPath().startsWith(path);
                  } else {
                    return dataEvent.getDataItem().getUri().getPath().equals(path);
                  }
                }
              });
    }

    return observable.map(
        new Func1<DataEvent, T>() {
          @SuppressWarnings({"unchecked", "ThrowableResultOfMethodCallIgnored"})
          @Override
          public T call(DataEvent dataEvent) {
            try {
              ObjectInputStream objectInputStream =
                  new ObjectInputStream(
                      new ByteArrayInputStream(dataEvent.getDataItem().getData()));
              return (T) objectInputStream.readObject();
            } catch (Exception e) {
              Exceptions.propagate(e);
              return null;
            }
          }
        });
  }
  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);
                  }
                }));
  }
示例#5
0
  private static void test3() {
    log("simple map");
    Observable.just("Test", "Me", "No").map(x -> x.length()).subscribe(Main::print);
    println("");

    log("map with count");
    Observable.just("Test", "Me", "No").count().subscribe(Main::print);
    println("");

    Observable<Integer> distinctObsv = Observable.just(2, 2, 3, 3, 4, 5, 6, 7, 8, 9).distinct();
    log("distinct & filter ");
    distinctObsv.filter(x -> (x % 2 == 0)).subscribe(Main::print);
    println("");

    log("distinct & filter & skip &  first");
    distinctObsv.filter(x -> (x % 2 == 0)).skip(2).first().subscribe(Main::print);
    println("");

    log("distinct & filter & skip & last");
    distinctObsv.filter(x -> (x % 2 == 0)).skip(2).last().subscribe(Main::print);
    println("");

    log("distinct & filter & take & last");
    distinctObsv.filter(x -> (x % 2 == 0)).take(2).last().subscribe(Main::print);
    println("");

    log("map with reduce to get total length of strings \"Test\", \"Me\", \"No\"");
    Observable.just("Test", "Me", "No")
        .map(x -> x.length())
        .reduce((x, y) -> x + y)
        .subscribe(Main::print);
    println("");

    log("sum of 1,2,3,4,5 using reduce");
    Observable.just(1, 2, 3, 4, 5).reduce((x, y) -> x + y).subscribe(Main::print);
    println("");

    log("get larget of seq 9,1,8,3,4,5 using reduce");
    Observable.just(9, 1, 8, 3, 4, 5).reduce((x, y) -> x > y ? x : y).subscribe(Main::print);
    println("");
  }
  /*default*/ static List<Flower> deleteAnyFutureFlowers(List<Flower> flowers) {
    final List<Flower> flowersFromPast = Lists.newArrayList();

    Observable<Flower> flowersObservable = Observable.from(flowers);
    // look for Flowers with future dates
    flowersObservable
        .filter(flower -> flower.getDate().compareTo(new Date()) < 0)
        .toList()
        .subscribe(filteredFlowers -> flowersFromPast.addAll(filteredFlowers));

    return flowersFromPast;
  }
  private void _setupViewBindings(
      Button signinButton,
      final TextView errorTextView,
      MyAccountDataLoginState myAccountDataLoginState,
      final EditText usernameField,
      final EditText passwordField,
      final RadioGroup institutionRadio,
      Observable<MyAccountDataLoginState> myAccountDataLoginStateObservable) {
    if (myAccountDataLoginState != null
        && myAccountDataLoginState.exception != null
        && errorTextView != null) {
      String errorMessage = myAccountDataLoginState.exception.getMessage();
      errorTextView.setText(errorMessage);
    }

    if (signinButton == null
        || errorTextView == null
        || usernameField == null
        || passwordField == null
        || institutionRadio == null) {
      return; // quit early if we can't verify the state
    }

    if (myAccountDataLoginStateERRORObservableSubscription == null
        || myAccountDataLoginStateERRORObservableSubscription.isUnsubscribed()) {

      myAccountDataLoginStateERRORObservableSubscription =
          myAccountDataLoginStateObservable
              .filter(
                  new Func1<MyAccountDataLoginState, Boolean>() {
                    @Override
                    public Boolean call(MyAccountDataLoginState myAccountDataLoginState) {
                      return (myAccountDataLoginState.type == MyAccountDataLoginStateType.ERROR)
                          || (myAccountDataLoginState.type
                              == MyAccountDataLoginStateType.SIGNED_OUT);
                    }
                  })
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(
                  new Action1<MyAccountDataLoginState>() {
                    @Override
                    public void call(MyAccountDataLoginState myAccountDataLoginState) {
                      if (myAccountDataLoginState.exception != null) {
                        errorTextView.setText(myAccountDataLoginState.exception.getMessage());
                      }
                      passwordField.setText("");
                    }
                  });
    }

    signInButton.setOnClickListener(null); // Ensures the function is idempotent

    signinButton.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            if (_isInputValid(usernameField, passwordField, institutionRadio)) {
              _clearErrorText(errorTextView);
              userModel
                  .getSigninActivatePublishSubject()
                  .onNext(
                      new UserCredentials(
                          usernameField.getText().toString().trim(),
                          passwordField.getText().toString(),
                          _getInsitutionIdFromRadioView(institutionRadio)));
            }
          }
        });
  }
 @Override
 public Subscription uplink(Observable<T> rxData) {
   return mChild.uplink(rxData.filter(x -> !mSuppressWrites));
 }
示例#9
0
 /**
  * If the {@link Observable} completes after emitting a single item that matches a predicate,
  * return that item; if it emits more than one such item, throw an <code>IllegalArgumentException
  * </code>; if it emits no items, return a default value.
  *
  * <p><img width="640"
  * src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/B.singleOrDefault.p.png">
  *
  * @param defaultValue a default value to return if the {@link Observable} emits no matching items
  * @param predicate a predicate function to evaluate items emitted by the {@link Observable}
  * @return the single item emitted by the {@link Observable} that matches the predicate, or the
  *     default value if no such items are emitted
  * @see <a
  *     href="https://github.com/Netflix/RxJava/wiki/Blocking-Observable-Operators#single-and-singleordefault">RxJava
  *     Wiki: singleOrDefault()</a>
  * @see <a
  *     href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.singleordefault.aspx">MSDN:
  *     Observable.SingleOrDefault</a>
  */
 public T singleOrDefault(T defaultValue, Func1<? super T, Boolean> predicate) {
   return from(o.filter(predicate)).singleOrDefault(defaultValue);
 }