agrosner
03/16/2023, 4:58 PMLazyVerticalGrid
with pagination detection (code in 🧵 ).
when using this extension on the lazy grid state, the composable begins to misbehave and continously rerender (non-stop, in the thousands over time). It seems that invalidations always go through the lazygrid measure result class. why would measurement change here, given we’re deriving state and remembering the value.agrosner
03/16/2023, 4:59 PM@Composable
private fun LazyGridState.onBottomReached(
// tells how many items before we reach the bottom of the list
// to call onLoadMore function
buffer: Int = 0,
loadMore: () -> Unit
) = apply {
// state object which tells us if we should load more
val shouldLoadMoreBuffer = remember(layoutInfo.totalItemsCount) {
derivedStateOf {
// get last visible item
val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull() ?: return@derivedStateOf false
// Check if last visible item is the last item in the list
val bufferIndex = layoutInfo.totalItemsCount - 1 - buffer
lastVisibleItem.index >= bufferIndex
}
}
LaunchedEffect(shouldLoadMoreBuffer) {
snapshotFlow { shouldLoadMoreBuffer.value }
.filter { it }
.collect { loadMore() }
}
}
agrosner
03/16/2023, 5:03 PMagrosner
03/16/2023, 5:39 PM