commonMain.com.github.hadilq.androidlifecyclehandler.SLife.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-lifecycle-handler-metadata Show documentation
Show all versions of android-lifecycle-handler-metadata Show documentation
This library is a simplifier for unnecessary complex lifecycles of `androidx.lifecycle:lifecycle-extensions`.
The newest version!
package com.github.hadilq.androidlifecyclehandler
/**
* Defines a [Life] object that can **sync** other lives with the same lifecycle.
*/
abstract class SLife : Life {
private var isAlive = false
private val lives = mutableSetOf()
protected fun Life.sync(): Boolean = lives.add(this).also { if (isAlive) onBorn() }
/**
* It doesn't make sense to make [onBorn] and [onDie] methods thread-safe.
*/
override fun onBorn() {
if (!isAlive) {
isAlive = true
lives.forEach { it.onBorn() }
}
}
/**
* It doesn't make sense to make [onBorn] and [onDie] methods thread-safe.
*/
override fun onDie() {
if (isAlive) {
isAlive = false
lives.forEach { it.onDie() }
lives.clear()
}
}
}