Last active
July 3, 2021 09:01
-
-
Save manavtamboli/88cab6f402a7bbce1081ac6378332bd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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