Daniel Okanin
06/23/2022, 8:40 PMLaunchedEffect(shouldLoadMore) {
if (shouldLoadMore.value) {
lastTotalItems = state.layoutInfo.totalItemsCount
onEndReached()
}
}
=========================
LaunchedEffect(shouldLoadMore) {
snapshotFlow { shouldLoadMore.value }
.collect { shouldLoadMore ->
if (shouldLoadMore){
lastTotalItems = state.layoutInfo.totalItemsCount
onEndReached()
}
}
}
Sean McQuillan [G]
06/23/2022, 8:42 PMshouldLoadMore
appears to be MutableState, so it will never restart the LaunchedEffect in the first one
LaunchedEffect(shouldLoadMore.value)
is probably what you want thereshouldLoadMore.value
inside the coroutineDaniel Okanin
06/23/2022, 8:43 PMLaunchedEffect(shouldLoadMore.value) {
if (shouldLoadMore.value) {
lastTotalItems = state.layoutInfo.totalItemsCount
onEndReached()
}
}
Sean McQuillan [G]
06/23/2022, 8:43 PM.collect
won't cancel previous when new emit() in flow
LaunchedEffect(keyChange)
will cancel previous when key changesDaniel Okanin
06/23/2022, 8:44 PMSean McQuillan [G]
06/23/2022, 8:44 PMDaniel Okanin
06/23/2022, 8:45 PMSean McQuillan [G]
06/23/2022, 8:46 PMDaniel Okanin
06/23/2022, 8:52 PMvar lastTotalItems by remember { mutableStateOf(-1) }
val shouldLoadMore = remember {
derivedStateOf {
val totalItems = state.layoutInfo.totalItemsCount
val lastVisibleItem = state.layoutInfo.visibleItemsInfo.lastOrNull() ?: return@derivedStateOf false
(lastVisibleItem.index >= state.layoutInfo.totalItemsCount - 1 - buffer) && (lastTotalItems != totalItems)
}
}
LaunchedEffect(shouldLoadMore) {
snapshotFlow { shouldLoadMore.value }
.collect { shouldLoadMore ->
if (shouldLoadMore){
lastTotalItems = state.layoutInfo.totalItemsCount
onEndReached()
}
}
}
Sean McQuillan [G]
06/23/2022, 8:54 PMDaniel Okanin
06/23/2022, 8:55 PMSean McQuillan [G]
06/23/2022, 8:56 PMshouldLoadMore
(this appears derived as a pure function from scroll position and items) which creates an event onEndReached
onEndReached
then this problem becomes internal to that - and possibly outside of compositionDaniel Okanin
06/23/2022, 9:07 PMvar viewModelsItems: ArrayList<Any> =object : ArrayList<Any>() {
override fun get(index: Int): Any {
val res = super.get(index)
if (index + 20 >= size) {
loadMore()
}
return res
}
}
Sean McQuillan [G]
06/23/2022, 9:09 PMsnapshotFlow
A way I typically implement this is to keep track of the last viewed item in reactive state (mutableState, StateFlow, whatever) then if it's past some threshold start the next refresh in a loading loopDaniel Okanin
06/23/2022, 9:23 PMSean McQuillan [G]
06/23/2022, 9:37 PMDaniel Okanin
06/23/2022, 9:41 PM