I want to have a list in my ViewModel that tracks ...
# compose
z
I want to have a list in my ViewModel that tracks what checkboxes the user selected. I saw this post which shows my approach is wrong https://github.com/Kotlin/kotlinx.coroutines/issues/2516, but what would be the correct approach? See thread for more info
ViewModel:
Copy code
private val _whitelist = MutableStateFlow<MutableList<ShowTag>>(value = mutableListOf())
val whitelist: StateFlow<MutableList<ShowTag>> = _whitelist

fun includeGenre(genre: ShowTag) {
  _whitelist.value.add(genre)
}

fun removeGenre(genre: ShowTag) {
  _whitelist.value.remove(genre)
}
UI:
Copy code
val checked by exploreViewModel.whitelist.collectAsState()

// code removed for brevity
Checkbox(
  checked = checked.any { it.id == genre.id },
  onCheckedChange = onCheckedChange,
  colors = CheckboxDefaults.colors()
)
s
Probably using a `List`:
Copy code
private val _whitelist = MutableStateFlow<List<ShowTag>>(value = listOf())

fun includeGenre(genre: ShowTag) {
  _whitelist.value = _whitelist.value + genre
}
2