Jan
05/25/2023, 11:58 AMJan
05/25/2023, 11:59 AMval shops by viewModel.homeFlow.collectAsState(listOf())
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
shops.forEach { (shop, products) ->
item {
Box(
modifier = Modifier
.padding(5.dp)
.fillMaxWidth()
.animateItemPlacement()
.clickable {
viewModel.changeShopVisibility(shop.id, !shop.isVisible)
},
contentAlignment = Alignment.Center
) {
Text(text = shop.name, fontSize = 20.sp, fontWeight = FontWeight.Bold)
IconButton(
onClick = {
viewModel.changeShopPinned(shop.id, !shop.isPinned)
},
modifier = Modifier.align(Alignment.CenterEnd)
) {
if(shop.isPinned) {
Icon(LocalIcon.BookmarkAdd, null)
} else {
Icon(LocalIcon.Bookmark, null, tint = MaterialTheme.colorScheme.surfaceTint)
}
}
}
}
if (shop.isVisible) {
items(products, { it.id }) {
ProductEntryCard(it, viewModel)
}
}
}
}
Jan
05/25/2023, 11:59 AMval homeFlow = shopFlow.combine(productEntryFlow) { shops, products ->
shops
.filter { // filter out shops that have no products
products.any { product -> product.shopId == it.id }
} // map the shops and products to a pair
.sortedBy { it.isPinned }
.map { it to products.filter { product -> product.shopId == it.id } }
}
.flowOn(Dispatchers.Default)
chanjungskim
05/25/2023, 12:00 PMitems
insteadJan
05/25/2023, 12:00 PMif (shop.isVisible) {
items(products, { it.id }) {
ProductEntryCard(it, viewModel)
}
}
)
And I didn't know how I'd do thatJan
05/25/2023, 12:01 PMSean Proctor
05/25/2023, 12:16 PMLazyColumn
. Does it lag when compiled for release? Compose is notorious for bad performance in debug builds. If it does, I would take a look at ProductEntryCard
to see if that's the culprit.Sean Proctor
05/25/2023, 12:18 PMJan
05/25/2023, 12:28 PMSergey Y.
05/25/2023, 1:03 PMSergey Y.
05/25/2023, 3:11 PMOdife K
05/25/2023, 6:11 PMProductEntryCard(it, viewModel)
ViewModel is not Stable. Do you really want to pass viewModel down the composable hierarchy? Try a pure data class and see if the performance improvesTimo Drick
05/26/2023, 7:31 AMitems
interface from LazyColumn not item
?
Maybe this could improve your performanceTimo Drick
05/26/2023, 7:36 AMLazyColumn() {
items(shops.entries) { (shop, products) ->
....
Jan
05/26/2023, 2:54 PMJan
05/26/2023, 2:56 PMitems
for shops instead of products (and use a loop for the products)
It's definitely smoother, but there are still some annoying lags.Jan
05/26/2023, 2:56 PMval shops by viewModel.homeFlow.collectAsState(listOf())
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
items(shops, { it.first.id }) { (shop, products) ->
Box(
modifier = Modifier
.padding(5.dp)
.fillMaxWidth()
.animateItemPlacement()
.clickable {
viewModel.changeShopVisibility(shop.id, !shop.isVisible)
},
contentAlignment = Alignment.Center
) {
Text(text = shop.name, fontSize = 20.sp, fontWeight = FontWeight.Bold)
IconButton(
onClick = {
viewModel.changeShopPinned(shop.id, !shop.isPinned)
},
modifier = Modifier.align(Alignment.CenterEnd)
) {
if (shop.isPinned) {
Icon(LocalIcon.BookmarkAdd, null)
} else {
Icon(
LocalIcon.Bookmark,
null,
tint = MaterialTheme.colorScheme.surfaceTint
)
}
}
}
Column {
products.forEach { product ->
ProductEntryCard(
product = product,
onDoneChange = {
if (it) {
viewModel.markEntryAsDone(product.id)
} else {
viewModel.markEntryAsNotDone(product.id)
}
}, onDelete = {
viewModel.deleteEntry(product.id)
}
)
}
}
}
}
Jan
05/26/2023, 2:59 PMJan
05/26/2023, 3:01 PMitems
and no loop. But I have no idea how I'd improve that.Sergey Y.
05/26/2023, 3:51 PMSergey Y.
05/26/2023, 3:52 PM