-
-
Save JoseAlcerreca/5b661f1800e1e654f07cc54fe87441af to your computer and use it in GitHub Desktop.
/** | |
* Used as a wrapper for data that is exposed via a LiveData that represents an event. | |
*/ | |
open class Event<out T>(private val content: T) { | |
var hasBeenHandled = false | |
private set // Allow external read but not write | |
/** | |
* Returns the content and prevents its use again. | |
*/ | |
fun getContentIfNotHandled(): T? { | |
return if (hasBeenHandled) { | |
null | |
} else { | |
hasBeenHandled = true | |
content | |
} | |
} | |
/** | |
* Returns the content, even if it's already been handled. | |
*/ | |
fun peekContent(): T = content | |
} | |
For those working with Kotlin Flow I have created an extension function, onEachEvent
, using the Event
wrapper above. You may find the full explanation and code here.
OnEachEvent.kt
/**
* Returns a flow which performs the given [action] on each value of the original flow's [Event].
*/
public fun <T> Flow<Event<T?>>.onEachEvent(action: suspend (T) -> Unit): Flow<T> = transform { value ->
value.getContentIfNotHandled()?.let {
action(it)
return@transform emit(it)
}
}
I thought about a solution that's based on the Event class but enables more than one client to consume an event (each client only once). My current approach is an EventRepeater class which takes an Event from a source LiveData and forwards it to a new (always unconsumed) Event for each client. To prevent the same event is forwarded twice, it will be compared with the previous event object:
class EventRepeater<T>(source: LiveData<Event<T>>) : MediatorLiveData<Event<T>>() {
var previousEvent: Event<T>? = null
init {
addSource(source) { event ->
if (event !== previousEvent) {
previousEvent = event
value = event?.let { Event(it.peekContent()) }
}
}
}
}
I modified the EventObserver class as well as the Event class. In the EventObserver class I added a secondary constructor that takes a string value to be used by the event class to check whether the event has been consumed by this specific observer (represented by that string value). This string value is passed on to the Event class that uses its presence (or absence) to determine whether to operate as a SingleObservationEvent or as a MultipleObservationEvent.
/**
* 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> : Observer<Event<T>> {
private val onEventUnhandledContent: (T) -> Unit
private var myStr: String? = null
constructor(onEventUnhandledContent: (T) -> Unit) {
this.onEventUnhandledContent = onEventUnhandledContent
}
constructor(onEventUnhandledContent: (T) -> Unit, str: String) {
this.onEventUnhandledContent = onEventUnhandledContent
myStr = str
}
override fun onChanged(event: Event<T>?) {
event?.getIfNotHandled(myStr)?.let { value ->
onEventUnhandledContent(value)
}
}
}
/**
* Used as a wrapper for data that is exposed via a LiveData that represents an event.
*/
data class Event<out T>(private val content: T) {
private val hasBeenHandled = AtomicBoolean(false)
private val lock = ReentrantLock()
private val handledMap = HashMap<String, Boolean>()
/**
* Returns the content and prevents its use again.
*/
fun getIfNotHandled(str: String? = null): T? {
var singleEvent = true
var allowContent = false
str?.let {
singleEvent = false
lock.withLock {
val handled = handledMap[it]
if (handled == null) {
allowContent = true
handledMap[it] = true
}
}
}
return if (singleEvent) {
if (hasBeenHandled.getAndSet(true))
null
else
content
} else {
if (allowContent)
content
else
null
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peek(): T = content
}
And to use it
-
Vanilla (Default SingleFire Observable)
myObservable.observe( this, EventObserver( onEventUnhandledContent = fun(it: String) { } }, myStr = "Observer1"))
-
MultipleObserver
myObservable.observe( this, EventObserver( onEventUnhandledContent = fun(it: String) { } }))
or the lambda version
myObservable.observe(this, EventObserver{ })
That looks nearly like my latest solution. However, I don't use a special EventObserver, since a Kotlin extension function can do the job:
class Event<out T>(private val content: T) {
private val consumedScopes = HashSet<String>()
fun isConsumed(scope: String = "") = consumedScopes.contains(scope)
@MainThread
fun consume(scope: String = ""): T? {
return if (isConsumed(scope)) {
null
} else {
consumedScopes.add(scope)
content
}
}
fun peek(): T = content
}
fun <T> LiveData<Event<T>>.observeEvent(lifecycleOwner: LifecycleOwner, scope: String = "", observer: Observer<T>) {
observe(lifecycleOwner) { event ->
event?.consume(scope)?.let { observer.onChanged(it) }
}
}
// How to use it
myObservable.observeEvent { ... }
myObservable.observeEvent("specialScope") { ... }
This is so concise.
In Kotlin, passing events to a single observer is also possible with Channel.receiveAsFlow()
and lifecycle-aware collector. Advantages:
- Can queue multiple events while observer is inactive (configuration change, app in background, fragment in back stack), with customizable buffer size and
onBufferOverflow
strategy - Optionally supports
null
values - No need to wrap data in
Event
That looks nearly like my latest solution. However, I don't use a special EventObserver, since a Kotlin extension function can do the job:
class Event<out T>(private val content: T) { private val consumedScopes = HashSet<String>()
Adding more syntactic sugar to choirwire's contribution
class Event<out T>(private val content: T) {
private val consumedScopes by lazy { HashSet<String>() }
fun isConsumed(scope: String = "") = scope in consumedScopes
@MainThread
fun consume(scope: String = ""): T? {
return content.takeIf { !isConsumed(scope) }?.also { consumedScopes.add(scope) }
}
fun peek(): T = content
}
If you want to use nullable
content
: