Skip to content

Instantly share code, notes, and snippets.

@CaiqueCoelho
Forked from JoseAlcerreca/Event.kt
Created October 10, 2018 04:26
Show Gist options
  • Select an option

  • Save CaiqueCoelho/13f9ae99279b1bdb2c25ec366ee280cf to your computer and use it in GitHub Desktop.

Select an option

Save CaiqueCoelho/13f9ae99279b1bdb2c25ec366ee280cf to your computer and use it in GitHub Desktop.
An event wrapper for data that is exposed via a LiveData that represents an event.
/**
* 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment