Create Presentation module and remove Presenter from core

This commit is contained in:
ganfra 2023-01-04 12:11:12 +01:00
parent 969756e744
commit fc14973049
8 changed files with 22 additions and 7 deletions

1
libraries/presentation/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build

View file

@ -0,0 +1,14 @@
plugins {
id("io.element.android-library")
alias(libs.plugins.molecule)
}
android {
namespace = "io.element.android.x.libraries.presentation"
}
dependencies {
api(libs.dagger)
api(libs.appyx.core)
api(libs.androidx.lifecycle.runtime)
}

View file

@ -0,0 +1,28 @@
package io.element.android.x.presentation
import androidx.lifecycle.lifecycleScope
import app.cash.molecule.AndroidUiDispatcher
import app.cash.molecule.RecompositionClock
import app.cash.molecule.launchMolecule
import com.bumble.appyx.core.node.Node
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.StateFlow
inline fun <reified State, reified Event> Node.presenterConnector(presenter: Presenter<State, Event>): NodePresenterConnector<State, Event> {
return NodePresenterConnector(node = this, presenter = presenter)
}
class NodePresenterConnector<State, Event>(private val node: Node, presenter: Presenter<State, Event>) {
private val moleculeScope = CoroutineScope(node.lifecycleScope.coroutineContext + AndroidUiDispatcher.Main)
private val eventFlow: MutableSharedFlow<Event> = MutableSharedFlow(extraBufferCapacity = 64)
val stateFlow: StateFlow<State> = moleculeScope.launchMolecule(RecompositionClock.ContextClock) {
presenter.present(events = eventFlow)
}
fun emitEvent(event: Event) {
eventFlow.tryEmit(event)
}
}

View file

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