Hi, I'm completely new to android development, and...
# compose-android
b
Hi, I'm completely new to android development, and I haven't found a good answer in the docs for this: I have a list loaded from a DB repository in the viewModel as a StateFlow, and I want to add some UI state to it (i.e. a checkbox setting). Where do I add the "checked" state such that I don't have to store it in the DB, and I also don't have to redraw the entire list each time? Following the examples I have
Copy code
var checked by mutableStateOf(initialChecked)
as part of my UI object but my understanding is that will be entirely overwritten the moment the StateFlow is updated. Am I completely misusing some functionality here or utterly misunderstanding the StateFlow? or how should you generally go about doing something like this? I don't actually need this to be a flow at all just curious how/if something like this should be accomplished
a
If you have not already seen, check out SSOT (single source of truth) / UDF (unidirectional data flow) here https://developer.android.com/topic/architecture you could simply write to DB and let it circle back to the UI. If it’s a form that doesn’t get written until “submit” etc you would store that temp state in ViewModel typically, and your StateFlow is somehow combined() with db + viewmodel state. Don’t forget to put the checked status in SavedStateHandle. So the Screen StateFlow could be combine (DB + SavedStateHandle)
b
my DB doesn't have any need for a selected column (it's not for persistence), but my concern is if I update the single selected item to be selected, I'll need to emit an entire new event with the entire list for it to update. Or am I mistaken there? I can easily store a map of key -> object with the selected value and map them in the view model. just not sure that's efficient.
a
Emitting a new event should be OK. Go with that before it’s obvious there are some performance problems. Once you give it to your composition make sure only the parts of the composition that need data get it, so they don’t recompose unnecessarily.
b
thanks! I'm not really planning on using this implementation anyhow just trying to learn the best way to accomplish things, and all the examples had a static list.
👍 1