Skip to content

Instantly share code, notes, and snippets.

@Flywith24
Created February 27, 2020 05:26

Revisions

  1. Flywith24 created this gist Feb 27, 2020.
    32 changes: 32 additions & 0 deletions Event.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    package com.whdx.library_login.bean

    /**
    * @author yyz (杨云召)
    * @date 2020/2/27
    * time 11:34
    * description
    * 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
    }