Screen Model and ViewModel
Modo provides support for Android ViewModel and also includes its own ScreenModel
.
ScreenModel
ScreenModel
is similar to ViewModel
. It is an interface that has an onDispose
function, which is called when the ScreenModel
needs to be cleared.
To use it inside a screen, call rememberScreenModel
inside the Content
function:
@Parcelize
class ScreenModelSampleScreen(
override val screenKey: ScreenKey = generateScreenKey()
) : Screen {
@Composable
override fun Content(modifier: Modifier) {
val screenModel = rememberScreenModel {
SampleScreenModel()
}
Column(Modifier.fillMaxSize()) {
Text(text = screenModel.state.intValue.toString())
}
}
}
private class SampleScreenModel : ScreenModel {
val state = mutableIntStateOf(0)
init {
coroutineScope.launch {
while (isActive) {
delay(COUNTER_DELAY_MS)
state.intValue++
}
}
}
}
ViewModel
You can use ViewModel
from Android Jetpack in Modo:
Last modified: 12 June 2024