androidMain.tech.skot.view.live.SKLiveData.kt Maven / Gradle / Ivy
package tech.skot.view.live
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
open class SKLiveData(initialValue: D) : SKLiveDataCommon(initialValue) {
fun observe(lifecycleOwner: LifecycleOwner, onChanged: (d: D) -> Unit) =
LifecycleOwnerObserver(lifecycleOwner, onChanged).also { observers.add(it) }
fun setObserver(lifecycleOwner: LifecycleOwner, onChanged: (d: D) -> Unit): LifecycleOwnerObserver{
val currentObservers = observers.toSet()
currentObservers.forEach {
(it as? LifecycleOwnerObserver)?.apply {
onDestroy()
lifecycleOwner.lifecycle.removeObserver(lifecycleOwnerObserver)
}
}
return LifecycleOwnerObserver(lifecycleOwner, onChanged).also { observers.add(it) }
}
inner class LifecycleOwnerObserver(
val lifecycleOwner: LifecycleOwner,
onChanged: (d: D) -> Unit
) : Observer(onChanged) {
private val liveData = this@SKLiveData
fun remove() {
liveData.removeObserver(this)
}
init {
if (shouldBeActive()) {
onBecomeActive()
}
}
val lifecycleOwnerObserver = object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onAny() {
val newState = lifecycleOwner.lifecycle.currentState
when {
newState == Lifecycle.State.DESTROYED -> {
onDestroy()
lifecycleOwner.lifecycle.removeObserver(this)
}
newState.isAtLeast(Lifecycle.State.STARTED) -> onBecomeActive()
else -> onBecomeInactive()
}
}
}
init {
lifecycleOwner.lifecycle.addObserver(lifecycleOwnerObserver)
}
override fun shouldBeActive(): Boolean {
return lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
}
}
}
fun SKLiveData.map(transformation: (S) -> T): MutableSKLiveData =
MediatorSKLiveData(transformation(value)).apply {
addSource(this@map) {
postValue(transformation(it))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy