Skip to content

Instantly share code, notes, and snippets.

@MtaufiqH
Forked from JoseAlcerreca/Event.kt
Created April 26, 2021 02:40
Show Gist options
  • Select an option

  • Save MtaufiqH/30939ce311a7ae3be9a57191cb6423cb to your computer and use it in GitHub Desktop.

Select an option

Save MtaufiqH/30939ce311a7ae3be9a57191cb6423cb to your computer and use it in GitHub Desktop.

Revisions

  1. @JoseAlcerreca JoseAlcerreca created this gist Apr 26, 2018.
    26 changes: 26 additions & 0 deletions Event.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /**
    * 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
    }