Best way for a "listener" when you hit the bottom ...
# compose
c
Best way for a "listener" when you hit the bottom of a list? Does this seem fine? https://stackoverflow.com/questions/68924018/jetpack-compose-scroll-to-bottom-listener-end-of-list
Copy code
LazyColumn(modifier = Modifier.fillMaxSize()) {
    items(someItemList) { item ->
        MyItem(item = item)
    }
    item {
        LaunchedEffect(true) {
            //Do something when List end has been reached
        }
    }
}
👍 1
a
snapshotFlow { !lazyListState.canScrollForward }
?
s
This is what I do to request more items when the the sixth-to-last (or down) item is displayed
Copy code
LaunchedEffect(lazyListState) {
        snapshotFlow { lazyListState.layoutInfo.visibleItemsInfo.lastOrNull()?.index }
            .filterNotNull()
            .map { it >= lazyListState.layoutInfo.totalItemsCount - 6 }
            .distinctUntilChanged()
            .filter { it && page.hasNext }
            .mapNotNull { page.mergeWithNext() }
            .collect { page = it }
    }
y
why do you use snapshotFlow when you can only use the launchedEffect?
s
Restarting a LaunchedEffect means you're cancelling a whole coroutine and starting a new one (on the next frame after the change has happened). With snapshotFlow you keep the coroutine running and just react to new states in your flow will receive. Definitely a better alternative.
y
but doesn't the LaunchedEffect automatically restart if its key is lazyListState, so when ever this key would change the entire block will be reset including the snapshotFlow
s
If the instance of the lazyListState changes sure. But that won't change under this normal operation. The instance you get back from rememberLazyListState will stay the same.
y
the instance itself wont change i am with you on that. but the lazyListState itself contains different states, for example the scroll state, any change of those states will trigger the launched effect to reset