@Composable
private fun PeopleScreen() {
val viewModel: PeopleViewModel2 = viewModel()
val viewState = viewModel.peopleList.collectAsState(initial = emptyList())
val listItems: List<PeopleItem> by viewState
LazyColumn(
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 16.dp),
modifier = Modifier.padding(bottom = 56.dp)
.fillMaxWidth()
) {
for (item in listItem) {
item(key = item.id) {
PeopleItem(
item = item,
onFavouriteClicked = { viewModel.onFavouriteClicked(item) }
)
Spacer(modifier = Modifier.padding(bottom = 8.dp))
}
}
}
}
t
Timo Drick
03/05/2021, 4:04 PM
To make the composable more reusable you could provide the peopleList and a callback for the favoriteClick as a parameter.
Copy code
private fun PeopleScreen(peopleList: Flow, favouriteClicked: (PeopleItem) -> Unit)
But of course it is your decision.
m
myanmarking
03/12/2021, 10:13 AM
no. This composable represents a screen, so it is the top level composable. The clicks are redirected to the viewModel because there is no composable higher, as i think it should be