Created
March 29, 2021 07:19
-
-
Save DanteAndroid/874eede76a7672d35142ef130249c09a to your computer and use it in GitHub Desktop.
One-time event wrapper for LiveData
This file contains 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
package com.rrtv.utils | |
/** | |
* 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 | |
/** | |
* 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
Usage:
And observe
message
in your view: