Use EventSink lambda in state instead of Flow in Presenter

This commit is contained in:
ganfra 2023-01-11 15:53:52 +01:00
parent e56ba5e315
commit ad7bf21f6d
43 changed files with 277 additions and 490 deletions

View file

@ -3,7 +3,7 @@ package io.element.android.x.architecture
import androidx.compose.runtime.Composable
import kotlinx.coroutines.flow.Flow
interface Presenter<State, Event> {
interface Presenter<State> {
@Composable
fun present(events: Flow<Event>): State
fun present(): State
}

View file

@ -8,19 +8,14 @@ import app.cash.molecule.launchMolecule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.StateFlow
inline fun <reified State, reified Event> LifecycleOwner.presenterConnector(presenter: Presenter<State, Event>): LifecyclePresenterConnector<State, Event> =
inline fun <reified State> LifecycleOwner.presenterConnector(presenter: Presenter<State>): LifecyclePresenterConnector<State> =
LifecyclePresenterConnector(lifecycleOwner = this, presenter = presenter)
class LifecyclePresenterConnector<State, Event>(lifecycleOwner: LifecycleOwner, presenter: Presenter<State, Event>) {
class LifecyclePresenterConnector<State>(lifecycleOwner: LifecycleOwner, presenter: Presenter<State>) {
private val moleculeScope = CoroutineScope(lifecycleOwner.lifecycleScope.coroutineContext + AndroidUiDispatcher.Main)
private val eventFlow = SharedFlowHolder<Event>()
val stateFlow: StateFlow<State> = moleculeScope.launchMolecule(RecompositionClock.Immediate) {
presenter.present(events = eventFlow.asSharedFlow())
}
fun emitEvent(event: Event) {
eventFlow.emit(event)
presenter.present()
}
}

View file

@ -9,4 +9,6 @@ class SharedFlowHolder<Data>(capacity: Int = 64) {
fun asSharedFlow() = mutableFlow.asSharedFlow()
fun emit(data: Data) = mutableFlow.tryEmit(data)
suspend fun awaitEmit(data: Data) = mutableFlow.emit(data)
}