Ejemplo n.º 1
0
  @NonNull
  public Observable<PageResult<T>> load(
      @NonNull DataConverter<T> dataConverter,
      @NonNull DatabaseQuery databaseQuery,
      @NonNull Observable<Page> pageObservable) {
    final SparseArrayCompat<T> allItems = new SparseArrayCompat<>();
    final Observable<Cursor> cursorObservable =
        database.load(databaseQuery).doOnNext(cursor -> allItems.clear());

    return Observable.combineLatest(
        pageObservable,
        cursorObservable,
        (page, cursor) -> {
          final List<T> pageItems = new ArrayList<>();
          for (int i = page.getStart(),
                  size = Math.min(cursor.getCount(), page.getStart() + page.getSize());
              i < size;
              i++) {
            T item = allItems.get(i);
            if (item == null) {
              cursor.moveToPosition(i);
              item = dataConverter.from(cursor);
              allItems.put(i, item);
            }
            pageItems.add(item);
          }

          return new PageResult<>(cursor, allItems, pageItems, page);
        });
  }
  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));
  }
 public Observable<SoftwareUpdateData> softwareUpdateObservable3() {
   return Observable.combineLatest(
       sprinklerDataManager.update3(true),
       sprinklerDataManager.versions(),
       new Func2<Update, Versions, SoftwareUpdateData>() {
         @Override
         public SoftwareUpdateData call(Update update, Versions versions) {
           return convertData(update, versions);
         }
       });
 }
Ejemplo n.º 4
0
  @Test
  public void testErrorPropagatesWhenNoOutstandingRequests() {
    Observable<Long> timer =
        Observable.timer(0, 1, TimeUnit.MICROSECONDS)
            .doOnEach(
                new Action1<Notification<? super Long>>() {

                  @Override
                  public void call(Notification<? super Long> n) {
                    //                                                System.out.println("BEFORE " +
                    // n);
                  }
                })
            .observeOn(Schedulers.newThread())
            .doOnEach(
                new Action1<Notification<? super Long>>() {

                  @Override
                  public void call(Notification<? super Long> n) {
                    try {
                      Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                    //                                                System.out.println("AFTER " +
                    // n);
                  }
                });

    TestSubscriber<Long> ts = new TestSubscriber<Long>();

    Observable.combineLatest(
            timer,
            Observable.<Integer>never(),
            new Func2<Long, Integer, Long>() {

              @Override
              public Long call(Long t1, Integer t2) {
                return t1;
              }
            })
        .take(RxRingBuffer.SIZE * 2)
        .subscribe(ts);

    ts.awaitTerminalEvent();
    assertEquals(1, ts.getOnErrorEvents().size());
    assertEquals(MissingBackpressureException.class, ts.getOnErrorEvents().get(0).getClass());
  }
Ejemplo n.º 5
0
  public static void main(String[] args) {

    Observable<String> lineInput =
        Observable.create(
            (Subscriber<? super String> subscriber) -> {
              if (subscriber.isUnsubscribed()) return;

              BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

              String line = null;

              try {
                while (!subscriber.isUnsubscribed() && (line = reader.readLine()) != null) {
                  subscriber.onNext(line);
                }

                if (!subscriber.isUnsubscribed()) {
                  subscriber.onCompleted();
                }
              } catch (IOException e) {
                subscriber.onError(e);
              }
            });

    ConnectableObservable<String> sourceObs = lineInput.publish();

    Function<Pattern, Observable<Integer>> lineParser =
        (pattern) ->
            sourceObs
                .filter(
                    line -> {
                      return pattern.matcher(line).matches();
                    })
                .map(
                    line -> {
                      return Integer.parseInt(line.substring(line.indexOf("=") + 1).trim());
                    });

    Subscriber<Integer> whenUnsub =
        new SafeSubscriber<Integer>(
            new Subscriber<Integer>() {

              @Override
              public void onCompleted() {}

              @Override
              public void onError(Throwable throwable) {}

              @Override
              public void onNext(Integer s) {
                System.out.println(s);
              }
            });

    whenUnsub.add(
        Subscriptions.create(
            () -> {
              System.out.println("unsubscribe callback");
            }));

    Observable.combineLatest(
            lineParser.apply(Pattern.compile("^a.*")).startWith(0),
            lineParser.apply(Pattern.compile("^b.*")).startWith(0),
            (x, y) -> x + y)
        .subscribe(whenUnsub);

    sourceObs.connect();
  }
Ejemplo n.º 6
0
  public static void main(String[] args) {
    Observable<Integer> observable1 =
        Observable.just(1, 2, 3, 4, 5)
            .subscribeOn(Schedulers.newThread())
            .map(
                new Func1<Integer, Integer>() {
                  @Override
                  public Integer call(Integer integer) {
                    int millis = new Random().nextInt(1000);
                    System.out.println(integer + "sleep, millis = " + millis);
                    try {
                      Thread.sleep(millis);
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }
                    return integer;
                  }
                });
    Observable<String> observable2 =
        Observable.just("A", "b", "c", "d")
            .subscribeOn(Schedulers.newThread())
            .map(
                new Func1<String, String>() {
                  @Override
                  public String call(String integer) {
                    int millis = new Random().nextInt(1000);
                    System.out.println("string : " + integer + ",sleep:" + millis);
                    try {
                      Thread.sleep(millis);
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }
                    return integer;
                  }
                });

    //        observable1.subscribe(o1-> System.out.println("o1 = " + o1));
    //        observable2.subscribe(o2-> System.out.println("o2 = " + o2));

    Observable.combineLatest(
            observable1,
            observable2,
            new Func2<Integer, String, String>() {
              @Override
              public String call(Integer integer, String s) {
                return integer + "**" + s;
              }
            })
        .subscribe(s -> System.out.println("s = " + s));

    //        Observable.combineLatest(observable2, observable1, new Func2<String, Integer,
    // String>() {
    //            @Override
    //            public String call(String s,Integer integer)  {
    //                return integer + "*_*" + s;
    //            }
    //        }).subscribe(s -> System.out.println("s2 = " + s));

    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  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);
                  }
                }));
  }