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
Zun
06/11/2021, 4:11 PM
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)
}
Zun
06/11/2021, 4:12 PM
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
Se7eN
06/11/2021, 4:21 PM
Probably using a `List`:
Copy code
private val _whitelist = MutableStateFlow<List<ShowTag>>(value = listOf())
fun includeGenre(genre: ShowTag) {
_whitelist.value = _whitelist.value + genre
}