diff --git a/src/main/java/ru/touchin/roboswag/components/extensions/Delegates.kt b/src/main/java/ru/touchin/roboswag/components/extensions/Delegates.kt index c8e0538..050bd6b 100644 --- a/src/main/java/ru/touchin/roboswag/components/extensions/Delegates.kt +++ b/src/main/java/ru/touchin/roboswag/components/extensions/Delegates.kt @@ -14,3 +14,11 @@ inline fun Delegates.observable( ): ReadWriteProperty = object : ObservableProperty(initialValue) { override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = onChange(newValue) } + +inline fun Delegates.distinctUntilChanged( + initialValue: T, + crossinline onChange: (newValue: T) -> Unit +): ReadWriteProperty = object : ObservableProperty(initialValue) { + override fun afterChange(property: KProperty<*>, oldValue: T, newValue: T) = + if (newValue != null && oldValue != newValue) onChange(newValue) else Unit +} diff --git a/src/main/java/ru/touchin/roboswag/components/utils/destroyable/BaseDestroyable.kt b/src/main/java/ru/touchin/roboswag/components/utils/destroyable/BaseDestroyable.kt index 418f2c4..7d7f520 100644 --- a/src/main/java/ru/touchin/roboswag/components/utils/destroyable/BaseDestroyable.kt +++ b/src/main/java/ru/touchin/roboswag/components/utils/destroyable/BaseDestroyable.kt @@ -24,51 +24,41 @@ open class BaseDestroyable : Destroyable { */ fun onDestroy() = subscriptions.dispose() - override fun untilDestroy( - flowable: Flowable, + override fun Flowable.untilDestroy( onNextAction: (T) -> Unit, onErrorAction: (Throwable) -> Unit, onCompletedAction: () -> Unit - ): Disposable = flowable - .observeOn(AndroidSchedulers.mainThread()) + ): Disposable = observeOn(AndroidSchedulers.mainThread()) .subscribe(onNextAction, onErrorAction, onCompletedAction) .also { subscriptions.add(it) } - override fun untilDestroy( - observable: Observable, + override fun Observable.untilDestroy( onNextAction: (T) -> Unit, onErrorAction: (Throwable) -> Unit, onCompletedAction: () -> Unit - ): Disposable = observable - .observeOn(AndroidSchedulers.mainThread()) + ): Disposable = observeOn(AndroidSchedulers.mainThread()) .subscribe(onNextAction, onErrorAction, onCompletedAction) .also { subscriptions.add(it) } - override fun untilDestroy( - single: Single, + override fun Single.untilDestroy( onSuccessAction: (T) -> Unit, onErrorAction: (Throwable) -> Unit - ): Disposable = single - .observeOn(AndroidSchedulers.mainThread()) + ): Disposable = observeOn(AndroidSchedulers.mainThread()) .subscribe(onSuccessAction, onErrorAction) .also { subscriptions.add(it) } - override fun untilDestroy( - completable: Completable, + override fun Completable.untilDestroy( onCompletedAction: () -> Unit, onErrorAction: (Throwable) -> Unit - ): Disposable = completable - .observeOn(AndroidSchedulers.mainThread()) + ): Disposable = observeOn(AndroidSchedulers.mainThread()) .subscribe(onCompletedAction, onErrorAction) .also { subscriptions.add(it) } - override fun untilDestroy( - maybe: Maybe, + override fun Maybe.untilDestroy( onSuccessAction: (T) -> Unit, onErrorAction: (Throwable) -> Unit, onCompletedAction: () -> Unit - ): Disposable = maybe - .observeOn(AndroidSchedulers.mainThread()) + ): Disposable = observeOn(AndroidSchedulers.mainThread()) .subscribe(onSuccessAction, onErrorAction, onCompletedAction) .also { subscriptions.add(it) } diff --git a/src/main/java/ru/touchin/roboswag/components/utils/destroyable/Destroyable.kt b/src/main/java/ru/touchin/roboswag/components/utils/destroyable/Destroyable.kt index b5c7c16..270318d 100644 --- a/src/main/java/ru/touchin/roboswag/components/utils/destroyable/Destroyable.kt +++ b/src/main/java/ru/touchin/roboswag/components/utils/destroyable/Destroyable.kt @@ -40,8 +40,7 @@ interface Destroyable { * @param T Type of emitted by observable items; * @return [Disposable] which is wrapping source observable to unsubscribe from it onDestroy. */ - fun untilDestroy( - flowable: Flowable, + fun Flowable.untilDestroy( onNextAction: (T) -> Unit = Functions.emptyConsumer()::accept, onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)), onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run @@ -58,8 +57,7 @@ interface Destroyable { * @param T Type of emitted by observable items; * @return [Disposable] which is wrapping source observable to unsubscribe from it onDestroy. */ - fun untilDestroy( - observable: Observable, + fun Observable.untilDestroy( onNextAction: (T) -> Unit = Functions.emptyConsumer()::accept, onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)), onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run @@ -76,8 +74,7 @@ interface Destroyable { * @param T Type of emitted by single items; * @return [Disposable] which is wrapping source single to unsubscribe from it onDestroy. */ - fun untilDestroy( - single: Single, + fun Single.untilDestroy( onSuccessAction: (T) -> Unit = Functions.emptyConsumer()::accept, onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)) ): Disposable @@ -92,8 +89,7 @@ interface Destroyable { * @param onErrorAction Action which will raise on every [io.reactivex.CompletableEmitter.onError] throwable; * @return [Disposable] which is wrapping source completable to unsubscribe from it onDestroy. */ - fun untilDestroy( - completable: Completable, + fun Completable.untilDestroy( onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run, onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)) ): Disposable @@ -108,8 +104,7 @@ interface Destroyable { * @param onErrorAction Action which will raise on every [io.reactivex.MaybeEmitter.onError] throwable; * @return [Disposable] which is wrapping source maybe to unsubscribe from it onDestroy. */ - fun untilDestroy( - maybe: Maybe, + fun Maybe.untilDestroy( onSuccessAction: (T) -> Unit = Functions.emptyConsumer()::accept, onErrorAction: (Throwable) -> Unit = getActionThrowableForAssertion(Lc.getCodePoint(this, 2)), onCompletedAction: () -> Unit = Functions.EMPTY_ACTION::run