Java Code Examples for io.reactivex.Observable.combineLatest()

The following are Jave code examples for showing how to use combineLatest() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: buckaroo   File: MoreObservables.java   View Source Code Vote up 7 votes
public static <T, S> Observable<Map<T, S>> zipMaps(final Map<T, Observable<S>> tasks) {

        Objects.requireNonNull(tasks, "tasks is null");

        return Observable.combineLatest(
            tasks.entrySet()
                .stream()
                .map(entry -> Observable.combineLatest(
                    Observable.just(entry.getKey()),
                    entry.getValue(),
                    Pair::with))
                .collect(ImmutableList.toImmutableList()),
            xs -> Arrays.stream(xs)
                .map(x -> (Pair<T, S>)x)
                .collect(ImmutableMap.toImmutableMap(Pair::getValue0, Pair::getValue1)));
    }
 
Example 2
Project: GitHub   File: DetailsInteractor.java   View Source Code Vote up 6 votes
private Observable<ProductDetail> getProductWithShoppingCartInfo(int productId) {
  List<Observable<?>> observables =
      Arrays.asList(backendApi.getProduct(productId), shoppingCart.itemsInShoppingCart());

  return Observable.combineLatest(observables, objects -> {
    Product product = (Product) objects[0];
    List<Product> productsInShoppingCart = (List<Product>) objects[1];
    boolean inShoppingCart = false;
    for (Product p : productsInShoppingCart) {
      if (p.getId() == productId) {
        inShoppingCart = true;
        break;
      }
    }

    return new ProductDetail(product, inShoppingCart);
  });
}
 
Example 3
Project: GitHub   File: DetailsInteractor.java   View Source Code Vote up 6 votes
private Observable<ProductDetail> getProductWithShoppingCartInfo(int productId) {
  List<Observable<?>> observables =
      Arrays.asList(backendApi.getProduct(productId), shoppingCart.itemsInShoppingCart());

  return Observable.combineLatest(observables, objects -> {
    Product product = (Product) objects[0];
    List<Product> productsInShoppingCart = (List<Product>) objects[1];
    boolean inShoppingCart = false;
    for (Product p : productsInShoppingCart) {
      if (p.getId() == productId) {
        inShoppingCart = true;
        break;
      }
    }

    return new ProductDetail(product, inShoppingCart);
  });
}
 
Example 4
Project: RxCommand   File: LoginViewModel.java   View Source Code Vote up 6 votes
public RxCommand<String> captchaCommand() {
    if (_captchaCommand == null) {
        Observable<Boolean> enabled = Observable.combineLatest(
                _phoneNumberValid,
                countdownCommand().executing(),
                (valid, executing) -> valid && !executing);

        _captchaCommand = RxCommand.create(enabled, o -> {
            String phone = phoneNumber.value().toString();
            Timber.i("fetch captcha with %s", phone);
            Observable<String> fetchCode =  fetchCaptcha(phone);
            Observable<String> countdown =  Observable.defer(() -> countdownCommand().execute(null).ignoreElements().toObservable()) ;
            return Observable.concat(fetchCode, countdown);
        });
    }
    return _captchaCommand;
}
 
Example 5
Project: RxCommand   File: LoginViewModel.java   View Source Code Vote up 5 votes
public RxCommand<Boolean> loginCommand() {
    if (_loginCommand == null) {
        Observable<Boolean> loginInputValid = Observable.combineLatest(
                _captchaValid,
                _phoneNumberValid,
                (captchaValid, phoneValid) -> captchaValid && phoneValid);

        _loginCommand = RxCommand.create(loginInputValid, o -> {
            String phone = this.phoneNumber.value().toString();
            String captcha = this.captcha.value().toString();
            return login(phone, captcha);
        });
    }
    return _loginCommand;
}