Issue
I have a SnapshotStateMap that I use to track updates in my layout, this map is stored in a viewmodel.
This the site call:
val roundState = viewModel.roundState
for (index in 0 until attempts) {
val state = roundState[index] ?: WordState.Empty
Row {
RoundWord(state, letters)
}
}
In my program there are changes to only one item at the time, so basically my train of thought is: I add a new state or update the old in map -> I pass it to RoundWord -> If there is no state for index I pass in empty state -> RoundWord Composable relies on state to display the needed UI.
Here is the body of RoundWord
@Composable
private fun RoundWord(
state: WordState,
letters: Int,
) {
when (state) {
is WordState.Progress -> ProgressWord(letters)
is WordState.Empty -> WordCells(letters)
is WordState.Resolved -> WordCells(letters) { WiPLetter(state.value.word[it]) }
}
}
From what I understand if there is no state in roundState map for a given index I provide Empty state that is defined as an object in a sealed interface hierarchy. Same object -> no recomposition. But for some reason it recomposes every time. I have been at this for a few days now and despite going though tons of documentation I can't see what I am missing here. Why does this recomposition happens for empty state?
Solution
WordState sealed class hierarchy contained a property of type List<T>
. Compose does not consider List to be a stable type so the signature was actually something like unstable words: List<String>
.
Compose compiler cannot be completely sure that collections such as List, Map, and Set are truly immutable and therefore marks them as unstable
There are a few solutions for this problem
- It's possible to wrap List type in a
@Stable
annotated class. This should work as long as you adhere to@Stable
contract. - Use Kotlinx Immutable Collections (They look to be abandoned)
- Use
SnapshotStateList
that is considered stable
I think the best option is to provide a stability configuration file which makes it possible to annotate all Kotlin collections as stable. This works as long as you respect stability contract which just happens to basically be a set of good practices.
Answered By - Maksym Moroz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.