Flowable handling, SingleLiveEvent
This commit is contained in:
parent
fffda150e5
commit
f84fae765d
|
|
@ -2,17 +2,26 @@ package ru.touchin.templates.livedata
|
|||
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Maybe
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import ru.touchin.roboswag.components.utils.destroyable.Destroyable
|
||||
import ru.touchin.templates.viewmodel.CompletableEvent
|
||||
import ru.touchin.templates.viewmodel.MaybeEvent
|
||||
import ru.touchin.templates.viewmodel.ObservableEvent
|
||||
import ru.touchin.templates.viewmodel.SingleEvent
|
||||
import ru.touchin.templates.livedata.event.CompletableEvent
|
||||
import ru.touchin.templates.livedata.event.MaybeEvent
|
||||
import ru.touchin.templates.livedata.event.ObservableEvent
|
||||
import ru.touchin.templates.livedata.event.SingleEvent
|
||||
|
||||
class BaseLiveDataDispatcher(private val destroyable: Destroyable) : LiveDataDispatcher {
|
||||
|
||||
override fun <T> Flowable<T>.dispatchTo(liveData: MutableLiveData<ObservableEvent<T>>) {
|
||||
liveData.value = ObservableEvent.Loading(liveData.value?.data)
|
||||
destroyable.untilDestroy(this,
|
||||
{ data -> liveData.value = ObservableEvent.Success(data) },
|
||||
{ throwable -> liveData.value = ObservableEvent.Error(throwable, liveData.value?.data) },
|
||||
{ liveData.value = ObservableEvent.Completed(liveData.value?.data) })
|
||||
}
|
||||
|
||||
override fun <T> Observable<T>.dispatchTo(liveData: MutableLiveData<ObservableEvent<T>>) {
|
||||
liveData.value = ObservableEvent.Loading(liveData.value?.data)
|
||||
destroyable.untilDestroy(this,
|
||||
|
|
@ -29,9 +38,9 @@ class BaseLiveDataDispatcher(private val destroyable: Destroyable) : LiveDataDis
|
|||
}
|
||||
|
||||
override fun Completable.dispatchTo(liveData: MutableLiveData<CompletableEvent>) {
|
||||
liveData.value = CompletableEvent.Loading()
|
||||
liveData.value = CompletableEvent.Loading
|
||||
destroyable.untilDestroy(this,
|
||||
{ liveData.value = CompletableEvent.Completed() },
|
||||
{ liveData.value = CompletableEvent.Completed },
|
||||
{ throwable -> liveData.value = CompletableEvent.Error(throwable) })
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,16 +2,19 @@ package ru.touchin.templates.livedata
|
|||
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Maybe
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Single
|
||||
import ru.touchin.templates.viewmodel.CompletableEvent
|
||||
import ru.touchin.templates.viewmodel.MaybeEvent
|
||||
import ru.touchin.templates.viewmodel.ObservableEvent
|
||||
import ru.touchin.templates.viewmodel.SingleEvent
|
||||
import ru.touchin.templates.livedata.event.CompletableEvent
|
||||
import ru.touchin.templates.livedata.event.MaybeEvent
|
||||
import ru.touchin.templates.livedata.event.ObservableEvent
|
||||
import ru.touchin.templates.livedata.event.SingleEvent
|
||||
|
||||
interface LiveDataDispatcher {
|
||||
|
||||
fun <T> Flowable<T>.dispatchTo(liveData: MutableLiveData<ObservableEvent<T>>)
|
||||
|
||||
fun <T> Observable<T>.dispatchTo(liveData: MutableLiveData<ObservableEvent<T>>)
|
||||
|
||||
fun <T> Single<T>.dispatchTo(liveData: MutableLiveData<SingleEvent<T>>)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
package ru.touchin.templates.livedata
|
||||
|
||||
import android.arch.lifecycle.LifecycleOwner
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.arch.lifecycle.Observer
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
class SingleLiveEvent<T> : MutableLiveData<T>() {
|
||||
|
||||
private val pending = AtomicBoolean(false)
|
||||
|
||||
override fun observe(owner: LifecycleOwner, observer: Observer<T?>) {
|
||||
super.observe(owner, Observer { value ->
|
||||
if (pending.compareAndSet(true, false)) {
|
||||
observer.onChanged(value)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun setValue(value: T) {
|
||||
pending.set(true)
|
||||
super.setValue(value)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package ru.touchin.templates.livedata.event
|
||||
|
||||
/**
|
||||
* Created by Denis Karmyshakov on 14.03.18.
|
||||
* Event class that emits from [io.reactivex.Completable].
|
||||
*/
|
||||
sealed class CompletableEvent {
|
||||
|
||||
object Loading: CompletableEvent()
|
||||
|
||||
object Completed: CompletableEvent()
|
||||
|
||||
data class Error(val throwable: Throwable): CompletableEvent()
|
||||
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.templates.viewmodel
|
||||
package ru.touchin.templates.livedata.event
|
||||
|
||||
/**
|
||||
* Created by Denis Karmyshakov on 14.03.18.
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.templates.viewmodel
|
||||
package ru.touchin.templates.livedata.event
|
||||
|
||||
/**
|
||||
* Created by Denis Karmyshakov on 14.03.18.
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package ru.touchin.templates.viewmodel
|
||||
package ru.touchin.templates.livedata.event
|
||||
|
||||
/**
|
||||
* Created by Denis Karmyshakov on 14.03.18.
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package ru.touchin.templates.viewmodel
|
||||
|
||||
/**
|
||||
* Created by Denis Karmyshakov on 14.03.18.
|
||||
* Event class that emits from [io.reactivex.Completable].
|
||||
*/
|
||||
sealed class CompletableEvent {
|
||||
|
||||
class Loading: CompletableEvent()
|
||||
|
||||
class Completed: CompletableEvent()
|
||||
|
||||
class Error(val throwable: Throwable): CompletableEvent()
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue