https://kotlinlang.org logo
#compose
Title
# compose
c

Chris Fillmore

01/21/2022, 12:01 AM
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

Adam Powell

01/21/2022, 12:50 AM
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

Chris Fillmore

01/21/2022, 2:44 PM
Ok thanks for the feedback Adam!
4 Views