Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 25accaf9 authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

Add observeWithoutEffect method

parent 11b12f9c
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -102,3 +102,45 @@ inline fun <reified STATE, EVENT, EFFECT> UnidirectionalViewModel<STATE, EVENT,
        dispatch = dispatch,
    )
}

/**
 * Composable function that observes a UnidirectionalViewModel without handling side effects.
 *
 * Example usage:
 * ```
 * @Composable
 * fun MyScreen(
 *   viewModel: MyUnidirectionalViewModel<MyState, MyEvent, MyEffect>,
 *   onNavigateNext: () -> Unit,
 *   onNavigateBack: () -> Unit,
 * ) {
 *   val (state, dispatch) = viewModel.observeWithoutEffect()
 *
 *   MyContent(
 *     onNextClick = {
 *       dispatch(MyEvent.OnNext)
 *     },
 *     onBackClick = {
 *       dispatch(MyEvent.OnBack)
 *     },
 *     state = state.value,
 *   )
 * }
 * ```
 *
 * @param STATE The type that represents the state of the ViewModel.
 * @param EVENT The type that represents user actions that can occur and should be handled by the ViewModel.
 *
 * @return A [StateDispatch] containing the state and a dispatch function.
 */
@Composable
inline fun <reified STATE, EVENT, EFFECT> UnidirectionalViewModel<STATE, EVENT, EFFECT>
    .observeWithoutEffect(): StateDispatch<STATE, EVENT> {
    val collectedState = state.collectAsStateWithLifecycle()
    val dispatch: (EVENT) -> Unit = { event(it) }

    return StateDispatch(
        state = collectedState,
        dispatch = dispatch,
    )
}
+2 −3
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@ package app.k9mail.feature.account.server.validation.ui

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import app.k9mail.core.ui.compose.common.DevicePreviews
import app.k9mail.core.ui.compose.common.mvi.observeWithoutEffect
import app.k9mail.core.ui.compose.designsystem.template.Scaffold
import app.k9mail.core.ui.compose.theme.K9Theme
import app.k9mail.core.ui.compose.theme.ThunderbirdTheme
@@ -21,8 +21,7 @@ internal fun ServerValidationMainScreen(
    viewModel: ViewModel,
    modifier: Modifier = Modifier,
) {
    val state = viewModel.state.collectAsStateWithLifecycle()
    val dispatch = { event: Event -> viewModel.event(event) }
    val (state, dispatch) = viewModel.observeWithoutEffect()

    Scaffold(
        topBar = {