When It Comes To State Management Stick To A Single Source Of Truth As Much As Possible Jetpack Compose
Published:
Right,
- When it’s
arrayList
, have some confidence that the key in theLaunchedEffect
,arrayList
itself should be enough - When it comes to nested array, or anything complex that requires a reference/associate, have a look at tuples (pair),
associateBy()
orassociate()
, mix it withMutableStateList
works well. - Avoid using
SideEffects
in the loop if it’s possible, it greatly affects the performance, still if you have to, don’t make a higher time complexity code - Also,
set()
function of anarrayList
or just directly useyourList[index]
works well triggeringSideEffects
of Jetpack Compose, no need toremove
thenadd
- Like you read that title, once again I’m saying, stick it to one reference, if you placed in a
viewModel
, just refer to it only. it won’t work if you try to manipulate a list that makes it has a new reference to a variable again with desired criteria (filtered, sublisted, etc.), but for helpers (just for read-only variable, OK) - Wrap Using
SideEffects
if it’s a direct parent of an element, say, you want to make a trigger after doing action on an element of the array, then wrap it using that direct array
Example
val isMonthlyDataCheckedList = mutableStateListOf<Pair<Boolean, SnapshotStateList<Pair<Boolean, Boolean>>>>()
The LaunchedEffect
will be
if ( viewModel.isMonthlyDataCheckedList.any { monthlyData -> monthlyData.second.any { it.second } }) {
LaunchedEffect(
viewModel.isMonthlyDataCheckedList.any { dailyData -> dailyData.second.any { it.second } }
) {
//yourCode
}
}