Use EventSink lambda in state instead of Flow in Presenter
This commit is contained in:
parent
e56ba5e315
commit
ad7bf21f6d
43 changed files with 277 additions and 490 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue