Loading core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/mvi/UnidirectionalViewModel.kt +42 −0 Original line number Diff line number Diff line Loading @@ -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, ) } feature/account/server/validation/src/main/kotlin/app/k9mail/feature/account/server/validation/ui/ServerValidationMainScreen.kt +2 −3 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 = { Loading Loading
core/ui/compose/common/src/main/kotlin/app/k9mail/core/ui/compose/common/mvi/UnidirectionalViewModel.kt +42 −0 Original line number Diff line number Diff line Loading @@ -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, ) }
feature/account/server/validation/src/main/kotlin/app/k9mail/feature/account/server/validation/ui/ServerValidationMainScreen.kt +2 −3 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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 = { Loading