@Override public Completable writeDescriptor( @NonNull final UUID serviceUuid, @NonNull final UUID characteristicUuid, @NonNull final UUID descriptorUuid, @NonNull final byte[] data ) { return discoverServices() .flatMap(new Function<RxBleDeviceServices, SingleSource<BluetoothGattDescriptor>>() { @Override public SingleSource<BluetoothGattDescriptor> apply(RxBleDeviceServices rxBleDeviceServices) { return rxBleDeviceServices.getDescriptor(serviceUuid, characteristicUuid, descriptorUuid); } }) .flatMapCompletable(new Function<BluetoothGattDescriptor, CompletableSource>() { @Override public CompletableSource apply(BluetoothGattDescriptor bluetoothGattDescriptor) { return writeDescriptor(bluetoothGattDescriptor, data); } }); }
@NonNull private Completable setupCharacteristicNotification( final UUID bluetoothGattCharacteristicUUID, final NotificationSetupMode setupMode, final boolean enabled, final boolean isIndication ) { if (setupMode == NotificationSetupMode.DEFAULT) { final byte[] enableValue = isIndication ? ENABLE_INDICATION_VALUE : ENABLE_NOTIFICATION_VALUE; return getClientConfigurationDescriptor(bluetoothGattCharacteristicUUID) .flatMapCompletable(new Function<BluetoothGattDescriptor, Completable>() { @Override public Completable apply(BluetoothGattDescriptor bluetoothGattDescriptor) { return writeDescriptor(bluetoothGattDescriptor, enabled ? enableValue : DISABLE_NOTIFICATION_VALUE); } }); } else { return Completable.complete(); } }
@Test public void dispose() { TestHelper.checkDisposed(Single.just(1).flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer v) throws Exception { return Completable.complete(); } })); } }
@Test public void singleCompletable() throws Exception { List<Throwable> errors = TestHelper.trackPluginErrors(); try { TestObserver<Void> to = Single.just(1) .subscribeOn(Schedulers.io()) .flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer v) throws Exception { sleep(); return Completable.error(new TestException()); } }) .test(); cb.await(); beforeCancelSleep(to); to.cancel(); Thread.sleep(SLEEP_AFTER_CANCEL); to.assertEmpty(); assertTrue(errors.toString(), errors.isEmpty()); } finally { RxJavaPlugins.reset(); } }
@Test public void singleCompletable2() throws Exception { List<Throwable> errors = TestHelper.trackPluginErrors(); try { TestObserver<Integer> to = Single.just(1) .subscribeOn(Schedulers.io()) .flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer v) throws Exception { sleep(); return Completable.error(new TestException()); } }) .toSingleDefault(0) .test(); cb.await(); beforeCancelSleep(to); to.cancel(); Thread.sleep(SLEEP_AFTER_CANCEL); to.assertEmpty(); assertTrue(errors.toString(), errors.isEmpty()); } finally { RxJavaPlugins.reset(); } }
public Completable updateChannelInfo(final ChannelInfo info) { final Function<List<SubscriptionEntity>, CompletableSource> update = new Function<List<SubscriptionEntity>, CompletableSource>() { @Override public CompletableSource apply(@NonNull List<SubscriptionEntity> subscriptionEntities) { if (DEBUG) Log.d(TAG, "updateChannelInfo() called with: subscriptionEntities = [" + subscriptionEntities + "]"); if (subscriptionEntities.size() == 1) { SubscriptionEntity subscription = subscriptionEntities.get(0); // Subscriber count changes very often, making this check almost unnecessary. // Consider removing it later. if (!isSubscriptionUpToDate(info, subscription)) { subscription.setData(info.getName(), info.getAvatarUrl(), info.getDescription(), info.getSubscriberCount()); return Completable.fromRunnable(() -> subscriptionTable().update(subscription)); } } return Completable.complete(); } }; return subscriptionTable().getSubscription(info.getServiceId(), info.getUrl()) .firstOrError() .flatMapCompletable(update); }
@Test public void mapperReturnsNull() { final boolean[] b = { false }; Single.just(1) .flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer t) throws Exception { return null; } }) .test() .assertFailure(NullPointerException.class); assertFalse(b[0]); }
@Test public void mapperThrows() { final boolean[] b = { false }; Single.just(1) .flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer t) throws Exception { throw new TestException(); } }) .test() .assertFailure(TestException.class); assertFalse(b[0]); }
@Test public void normal() { final boolean[] b = { false }; Single.just(1) .flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer t) throws Exception { return Completable.complete().doOnComplete(new Action() { @Override public void run() throws Exception { b[0] = true; } }); } }) .test() .assertResult(); assertTrue(b[0]); }
@Test public void error() { final boolean[] b = { false }; Single.<Integer>error(new TestException()) .flatMapCompletable(new Function<Integer, Completable>() { @Override public Completable apply(Integer t) throws Exception { return Completable.complete().doOnComplete(new Action() { @Override public void run() throws Exception { b[0] = true; } }); } }) .test() .assertFailure(TestException.class); assertFalse(b[0]); }
@Override public Completable loginWithTwitter(final Activity activity) { return Single.create((SingleOnSubscribe<AuthCredential>) e -> { twitterButton = new TwitterLoginButton(activity); twitterButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { e.onSuccess(TwitterAuthProvider.getCredential(result.data.getAuthToken().token, result.data.getAuthToken().secret)); } @Override public void failure(TwitterException exception) { e.onError(exception); } }); twitterButton.callOnClick(); }).flatMapCompletable(authCredential -> signInWithCredential(activity, authCredential)); }
}).flatMapCompletable(user -> new UserWrapper(user).push()).subscribeOn(Schedulers.single());
public Completable deleteThreadWithEntityID(final String entityID) { return Single.create((SingleOnSubscribe<Thread>) e -> { final Thread thread = DaoCore.fetchEntityWithEntityID(Thread.class, entityID); e.onSuccess(thread); }).flatMapCompletable(thread -> new ThreadWrapper(thread).deleteThread()).subscribeOn(Schedulers.single()); }
@Override public Completable loginWithFacebook(final Activity activity) { return Single.create((SingleOnSubscribe<AuthCredential>) e -> { LoginButton button = new LoginButton(activity); facebookCallbackManager = CallbackManager.Factory.create(); button.registerCallback(facebookCallbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { e.onSuccess(FacebookAuthProvider.getCredential(loginResult.getAccessToken().getToken())); } @Override public void onCancel() { e.onError(null); } @Override public void onError(FacebookException error) { e.onError(error); } }); button.callOnClick(); }).flatMapCompletable(authCredential -> signInWithCredential(activity, authCredential)); }
@Override public Completable loginWithGoogle(final Activity activity) { return Single.create((SingleOnSubscribe<AuthCredential>) e -> { googleClient = new GoogleApiClient.Builder(activity) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleClient); activity.startActivityForResult(signInIntent, RC_GOOGLE_SIGN_IN); googleSignInCompleteListener = result -> { if(result.isSuccess()) { AuthCredential credential = GoogleAuthProvider.getCredential(result.getSignInAccount().getIdToken(), null); e.onSuccess(credential); } else { e.onError(new Exception(result.getStatus().toString())); } }; }).flatMapCompletable(authCredential -> signInWithCredential(activity, authCredential)); }
public Completable authenticateWithCachedToken() { return Single.create((SingleOnSubscribe<FirebaseUser>) emitter-> { if (isAuthenticating()) { emitter.onError(ChatError.getError(ChatError.Code.AUTH_IN_PROCESS, "Cant execute two auth in parallel")); } else { setAuthStatus(AuthStatus.CHECKING_IF_AUTH); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { emitter.onSuccess(user); } else { emitter.onError(ChatError.getError(ChatError.Code.NO_AUTH_DATA, "No auth bundle found")); } } }) .flatMapCompletable(this::authenticateWithUser) .doOnTerminate(this::setAuthStateToIdle) // Whether we complete successfully or not, we set the status to idle .subscribeOn(Schedulers.single()); }
public Completable addComment(String commentText) { return localCommentRepository.add(ModelConstants.DUMMY_PHOTO_ID, commentText) .flatMapCompletable(syncCommentUseCase::syncComment); } }
@Override public Publisher<?> persistFramedEntries(Flowable<Map<String, Object>> framedEntries) { return framedEntries .flatMapIterable(BdioDocument::toGraphNodes) .reduce(this, TinkerGraphBlackDuckIoReader::accumulate) .flatMapCompletable(TinkerGraphBlackDuckIoReader::checkForMissingNodes) .toFlowable(); }
.flatMapCompletable(this::authenticateWithUser) .doOnTerminate(this::setAuthStateToIdle) .subscribeOn(Schedulers.single());
public static Completable doInConnectionCompletable(Func1<? super SQLConnection, ? extends Completable> func){ return Completable.defer(() -> { Single<SQLConnection> connection = getConnection(); return connection.flatMapCompletable(conn -> { return func.call(conn).doAfterTerminate(() -> { conn.close(); }); }); }); }