Skip to content

Instantly share code, notes, and snippets.

@TorkelV
Created February 24, 2021 12:09
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/42f886e4918aaef1ec7e1126640b5442 to your computer and use it in GitHub Desktop.
Save TorkelV/42f886e4918aaef1ec7e1126640b5442 to your computer and use it in GitHub Desktop.
override fun getLeaders(lastVisibleItem: Flow<Int>): Flow<List<User>> =
flow {
val users = mutableListOf<DocumentSnapshot>()
users.addAll(
suspendCoroutine<List<DocumentSnapshot>> { c ->
collection("users")
.whereGreaterThan("points", 0)
.orderBy("points", Query.Direction.DESCENDING)
.limit(25)
.get().addOnSuccessListener {
c.resume(it.documents)
}
}
)
emit(users.map { it.toUserDto().toUser() })
lastVisibleItem.transform { lastVisibleItem ->
if (lastVisibleItem == users.size) {
users.addAll(
suspendCoroutine<List<DocumentSnapshot>> { c ->
collection("users")
.whereGreaterThan("points", 0)
.orderBy("points", Query.Direction.DESCENDING)
.startAfter(users.last())
.limit(25)
.get().addOnSuccessListener {
c.resume(it.documents)
}
}
)
emit(users.map { it.toUserDto().toUser() })
}
}.collect { newUsers: List<User> ->
emit(newUsers)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment