diff --git a/src/main/java/ru/touchin/templates/TouchinService.java b/src/main/java/ru/touchin/templates/TouchinService.java index 570ee05..aca5d2b 100644 --- a/src/main/java/ru/touchin/templates/TouchinService.java +++ b/src/main/java/ru/touchin/templates/TouchinService.java @@ -28,6 +28,8 @@ import android.support.annotation.NonNull; import io.reactivex.Completable; import io.reactivex.CompletableEmitter; import io.reactivex.Emitter; +import io.reactivex.Maybe; +import io.reactivex.MaybeEmitter; import io.reactivex.Observable; import io.reactivex.Single; import io.reactivex.SingleEmitter; @@ -252,6 +254,53 @@ public abstract class TouchinService extends Service { Functions.emptyConsumer(), onErrorAction, onCompletedAction); } + /** + * Method should be used to guarantee that maybe won't be subscribed after onDestroy. + * It is automatically subscribing to maybe. + * Don't forget to process errors if maybe can emit them. + * + * @param maybe {@link Maybe} to subscribe until onDestroy; + * @param Type of emitted by maybe items; + * @return {@link Disposable} which is wrapping source maybe to unsubscribe from it onDestroy. + */ + @NonNull + public Disposable untilDestroy(@NonNull final Maybe maybe) { + return untilDestroy(maybe, Functions.emptyConsumer(), getActionThrowableForAssertion(Lc.getCodePoint(this, 1))); + } + + /** + * Method should be used to guarantee that maybe won't be subscribed after onDestroy. + * It is automatically subscribing to maybe and calls onSuccessAction on emitted item. + * Don't forget to process errors if maybe can emit them. + * + * @param maybe {@link Maybe} to subscribe until onDestroy; + * @param onSuccessAction Action which will raise on {@link MaybeEmitter#onSuccess(Object)} item; + * @param Type of emitted by maybe items; + * @return {@link Disposable} which is wrapping source maybe to unsubscribe from it onDestroy. + */ + @NonNull + public Disposable untilDestroy(@NonNull final Maybe maybe, @NonNull final Consumer onSuccessAction) { + return untilDestroy(maybe, onSuccessAction, getActionThrowableForAssertion(Lc.getCodePoint(this, 1))); + } + + /** + * Method should be used to guarantee that maybe won't be subscribed after onDestroy. + * It is automatically subscribing to maybe and calls onSuccessAction and onErrorAction on maybe events. + * Don't forget to process errors if maybe can emit them. + * + * @param maybe {@link Maybe} to subscribe until onDestroy; + * @param onSuccessAction Action which will raise on {@link MaybeEmitter#onSuccess(Object)} item; + * @param onErrorAction Action which will raise on every {@link MaybeEmitter#onError(Throwable)} throwable; + * @param Type of emitted by maybe items; + * @return {@link Disposable} which is wrapping source maybe to unsubscribe from it onDestroy. + */ + @NonNull + public Disposable untilDestroy(@NonNull final Maybe maybe, + @NonNull final Consumer onSuccessAction, + @NonNull final Consumer onErrorAction) { + return until(maybe.toObservable(), isCreatedSubject.map(created -> !created), onSuccessAction, onErrorAction, Functions.EMPTY_ACTION); + } + @NonNull private Disposable until(@NonNull final Observable observable, @NonNull final Observable conditionSubject,