Christopher Mederos
06/27/2023, 7:21 AMremember { mutableStateListOf("item 1", "item 2", "item 3") }
However - what is the best practice when you would like to store the list items in a view model? In my case, I'm trying to keep track of a list of things a user enters into a form and eventually uploading it to a server.yschimke
06/27/2023, 7:46 AMyschimke
06/27/2023, 7:47 AMMini
06/27/2023, 12:48 PMmutableStateListOf
in your ViewModel?Zach Klippenstein (he/him) [MOD]
06/28/2023, 5:08 AMChristopher Mederos
06/29/2023, 2:45 AMval uiState by myViewModel.collectAsState()
• My view model is supporting a long input form
• My state is held in the ViewModel as a private data class: val _uiState = MutableStateFlow(MyUiState())
• My state model has many fields that are related to a user recording information for a single report. Something like -
data class MyUiState constructor(
val firstName: String = "",
val lastName: String = "",
val hobbies: PersistentList<String> = persistentListOf()
)
• I'm updating state using MutableStateFlow.update() and copying state to emit new values
fun addHobby(newHobby: String) {
_uiState.update { it.copy(hobbies = it.hobbies.add(newHobby)) }
}
Colton Idle
06/29/2023, 9:00 PMZach Klippenstein (he/him) [MOD]
06/29/2023, 9:01 PMcollectAsState
is just produceState
, which is just mutableStateOf
with a LaunchedEffect