GOAL
To have one data class representing my whole screen state && utilizing smart recompositions.
EXAMPLE
Lets say I have a data class as follows:
data class MyDataClass(
val count : Int,
val text : String
)
And also two composable functions A and B, taking Int and String respectively:
@Composable
fun ParentComposable(){
val vm = anyWayYouLikeToGetYourVM()
val state = vm.testState.collectAsState()
SideEffect {
println("Naturally recomposes each time it reads the state")
}
A(count = state.value.count)
B(text = state.value.text)
}
@Composable
fun B(
text: String
){
SideEffect {
println("Will skip recomposition if value hasn't changed")
}
...
}
This example gives me a hope in smart recompositions, because it skips those out of the box, no dancing with derivative states, lambdas, etc. If data class contains stable fields – it works. Children are skipped even when their parent is recomposing.
PROBLEM
There is no way to make it work with the notorious lists. I can annotate my data class as @Stable or @Immutable, use persisten list from the immutable library, lambdas and derivative states. It keeps recomposing.
Since the example above does work I am really confused why this does not:
@Composable
fun ParentComposable(){
...
B(text = state.value.myPersistentList)
// B will recompose
}