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 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) }) + } +}