All Downloads are FREE. Search and download functionalities are using the official Maven repository.

androidMain.tech.skot.view.live.SKMessage.kt Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
package tech.skot.view.live

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent

class SKMessage(multiReceiver:Boolean = false) : SKMessageCommon(multiReceiver) {

    fun observe(lifecycleOwner: LifecycleOwner, onChanged: (d: D) -> Unit) {
        observers.add(LifecycleOwnerObserver(lifecycleOwner, onChanged))
    }

    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) {

        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)
        }

    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy