Ryan Kay
10/16/2021, 2:42 PMRyan Kay
10/16/2021, 2:51 PMfun main() = application {
//...
var windowState by remember {
mutableStateOf(WindowState.VIEW_DAY)
}
//..
Window(onCloseRequest = ::exitApplication, title = "Todo", resizable = false) {
SamsaraTheme {
when (windowState) {
WindowState.VIEW_DAY -> {
val vm = DayViewModel()
val container = buildDayFeature(
{ newState: WindowState, arg: Int ->
windowState = newState
windowArgs = arg
},
vm,
storageService
)
DayViewScreen(
container.logic::onViewEvent,
vm
) //...
}}}}}
fun buildDayFeature(
stateHandler: (WindowState, Int) -> Unit,
vm: DayViewModel,
storageService: StorageService
): DayViewContainer =
DayViewContainer(stateHandler).start(storageService, vm)Adam Powell
10/16/2021, 4:04 PMremember {} something it will be hard referenced by the composition. Any lambda captures you create that Compose might need to re-run to recompose will also be hard referenced by the composition. These references are dropped when the @Composable caller leaves the composition, or when the composition itself is disposed.Adam Powell
10/16/2021, 4:09 PMval vm = DayViewModel() will create a new instance of DayViewModel each time the SamsaraTheme trailing lambda function recomposes and windowState == WindowState.VIEW_DAY. Generally when someone writes something called ViewModel this is not what they intend. 🙂 You might want to wrap a remember {} around that so that it keeps the same instance across recompositions.Adam Powell
10/16/2021, 4:09 PMval vm = remember { DayViewModel() }Ryan Kay
10/16/2021, 5:38 PM