Created
October 26, 2017 23:51
-
-
Save satoshun/2b05e03b6cb44f7070ff14c5601e51f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MutableErrorLiveData<T> : MutableLiveData<Notification<T>>() { | |
fun observe(owner: LifecycleOwner, onNext: (T) -> Unit, onError: (Throwable) -> Unit) { | |
observe(owner, Observer { notification -> | |
notification ?: return@Observer | |
if (notification.isOnNext) { | |
onNext(notification.value!!) | |
} else if (notification.isOnError) { | |
onError(notification.error!!) | |
} | |
}) | |
} | |
fun getNextValue(): T? { | |
val notification = value | |
if (notification == null) { | |
return null | |
} | |
if (notification.isOnNext) { | |
return notification.value!! | |
} | |
return null | |
} | |
fun postNextValue(t: T) { | |
postValue(Notification.createOnNext(t)) | |
} | |
fun postErrorValue(t: Throwable) { | |
postValue(Notification.createOnError(t)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment