Kshitij Patil
02/14/2021, 8:42 AMJohn O'Reilly
02/14/2021, 9:26 AMKshitij Patil
02/14/2021, 9:39 AMTimo Drick
02/14/2021, 1:27 PMKshitij Patil
02/14/2021, 1:28 PMTimo Drick
02/14/2021, 1:30 PM/**
* Executes loadPage until result list is empty and emits the items as flow.
*/
fun <T>pagingFlow(firstPage: Int = 0, maxPages: Int = 100, init: (suspend FlowCollector<T>.() -> Unit)? = null, loadPage: suspend (Int) -> List<T>): Flow<T> {
var page = firstPage
return flow {
init?.invoke(this)
var emittedValues = true
while (page < maxPages && emittedValues) {
emittedValues = false
loadPage(page).forEach {
emit(it)
emittedValues = true
}
page++
}
}
}
Kshitij Patil
02/14/2021, 1:35 PMTimo Drick
02/14/2021, 1:41 PMval flowCollector = yourFlow.stateList()
//showing a lazy row
FlowRowFor(flow2stateList = flowCollector) { state ->
key(state) {
when (state) {
is FlowState.Loading -> LoadingBox()
is FlowState.Item -> // Your content view
is FlowState.Error -> ErrorBox()
is FlowState.Empty -> ErrorBox()
}
}
}
Kshitij Patil
02/14/2021, 1:43 PMTimo Drick
02/14/2021, 1:43 PM@Composable
fun <T> FlowColumnFor(
flow2StateList: Flow2StateList<T>,
...,
itemContent: @Composable LazyItemScope.(FlowState<T>) -> Unit
) {
LazyColumn(...) {
itemsIndexed(items = flow2StateList.list) { index, item ->
flow2StateList.requestListSize(index + 1)
itemContent(this, item)
}
}
}
So every time a element gets visible i request more items from the flow2StateList. You can also define a buffer to preload items before they get visible.