Nicholas Doglio
09/16/2021, 12:52 PMSnapshotStateList
and it’s working fine but still new to Compose so was wondering if anyone had a better implementation.Ravi
09/16/2021, 1:04 PM@Composable
fun Pagination(
listState: LazyListState, buffer: Int = 5, action: () -> Unit
) {
val loadMore = remember {
derivedStateOf {
val layoutInfo = listState.layoutInfo
val totalItemsNumber = layoutInfo.totalItemsCount
val lastVisibleItemIndex = (layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0) + 1
lastVisibleItemIndex > (totalItemsNumber - buffer)
}
}
LaunchedEffect(loadMore) {
snapshotFlow { loadMore.value }
.distinctUntilChanged()
.collect {
if (it) {
action()
}
}
}
}
implemented pagination with this logic, didn’t write any JVM test cases till nowNicholas Doglio
09/16/2021, 7:37 PMPagination
function?
items
LazyColumn
function?LazyListState
is a parameter for LazyColumn
so this is all constructed above the LazyColumn
? Nice I think I like that better than what I have, what is derivedStateOf
needed for here?
This is my first attempt at paging and it seems to be working but maybe I should be using LaunchEffect
here 🤔
@Composable
fun PagingColumn(
data: SnapshotStateList<String>,
fetchIndex: Int,
fetchMoreContent: () -> Unit,
content: @Composable (String) -> Unit
) {
LazyColumn {
itemsIndexed(data) { index, item ->
if ((data.size - fetchIndex) == index) {
fetchMoreContent()
}
content(item)
}
}
}