Browse Source

Use LifecycleOwner instead of explicit Node on presenterConnector

feature/bma/flipper
ganfra 2 years ago
parent
commit
a37cf3adde
  1. 2
      features/roomlist/src/main/java/io/element/android/x/features/roomlist/RoomListNode.kt
  2. 10
      libraries/architecture/src/main/java/io/element/android/x/architecture/LifecyclePresenterConnector.kt

2
features/roomlist/src/main/java/io/element/android/x/features/roomlist/RoomListNode.kt

@ -31,7 +31,7 @@ class RoomListNode @AssistedInject constructor( @@ -31,7 +31,7 @@ class RoomListNode @AssistedInject constructor(
fun onRoomClicked(roomId: RoomId)
}
private val connector = presenterConnector(presenter)
private val connector by presenterConnector(presenter)
private fun updateFilter(filter: String) {
connector.emitEvent(RoomListEvents.UpdateFilter(filter))

10
libraries/architecture/src/main/java/io/element/android/x/architecture/NodePresenterConnector.kt → libraries/architecture/src/main/java/io/element/android/x/architecture/LifecyclePresenterConnector.kt

@ -1,21 +1,21 @@ @@ -1,21 +1,21 @@
package io.element.android.x.architecture
import androidx.lifecycle.LifecycleOwner
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)
inline fun <reified State, reified Event> LifecycleOwner.presenterConnector(presenter: Presenter<State, Event>): Lazy<LifecyclePresenterConnector<State, Event>> = lazy {
LifecyclePresenterConnector(lifecycleOwner = this, presenter = presenter)
}
class NodePresenterConnector<State, Event>(private val node: Node, presenter: Presenter<State, Event>) {
class LifecyclePresenterConnector<State, Event>(lifecycleOwner: LifecycleOwner, presenter: Presenter<State, Event>) {
private val moleculeScope = CoroutineScope(node.lifecycleScope.coroutineContext + AndroidUiDispatcher.Main)
private val moleculeScope = CoroutineScope(lifecycleOwner.lifecycleScope.coroutineContext + AndroidUiDispatcher.Main)
private val eventFlow: MutableSharedFlow<Event> = MutableSharedFlow(extraBufferCapacity = 64)
val stateFlow: StateFlow<State> = moleculeScope.launchMolecule(RecompositionClock.ContextClock) {
Loading…
Cancel
Save