I’m trying to wrap my head around this. I have an ...
# compose
c
I’m trying to wrap my head around this. I have an observable come back with the data to make a Profile page, however I have to make a second call to display more data to the user afterwards. How could I react when the second set of data comes back and add that to the tree? Example:
Copy code
@Composable
Profile( username: String, name: String, location: String, activityTotals: List<ActivityTotals>? = null )
The
activityTotals
come back afterwards. Since
Profile
isn’t an object I can’t expose a property to update with the new value. Would I expose a state or pass in an observable or do something else?
a
Can you show the code that calls this so far?
c
Copy code
viewModel.detailedAthlete.observe(viewLifecycleOwner, Observer { athlete ->
    profilezone.setContent{      Profile(athlete.username, athlete.name, athlete.location))
}
Hadn’t gotten there yet, was trying to figure it out because I’m mixing compose in with a fragment that has other views. But I’m thinking it would be something like that.
There would be a second observable for the other call on the viewModel
a
Compose works a bit differently from that, a composable function doesn't return a
View
and may only be called by another composable function. Analogous code from a
@Composable
caller might look something like this:
Copy code
val athlete by viewModel.detailedAthlete.observeAsState()

Profile(athlete.username, athlete.name, athlete.location)
Compose will re-evaluate the function body when the input data changes, in this case when the state being observed changes.
c
I could add the Composable to a fragment with
ViewGroup.setViewContent
right? edit: I see it’s going away. Never mind.
a
You probably want the
ViewGroup.setContent
extension rather than
setViewContent
for now, but yes. You'll want to call that extension on a
FrameLayout
or similar since
ViewGroup.setContent
doesn't give you much control over the child
LayoutParams
or ordering
c
Thanks for the responses.
👍 1
a
The model is quite different at first but it starts paying off quickly 🙂