Does it make sense to back a toggleable component ...
# compose
c
Does it make sense to back a toggleable component (Switch/Checkbox) using a
StateFlow<Boolean>
? Even if there are many such components in a list? (dozens? hundreds?)
Supposing I have such a flow attached to some data, like:
Copy code
data class MyToggleableData(
  val data: MyData,
  val toggle: MutableStateFlow<Boolean>,
)
I wonder if it makes more sense to just store a Boolean, and update the entire List when there is a state change, like:
Copy code
val myData: MutableStateFlow<List<Pair<MyData, Boolean>>> = ...

fun toggle(myDataItem: MyData, newState: Boolean) {
  myData.update { list ->
    list.map {
      if (it == myDataItem) it.copy(toggle = newState)
    }
  }
}
Roughly like that
Any advice? Thanks!
a
you could, but if you're going to make individual objects observable+mutable I think you'd have a better time writing that as
Copy code
class MyToggleableData(
  val data: MyData,
  toggle: Boolean
) {
  var toggle by mutableStateOf(toggle)
}
2
c
Ok thanks for the feedback Adam!