Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Observer for Events, simplifying the pattern of checking if the Event's content has already been handled.
/**
* An [Observer] for [Event]s, simplifying the pattern of checking if the [Event]'s content has
* already been handled.
*
* [onEventUnhandledContent] is *only* called if the [Event]'s contents has not been handled.
*/
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let { value ->
onEventUnhandledContent(value)
}
}
}
@kroegerama

This comment has been minimized.

Copy link

commented Jun 18, 2018

You can make lines 9..11 a one liner:

event?.getContentIfNotHandled()?.let(onEventUnhandledContent)
@AdamSHurwitz

This comment has been minimized.

Copy link

commented Sep 23, 2018

Thank you! This solved my need for handling click events with LiveData. One small tweak: I changed the name of the method getContentIfNotHandled to getEventIfNotHandled since it is more generic.

@AlexBacich

This comment has been minimized.

Copy link

commented Oct 30, 2018

How can I use this EventObserver? Can you give an example. Probably topic is still too hard for me, but the original article was clear: https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150

@eyeahs

This comment has been minimized.

Copy link

commented Nov 1, 2018

inline fun <T> LiveData<Event<T>>.observeEvent(owner: LifecycleOwner, crossinline onEventUnhandledContent: (T) -> Unit) {
    observe(owner, Observer { it?.getContentIfNotHandled()?.let(onEventUnhandledContent) })
}
@WebTiger89

This comment has been minimized.

Copy link

commented Nov 28, 2018

Am I wrong or can't EventObserver handle null events? i.e. new Event<>(null). I'm not in Kotlin but when I got it right it?.getContentIfNotHandled()?.let(onEventUnhandledContent)

onEventUnhandledContent

is only called if event != null and the return value != null so with new Event<>(null)

onEventUnhandledContent

is never called?!

@dri94

This comment has been minimized.

Copy link

commented Feb 26, 2019

@WebTiger89 yes you are correct, not completely ideal

@ildar2

This comment has been minimized.

Copy link

commented Mar 13, 2019

@WebTiger89 you can use Null-objects for empty events, e.g. class VoidEvent or MyObject.EMPTY

@Nghicv

This comment has been minimized.

Copy link

commented Apr 28, 2019

Thank you

@OssamaDroid

This comment has been minimized.

Copy link

commented May 3, 2019

@AlexBacich You have to replace the Observer instantiation by the EventObserver one.
If we go back to the article you've mentioned (https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150) where it sounded pretty clear to you, we'll have something like this:
myViewModel.navigateToDetails.observe(this, EventObserver<T> { value -> startActivity(DetailsActivity...) } })
This is simply added to avoid the getContentIfNotHandled() checking every time :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.