From 24b380fff06b0c3147a976c7c0f6057e83ec8d1a Mon Sep 17 00:00:00 2001 From: Alemoore Date: Wed, 2 Mar 2022 15:44:15 +0300 Subject: [PATCH 1/2] use interface in RxViewModel --- .../src/main/java/ru/touchin/lifecycle/viewmodel/RxViewModel.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/RxViewModel.kt b/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/RxViewModel.kt index 74b8f67..5919bdf 100644 --- a/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/RxViewModel.kt +++ b/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/RxViewModel.kt @@ -8,7 +8,7 @@ import androidx.annotation.CallSuper */ open class RxViewModel( private val destroyable: BaseDestroyable = BaseDestroyable(), - private val liveDataDispatcher: BaseLiveDataDispatcher = BaseLiveDataDispatcher(destroyable) + private val liveDataDispatcher: LiveDataDispatcher = BaseLiveDataDispatcher(destroyable) ) : ViewModel(), Destroyable by destroyable, LiveDataDispatcher by liveDataDispatcher { @CallSuper From 7ed7a25bfeca2944d7fc09caaedf2047eac4ad34 Mon Sep 17 00:00:00 2001 From: Alemoore Date: Wed, 2 Mar 2022 15:46:55 +0300 Subject: [PATCH 2/2] add TestableLiveDataDispatcher without android classes --- .../viewmodel/TestableLiveDataDispatcher.kt | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/TestableLiveDataDispatcher.kt diff --git a/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/TestableLiveDataDispatcher.kt b/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/TestableLiveDataDispatcher.kt new file mode 100644 index 0000000..c7ea674 --- /dev/null +++ b/lifecycle-rx/src/main/java/ru/touchin/lifecycle/viewmodel/TestableLiveDataDispatcher.kt @@ -0,0 +1,49 @@ +package ru.touchin.lifecycle.viewmodel + +import androidx.lifecycle.MutableLiveData +import io.reactivex.Completable +import io.reactivex.Flowable +import io.reactivex.Maybe +import io.reactivex.Observable +import io.reactivex.Single +import io.reactivex.disposables.Disposable +import ru.touchin.lifecycle.event.ContentEvent +import ru.touchin.lifecycle.event.Event + +class TestableLiveDataDispatcher( + private val destroyable: BaseDestroyable = BaseDestroyable() +) : LiveDataDispatcher, Destroyable by destroyable { + + override fun Flowable.dispatchTo(liveData: MutableLiveData>): Disposable { + return untilDestroy( + { data -> liveData.value = ContentEvent.Success(data) }, + { throwable -> liveData.value = ContentEvent.Error(throwable, liveData.value?.data) }, + { liveData.value = ContentEvent.Complete(liveData.value?.data) }) + } + + override fun Observable.dispatchTo(liveData: MutableLiveData>): Disposable { + return untilDestroy( + { data -> liveData.value = ContentEvent.Success(data) }, + { throwable -> liveData.value = ContentEvent.Error(throwable, liveData.value?.data) }, + { liveData.value = ContentEvent.Complete(liveData.value?.data) }) + } + + override fun Single.dispatchTo(liveData: MutableLiveData>): Disposable { + return untilDestroy( + { data -> liveData.value = ContentEvent.Success(data) }, + { throwable -> liveData.value = ContentEvent.Error(throwable, liveData.value?.data) }) + } + + override fun Maybe.dispatchTo(liveData: MutableLiveData>): Disposable { + return untilDestroy( + { data -> liveData.value = ContentEvent.Success(data) }, + { throwable -> liveData.value = ContentEvent.Error(throwable, liveData.value?.data) }, + { liveData.value = ContentEvent.Complete(liveData.value?.data) }) + } + + override fun Completable.dispatchTo(liveData: MutableLiveData): Disposable { + return untilDestroy( + { liveData.value = Event.Complete }, + { throwable -> liveData.value = Event.Error(throwable) }) + } +}