Colton Idle
11/01/2022, 6:15 PMKevin Del Castillo
11/01/2022, 6:21 PMshikasd
11/01/2022, 6:23 PMLaunchedEffect(state) {
snapshotFlow {
val lastItem = state.layoutInfo.visibleItemsInfo.last()
lastItem.index == state.layoutInfo.itemCount - 1 && lastItem.offset + lastItem.size == state.layoutInfo.viewportEndOffset
}.collect { }
}
dorche
11/01/2022, 6:49 PMval isLastVisible by remember(vmState) {
derivedStateOf {
lazyListState.layoutInfo.visibleItemsInfo.any { it.key == LOADING_KEY }
}
}
LaunchedEffect(vmState, isLastVisible) {
if (isLastVisible) {
// action
}
}
vmState.data.forEach {
item(key = "${it.idFromNetwork}") {
Card(data = data, onClick = onClick)
}
}
if (!vmState.isLastPage) {
item(key = LOADING_KEY) {
LoadingCard()
}
}
Chris Johnson
11/01/2022, 7:20 PMLazyColumn(state = state, modifier = modifier) {
itemsIndexed(items) { index, item ->
// We do the check for pagination in here so that we're not recomposing the
// LazyColumn everytime checking for current index == item.size -1.
// This way we only check during composition of the items in the lazyColumn as
// they are composed.
if (index == items.size - 1 && hasNextPage() && !isLoadingNextPage()) {
loadNextPage()
}
content(index, item)
}
}
edit: forgive the formattingColton Idle
11/01/2022, 8:13 PMStylianos Gakis
11/01/2022, 8:15 PMloadNextPage()
in composition? Might be scary if isLoadingNextPage doesn't get the chance to return true if recomposition happens too fast.Chris Johnson
11/01/2022, 8:21 PMshikasd
11/01/2022, 9:54 PMStylianos Gakis
11/02/2022, 12:09 PMThat’s the only recomposition that happens on my pagination composable so it’s pretty safeJust gonna throw in there that this may stop being true if you add some sort of animation or something like that on your item. And it may come back to bite you in the future. Have you considered wrapping it in a LaunchedEffect with the necessary keys instead? And still have the same if check in there. Wouldn’t that be a better idea?
Chris Johnson
11/02/2022, 5:42 PM