io.reactivex.CompletableOnSubscribe Java Examples

The following examples show how to use io.reactivex.CompletableOnSubscribe. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RxActionDelegate.java    From pandroid with Apache License 2.0 6 votes vote down vote up
public static Completable completable(final OnSubscribeAction<Void> subscribe){
    return Completable.create(new CompletableOnSubscribe() {
        @Override
        public void subscribe(final CompletableEmitter emitter) throws Exception {
            RxActionDelegate<Void> delegate = new RxActionDelegate<>(new ActionDelegate<Void>() {
                @Override
                public void onSuccess(Void result) {
                    emitter.onComplete();
                }

                @Override
                public void onError(Exception e) {
                    emitter.onError(e);
                }
            });
            emitter.setDisposable(delegate);
            subscribe.subscribe(delegate);
        }
    });
}
 
Example #2
Source File: MaybeConsumers.java    From science-journal with Apache License 2.0 6 votes vote down vote up
/**
 * Given an operation that takes a {@link MaybeConsumer<Success>}, create a JavaRX {@link
 * Completable} that succeeds iff the operation does.
 *
 * <p>Example:
 *
 * <pre>
 *     // update the experiment, and then log that it was successful
 *     DataController dc = getDataController();
 *     MaybeConsumers.buildCompleteable(mc -> dc.updateExperiment(e.getExperimentId(), mc))
 *                   .subscribe(() -> log("successfully updated!"));
 * </pre>
 */
public static Completable buildCompleteable(
    io.reactivex.functions.Consumer<MaybeConsumer<Success>> c) {
  return Completable.create(
      new CompletableOnSubscribe() {
        @Override
        public void subscribe(CompletableEmitter emitter) throws Exception {
          c.accept(
              new MaybeConsumer<Success>() {
                @Override
                public void success(Success value) {
                  emitter.onComplete();
                }

                @Override
                public void fail(Exception e) {
                  emitter.onError(e);
                }
              });
        }
      });
}
 
Example #3
Source File: Rx2Apollo.java    From apollo-android with MIT License 6 votes vote down vote up
/**
 * Converts an {@link ApolloPrefetch} to a synchronous Completable
 *
 * @param prefetch the ApolloPrefetch to convert
 * @return the converted Completable
 * @throws NullPointerException if prefetch == null
 */
@NotNull
@CheckReturnValue
public static Completable from(@NotNull final ApolloPrefetch prefetch) {
  checkNotNull(prefetch, "prefetch == null");

  return Completable.create(new CompletableOnSubscribe() {
    @Override public void subscribe(final CompletableEmitter emitter) {
      cancelOnCompletableDisposed(emitter, prefetch);
      prefetch.enqueue(new ApolloPrefetch.Callback() {
        @Override public void onSuccess() {
          if (!emitter.isDisposed()) {
            emitter.onComplete();
          }
        }

        @Override public void onFailure(@NotNull ApolloException e) {
          Exceptions.throwIfFatal(e);
          if (!emitter.isDisposed()) {
            emitter.onError(e);
          }
        }
      });
    }
  });
}
 
Example #4
Source File: RealValueStore.java    From RxStore with Apache License 2.0 6 votes vote down vote up
@Override @NonNull public Completable observeClear() {
  return Completable.create(new CompletableOnSubscribe() {
    @Override public void subscribe(final CompletableEmitter emitter) throws Exception {
      runInWriteLock(readWriteLock, new ThrowingRunnable() {
        @Override public void run() throws Exception {
          if (file.exists() && !file.delete()) {
            throw new IOException("Clear operation on store failed.");
          } else {
            emitter.onComplete();
          }

          updateSubject.onNext(ValueUpdate.<T>empty());
        }
      });
    }
  });
}
 
Example #5
Source File: ExampleUnitTest.java    From RxAndroid-Sample with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompletableObservable() {

    Completable.create(new CompletableOnSubscribe() {
        @Override
        public void subscribe(CompletableEmitter emitter) {
            emitter.onComplete();
        }
    })
            .subscribeOn(Schedulers.io())
            .subscribe(new CompletableObserver() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onComplete() {
                    System.out.println("onComplete is called");
                }

                @Override
                public void onError(Throwable e) {
                    System.out.println("onError is called" + e.getMessage());
                }
            });
}
 
Example #6
Source File: CropImageView.java    From SimpleCropView with MIT License 5 votes vote down vote up
/**
 * Load image from Uri with RxJava2
 *
 * @param sourceUri Image Uri
 *
 * @see #load(Uri)
 *
 * @return Completable of loading image
 */
public Completable loadAsCompletable(final Uri sourceUri, final boolean useThumbnail,
    final RectF initialFrameRect) {
  return Completable.create(new CompletableOnSubscribe() {

    @Override public void subscribe(@NonNull final CompletableEmitter emitter) throws Exception {

      mInitialFrameRect = initialFrameRect;
      mSourceUri = sourceUri;

      if (useThumbnail) {
        applyThumbnail(sourceUri);
      }

      final Bitmap sampled = getImage(sourceUri);

      mHandler.post(new Runnable() {
        @Override public void run() {
          mAngle = mExifRotation;
          setImageDrawableInternal(new BitmapDrawable(getResources(), sampled));
          emitter.onComplete();
        }
      });
    }
  }).doOnSubscribe(new Consumer<Disposable>() {
    @Override public void accept(@NonNull Disposable disposable) throws Exception {
      mIsLoading.set(true);
    }
  }).doFinally(new Action() {
    @Override public void run() throws Exception {
      mIsLoading.set(false);
    }
  });
}