Skip to content

Instantly share code, notes, and snippets.

@TorkelV
Created February 24, 2021 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TorkelV/cde5137608c87f2261bade3a61cd8e88 to your computer and use it in GitHub Desktop.
Save TorkelV/cde5137608c87f2261bade3a61cd8e88 to your computer and use it in GitHub Desktop.
private fun Query.paginate(lastVisibleItem: Flow<Int>): Flow<List<DocumentSnapshot>> = flow {
val documents = mutableListOf<DocumentSnapshot>()
documents.addAll(
suspendCoroutine { c ->
this@paginate.limit(25).get().addOnSuccessListener { c.resume(it.documents) }
}
)
emit(documents)
lastVisibleItem.transform { lastVisible ->
if (lastVisible == documents.size && documents.size > 0) {
documents.addAll(
suspendCoroutine { c ->
this@paginate.startAfter(documents.last())
.limit(25)
.get()
.addOnSuccessListener {
c.resume(it.documents)
}
}
)
emit(documents)
}
}.collect { docs ->
emit(docs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment