Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save manavtamboli/88cab6f402a7bbce1081ac6378332bd6 to your computer and use it in GitHub Desktop.

Select an option

Save manavtamboli/88cab6f402a7bbce1081ac6378332bd6 to your computer and use it in GitHub Desktop.
@Composable
fun LazyListState.OnBottomReached(
loadMore : () -> Unit
){
val shouldLoadMore = remember {
derivedStateOf {
val lastVisibleItem = layoutInfo.visibleItemsInfo.lastOrNull()
?: return@derivedStateOf true
lastVisibleItem.index == layoutInfo.totalItemsCount - 1
}
}
// Convert the state into a cold flow and collect
LaunchedEffect(shouldLoadMore){
snapshotFlow { shouldLoadMore.value }
.collect {
// if should load more, then invoke loadMore
if (it) loadMore()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment