-
Notifications
You must be signed in to change notification settings - Fork 9
view
Florian Schuster edited this page Mar 23, 2025
·
11 revisions
the View
- binds the
Controller.stateto its components - binds interactions to the
Controller.dispatch(Action)function
in this example a literal view with a Button and a TextView is implemented. however that does not mean that only a view can have a Controller - there could also be a feature wide or a global Controller that controls the state of the corresponding system or app.
@Composable
fun ViewScreen(
valueController : Controller<Action, Mutation, State>
) {
val state by valueController.state.collectAsState()
View(state, valueController::dispatch)
}
@Composable
private fun View(
state: State
dispatch: (Action) -> Unit
) {
Column {
Button("Set Value") { dispatch(Action.SetValue(value = 3)) }
Text("value: ${state.counterValue}")
}
}on Android process death is a common thing. It is possible to get the latest state via Controller.state.value to save it or parts of it and later retrieve it again to set as Controller.initialState.
private const val counterValueKey = "value_ key"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val controller = scope.createController(
initialState = State(counterValue = savedInstanceState?.getInt(counterValueKey) ?: 0),
...
)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(counterValueKey, controller.state.value.counterValue)
}