https://kotlinlang.org logo
#compose
Title
# compose
a

agrosner

03/16/2023, 4:58 PM
hey we have a
LazyVerticalGrid
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.
Copy code
@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() }
    }
}
moved it into a snapshotflow seems to have fixed it.
94 Views