I'm a little confused. I want to use a boolean val...
# compose-desktop
a
I'm a little confused. I want to use a boolean value from my model to show/hide some things on my UI, but it doesn't work if I have a TextField in my set of composables. The state won't change for any of the composables in the block
In psuedocode:
Copy code
if(model.shouldShow()) { // Doesn't show/hide
 Text("Title")
 TextField(...)
 Button(...)
}


Text("Title")
TextField(...)
if(model.shouldShow()) { // Does show/hide
 Button(...)
}
Actually, I guess I'm misunderstanding something. If there's something that triggers the TextField to update, then it will work, but otherwise it doesn't. I do understand that the boolean needs to be observable for the UI state to change, but I'm passing the boolean from a function, which is checking whether a list has > 0 items, and I'm not sure how I would make that observable.
t
Maybe with
derivedStateOf
(if your list is an observable state):
Copy code
val shouldShow by derivedStateOf { list.size > 0 }
z
Your list needs to either be an immutable list in a MutableState, or a SnapshotStateList/mutableStateListOf
You don't need to use derivedState just to propagate change notifications, although it will save unnecessary recompositions once the list is non-empty.
a
@Zach Klippenstein (he/him) [MOD] I've provided a more concrete example with two classes here: https://gist.github.com/aaronjyoder/5443f0da5765c1892fcbb199ff415737 and https://gist.github.com/aaronjyoder/c10c214f8c816fea28ba23fe6658b862
Those gists reflect the state of the code without the mentioned modifications above. I have tried derivedStateOf and tried using MutableState, though I don't see any changes to how things function by doing that.
z
can you share your code after making the changes?
a
Sure
I assume nothing needs to change in the other gist; I have other areas of my code where I've used mutableStateOf without issue so that's why I'm a little confused here; those other areas of code don't deal with lists/sets in this way though
z
You missed a word in my original suggestion: the lists need to be immutable if they’re in a
MutableState
MutableState
only sends change notifications when its reference changes, so if you’re mutating the same list then no change notifications get sent
a
Oh, so if I use that, I have to instantiate a new set every time its contents change?
z
Yes, or use a
mutableStateMapOf
if you don’t want to do that
a
Are there any other alternatives? This would require me to change quite a lot since I am constantly altering the contents of those sets
z
Not really. You either have to make the property itself a mutable state (or
StateFlow
, or
BehaviorSubject
, or etc) with an immutable collection, or use a collection type that is aware of the snapshot state system.
a
Alright thanks
z
It’s easy to treat a map like a set, just use
Unit
as the value type
👍 1
a
Just in case anyone else comes across this: perhaps this isn't the cleanest way of doing it, but I decided to use a boolean with
mutableStateOf
and observe that, and change the state of the boolean upon selection/deselection since that was the goal anyway (to make UI appear or disappear based on whether something is selected or not).