permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.21k stars 1.44k forks source link

Solve multiple call of events #688

Closed mtrakal closed 3 years ago

mtrakal commented 3 years ago

https://github.com/permissions-dispatcher/PermissionsDispatcher/issues/687

hotchemi commented 3 years ago

Thank you, I was reading https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150 and it said using Event like the following is more recommended as SingleLiveEvent is that it’s restricted to one observer., what do you think?

/**
 * 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
}
hotchemi commented 3 years ago

https://github.com/permissions-dispatcher/PermissionsDispatcher/pull/689

hotchemi commented 3 years ago

Let me close the PR since we've already merged https://github.com/permissions-dispatcher/PermissionsDispatcher/pull/689, thank you for the support!