Skip to content

Commit

Permalink
Remove nullable view models from view interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dellisd committed Mar 15, 2024
1 parent 703bd3f commit 6a942e7
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ca.derekellis.reroute.ui.View

class HomeView : View<Unit, Unit> {
@Composable
override fun Content(model: Unit?, emit: (Unit) -> Unit) {
override fun Content(model: Unit, emit: (Unit) -> Unit) {
/* No-op */
}
}
4 changes: 1 addition & 3 deletions web/src/jsMain/kotlin/ca/derekellis/reroute/map/MapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import org.jetbrains.compose.web.dom.Div
@Inject
class MapView : View<MapViewModel, MapViewEvent> {
@Composable
override fun Content(model: MapViewModel?, emit: (MapViewEvent) -> Unit) {
model ?: return

override fun Content(model: MapViewModel, emit: (MapViewEvent) -> Unit) {
val mapState = rememberMapboxState(center = LngLat(-75.7181, 45.3922), zoom = 11.0)

LaunchedEffect(model.targetStop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ import org.jetbrains.compose.web.dom.Input

class SearchView : View<SearchViewModel, SearchViewEvent> {
@Composable
override fun Content(model: SearchViewModel?, emit: (SearchViewEvent) -> Unit) {
model ?: return

override fun Content(model: SearchViewModel, emit: (SearchViewEvent) -> Unit) {
var isFocused by remember { mutableStateOf(false) }

Style(SearchStyleSheet)
Expand Down
6 changes: 2 additions & 4 deletions web/src/jsMain/kotlin/ca/derekellis/reroute/stops/StopView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import org.jetbrains.compose.web.dom.Text

class StopView : View<StopViewModel, StopViewEvent> {
@Composable
override fun Content(model: StopViewModel?, emit: (StopViewEvent) -> Unit) {
model ?: return

override fun Content(model: StopViewModel, emit: (StopViewEvent) -> Unit) {
Style(StopsStyleSheet)
InfoPanel(onClose = { emit(Close) }) {
StopContent(model)
Expand All @@ -31,7 +29,7 @@ private fun StopContent(model: StopViewModel) {
when (model) {
is StopViewModel.Loaded -> {
H1 { Text(model.stop.name) }
H2 { Text(model.stop.code ?: "") }
H2 { Text(model.stop.code) }
Div { Text(model.stop.id) }
Hr()
model.groupedRoutes.forEach { routes -> RouteInfo(routes) }
Expand Down
27 changes: 8 additions & 19 deletions web/src/jsMain/kotlin/ca/derekellis/reroute/ui/ScreenWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
package ca.derekellis.reroute.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import app.cash.molecule.RecompositionClock
import app.cash.molecule.moleculeFlow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filterNotNull
import androidx.compose.runtime.remember
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import me.tatarka.inject.annotations.Inject

@Inject
class ScreenWrapper(
private val presenter: Presenter<Any, Any>,
private val view: View<Any, Any>,
) {
private val eventsChannel = Channel<Any?>()
private val events = eventsChannel.consumeAsFlow().filterNotNull()

private val models = moleculeFlow(RecompositionClock.ContextClock) { presenter.produceModel(events) }
private var model by mutableStateOf<Any?>(null)

@Composable
fun screen() {
LaunchedEffect(presenter) {
models.collect { model = it }
}
val triggerFlow = remember { MutableSharedFlow<Any>(replay = 1) }
val listenerFlow = remember { triggerFlow.asSharedFlow() }

val model = presenter.produceModel(listenerFlow)

view.Content(model) { eventsChannel.trySend(it) }
view.Content(model) { triggerFlow.tryEmit(it) }
}

override fun equals(other: Any?): Boolean {
Expand Down
2 changes: 1 addition & 1 deletion web/src/jsMain/kotlin/ca/derekellis/reroute/ui/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import androidx.compose.runtime.Composable

interface View<ViewModel, ViewEvent> {
@Composable
fun Content(model: ViewModel?, emit: (ViewEvent) -> Unit)
fun Content(model: ViewModel, emit: (ViewEvent) -> Unit)
}

0 comments on commit 6a942e7

Please sign in to comment.