commonMain.com.arkivanov.decompose.extensions.compose.experimental.BroadcastBackHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extensions-compose-experimental-jvm Show documentation
Show all versions of extensions-compose-experimental-jvm Show documentation
Kotlin Multiplatform lifecycle-aware business logic components
package com.arkivanov.decompose.extensions.compose.experimental
import com.arkivanov.essenty.backhandler.BackCallback
import com.arkivanov.essenty.backhandler.BackEvent
import com.arkivanov.essenty.backhandler.BackHandler
internal class BroadcastBackHandler(
private val delegate: BackHandler,
) : BackCallback(), BackHandler {
private var callbacks = emptyList()
override fun onBackStarted(backEvent: BackEvent) {
callbacks.forEach { it.onBackStarted(backEvent) }
}
override fun onBackProgressed(backEvent: BackEvent) {
callbacks.forEach { it.onBackProgressed(backEvent) }
}
override fun onBackCancelled() {
callbacks.forEach(BackCallback::onBackCancelled)
}
override fun onBack() {
callbacks.forEach(BackCallback::onBack)
}
override fun isRegistered(callback: BackCallback): Boolean =
callback in callbacks
override fun register(callback: BackCallback) {
check(callback !in callbacks)
callbacks += callback
callbacks = callbacks.sortedByDescending(BackCallback::priority)
priority = callback.priority
if (callbacks.size == 1) {
delegate.register(this)
}
}
override fun unregister(callback: BackCallback) {
check(callback in callbacks)
callbacks -= callback
if (callbacks.isEmpty()) {
delegate.unregister(this)
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy