Hi again, So say I were to have a data structure ...
# compose-android
r
Hi again, So say I were to have a data structure like so:
Copy code
data class commentNode (
    var children: MutableList<CommentNode>? = mutableListOf(),
    var collapsed: Boolean = false
) {}
And all of this is contained in a variable that looks something like so:
Copy code
var tree: MutableList<CommentNode> = mutableListOf()
As you can imagine, this would produce a nested tree-like structure. So, using state, how would I trigger recomposition when changing the value
collapsed
in any nested
CommentNode
? Any help is appreciated :)
a
Do you want your
CommentNode
to implement
State
in a custom way and notify its observers about changes? I'd suggest a different approach and make your
CommentNode
immutable, and just pass it as a regular
@Stable
class. Then just pass it to a composable function, and it will recompose on any change. Of course, you'd have to "change" it using
copy()
r
That's an interesting idea, but if I change a nested comment node inside a mutable list using
copy()
, would that still cause the screen to recompose?
a
no, regular mutable collections aren't observable in Compose. You should use
ImmutableList<CommentNode>
if you take immutable approach, or
mutableStateListOf<CommentNode>()
if you insist on mutability
I would probably make it like this:
Copy code
data class CommentNode (
    val children: ImmutableList<CommentNode> = persistentListOf(),
    val collapsed: Boolean = false
)
1
r
Thank you so much! This works perfectly 😄 I've spent a while trying to get this working so thanks!
🙌 1