Hi all, very new to compose ui. I have a list of u...
# compose
j
Hi all, very new to compose ui. I have a list of users displayed, with the option of expanding each user to display things like the user's nickname, status, etc. This data only needs to be updated from backend when it needs to be displayed on user click, so does it make the most sense to just call a suspending function every time it needs to be displayed, or use some sort of flow? Thanks
l
Why do you say it only needs to be updated from the backend when it needs to be displayed? Why can’t you just include that data in the state you are passing to the Ui for building the collapsed state?
Copy code
class ViewModel() {
   val screenDataListFlow: Flow<ScreenData> = ....
}

data class ScreenData(
    val userDataList: List<UserData>,
    ...
)

data class UserData(
    val username: String,
    val nickname: String,
    val status: Status,
    ...
)
and then in the UI:
Copy code
@Composable
fun Screen(vm: ViewModel) {
    val state by vm.screenDataListFlow.collectAsState(...)

    ...
}
j
Right, my backend is able to provide a
screenDataListFlow
. However, I would only ever display one of those
UserData
at a time, so would it be a waste to collect the entire
List<UserData>
as state? Alternatively I could just fetch
UserData
from local data store when I need to display it
From what I understand, flows and StateFlows specifically are designed for data that needs to be continuously updated from the backend, such as a list of users that would be displayed at all times on a main screen
l
This is true, however the Android OS is very powerful and it may not be necessary to fetch such simple data dynamically.
Flows allow for continuously updated data this is true. so your
UserData
implementation could look like this:
Copy code
sealed class UserData {
data class UserData(
    val username: String,
    val nickname: String,
    val status: Status,
    ...
)
If you really want to dnyamically load I would consider giving the expanded portion of the component its own view model
j
Okay great, the expensiveness of flows is what I was unsure about, thanks