Created
February 24, 2021 12:09
-
-
Save TorkelV/42f886e4918aaef1ec7e1126640b5442 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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