Olivier Patry
03/01/2021, 5:56 PM@Composable
fun MainLayout(catRepository: CatRepository = (CatRepository((FakeCatDataSource())))) {
val catsViewModel = viewModel<CatsViewModel>(factory = CatsViewModelFactory(catRepository))
Surface(color = MaterialTheme.colors.background) {
when {
!booleanResource(R.bool.is_tablet) -> MainLayoutTabletPhone(catsViewModel)
booleanResource(R.bool.is_portrait) -> MainLayoutTabletPortrait(catsViewModel)
else -> MainLayoutTabletLandscape(catsViewModel)
}
}
}
var selected by remmeber { mutableStateOf<MyModel>(null) }
mutableState
(currently stored in each individual composable), would you hoist it?var selectedCat by remember { mutableStateOf<CatModel?>(null) }
on parent @Composable
and giving the selected state and the selection callback, it works but configuration change doesn't restore the staterememberSaveable
seems to solve, is it the recommended solution?if (booleanResource(R.bool.is_tablet)) {
var selectedCatUUID by rememberSaveable { mutableStateOf<UUID?>(null) }
val selectedCat = selectedCatUUID?.let { uuid ->
catsViewModel.findCatByUUID(uuid)
}
if (booleanResource(R.bool.is_portrait)) {
MainLayoutTabletPortrait(catsViewModel, selectedCat) { cat ->
selectedCatUUID = cat.uuid
}
} else {
MainLayoutTabletLandscape(catsViewModel, selectedCat) { cat ->
selectedCatUUID = cat.uuid
}
}
} else {
MainLayoutTabletPhone(catsViewModel)
}