Skip to content

Instantly share code, notes, and snippets.

@satoshun
Created October 26, 2017 23:51
Show Gist options
  • Save satoshun/2b05e03b6cb44f7070ff14c5601e51f5 to your computer and use it in GitHub Desktop.
Save satoshun/2b05e03b6cb44f7070ff14c5601e51f5 to your computer and use it in GitHub Desktop.
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