Add observeWithoutEffect method
This commit is contained in:
parent
11b12f9c5e
commit
25accaf905
2 changed files with 44 additions and 3 deletions
|
@ -102,3 +102,45 @@ inline fun <reified STATE, EVENT, EFFECT> UnidirectionalViewModel<STATE, EVENT,
|
||||||
dispatch = dispatch,
|
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,8 +2,8 @@ package app.k9mail.feature.account.server.validation.ui
|
||||||
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
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.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.designsystem.template.Scaffold
|
||||||
import app.k9mail.core.ui.compose.theme.K9Theme
|
import app.k9mail.core.ui.compose.theme.K9Theme
|
||||||
import app.k9mail.core.ui.compose.theme.ThunderbirdTheme
|
import app.k9mail.core.ui.compose.theme.ThunderbirdTheme
|
||||||
|
@ -21,8 +21,7 @@ internal fun ServerValidationMainScreen(
|
||||||
viewModel: ViewModel,
|
viewModel: ViewModel,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
val state = viewModel.state.collectAsStateWithLifecycle()
|
val (state, dispatch) = viewModel.observeWithoutEffect()
|
||||||
val dispatch = { event: Event -> viewModel.event(event) }
|
|
||||||
|
|
||||||
Scaffold(
|
Scaffold(
|
||||||
topBar = {
|
topBar = {
|
||||||
|
|
Loading…
Reference in a new issue