commonMain.com.paoapps.fifi.utils.ActionHandler.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fifi-framework Show documentation
Show all versions of fifi-framework Show documentation
Kotlin Multiplatform Mobile framework for optimal code sharing between iOS and Android.
package com.paoapps.fifi.utils
import com.paoapps.fifi.ui.component.ConfirmationDialogDefinition
import com.paoapps.fifi.utils.flow.FlowAdapter
import com.paoapps.fifi.utils.flow.wrap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
interface Emitter {
fun event(event: E)
fun action(action: A)
}
data class ActionHandler(val scope: CoroutineScope, private val handler: suspend (E, Any?) -> EventResult?) {
val globalActionsFlow = MutableSharedFlow()
val globalActions = globalActionsFlow.asSharedFlow().wrap(scope)
val actionsFlow = MutableSharedFlow()
val actions = FlowAdapter(scope, actionsFlow.asSharedFlow())
private val _links = MutableSharedFlow()
val links = _links.asSharedFlow().wrap(scope)
val events = MutableSharedFlow>()
private val _confirmationDialogs = MutableSharedFlow>()
val confirmationDialogs = _confirmationDialogs.asSharedFlow().wrap(scope)
sealed interface EventResult {
data class Event(val event: E): EventResult
data class Action(val action: A): EventResult
// data class Link(val link: String): EventResult
data class ConfirmationDialog(val confirmationDialog: ConfirmationDialogDefinition.Properties): EventResult
data class Global(val action: Any): EventResult
}
init {
scope.launch {
events.collect {
handleEvent(it.first, it.second)
}
}
}
suspend fun emitEvent(event: E) {
events.emit(Pair(event, null))
}
fun
© 2015 - 2025 Weber Informatics LLC | Privacy Policy