Skip to content

Commit

Permalink
[Add/#9] LiveData를 위한 Event.kt 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
gaeun5744 committed Jun 27, 2023
1 parent af70c6c commit c0c7c0d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/org/android/go/sopt/util/Event.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.android.go.sopt.util

import androidx.lifecycle.Observer

class Event<T>(private val content: T) {

private var hasBeenHandled = false

fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
}

class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>) {
event?.getContentIfNotHandled()?.let {
onEventUnhandledContent(it)
}
}
}

0 comments on commit c0c7c0d

Please sign in to comment.