Vivek Modi
06/26/2021, 10:04 PMGeorge Pandian
06/26/2021, 10:13 PMVivek Modi
06/26/2021, 10:44 PMGeorge Pandian
06/26/2021, 11:01 PMTimo Drick
06/26/2021, 11:06 PMTimo Drick
06/26/2021, 11:12 PMdata class Data(val data: String)
val dataFlow<Flow<Data>> = pagingFlow(firstPage = 1) { page ->
loadPageData() // must return a list of Data
}
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++
}
}
}
George Pandian
06/26/2021, 11:13 PMTimo Drick
06/26/2021, 11:14 PMGeorge Pandian
06/26/2021, 11:15 PMTimo Drick
06/26/2021, 11:15 PMGeorge Pandian
06/26/2021, 11:19 PMTimo Drick
06/26/2021, 11:20 PMgildor
06/28/2021, 3:26 AMAnd Flow is cold. So it will not start doing anything until you consume the flow.There are hot flows as well
gildor
06/28/2021, 3:27 AMgildor
06/28/2021, 3:32 AMVivek Modi
06/28/2021, 8:26 AM