Skip to content
Snippets Groups Projects
Commit 2d88cb81 authored by Fodor Patrik's avatar Fodor Patrik :ghost:
Browse files

fix functions, change types

parent 5b6d2518
No related branches found
No related tags found
No related merge requests found
......@@ -24,48 +24,45 @@ fun <T> getListOfData(
queryTransform: (CollectionReference) -> Query = { it },
): Flow<List<T>> = callbackFlow {
val coll = queryTransform(db.collection(collection))
val reg = coll.addSnapshotListener { conversations, error ->
val reg = coll.addSnapshotListener { datas, error ->
if (error != null) {
cancel("Firebase error", error)
return@addSnapshotListener
}
launch {
val ret = map(conversations!!)
val ret = map(datas!!)
trySend(ret)
}
}
awaitClose { reg.remove() }
}
val likeableUsers
get(): Flow<List<User>> = getListOfData(
//var likeableUsers get() = getLikeableUsers()
fun getLikeableUsers(
userList: List<String>,
limit: Long
): Flow<List<User>> = getListOfData(
collection = "users",
map = { datas ->
datas.map { data ->
User(
id = data.id,
name = data.get("name") as String,
description = data.get("description") as String,
preferences = data.get("preferences") as Preferences,
dismissedUsers = data.get("dismissedUsers") as List<String>,
likedUsers = data.get("likedUsers") as List<String>,
contacts = data.get("contacts") as String,
)
map = { it.toObjects(User::class.java) },
queryTransform = {
var q = it.limit(limit)
q = if (userList.isNotEmpty()) q.whereNotIn("id", userList) else q
q
}
},
)
suspend fun getNextLikeableUser(likedUserList: List<String>): User? =
suspend fun geLimitedLikeableUsers(
userList: List<String>,
limit: Long
): List<User> =
suspendCoroutine { continuation ->
db
.collection("users")
.whereNotIn("id", likedUserList)
.limit(1)
.get()
.addOnSuccessListener {
var q = db.collection("users").limit(limit)
q = if (userList.isNotEmpty()) q.whereNotIn("id", userList) else q
q.get().addOnSuccessListener {
continuation.resume(
if (it.size() > 0) it.toObjects(User::class.java)[0] else null
if (it.size() > 0) it.toObjects(User::class.java) else listOf()
)
}
.addOnFailureListener {
......@@ -86,6 +83,7 @@ val userData
launch {
val ret = data?.toObject(User::class.java)
?.copy(id = data.id)
if (ret != null) {
trySend(ret)
}
......@@ -115,26 +113,13 @@ val matchedUsersData
private fun getMatchedUsers(): Flow<List<User>> {
var user: User?
runBlocking {
user = getUserById(firebaseUser!!.uid)
val user: User? = runBlocking {
getUserById(firebaseUser!!.uid)
}
return getListOfData(
collection = "users",
map = { datas ->
datas.map { data ->
User(
id = data.id,
name = data.get("name") as String,
description = data.get("description") as String,
preferences = data.get("preferences") as Preferences,
dismissedUsers = data.get("dismissedUsers") as List<String>,
likedUsers = data.get("likedUsers") as List<String>,
contacts = data.get("contacts") as String,
)
}
},
map = { it.toObjects(User::class.java) },
queryTransform = {
var q = it.whereArrayContains("likedUsers", firebaseUser!!.uid)
if (user!!.likedUsers.isNotEmpty()) {
......
......@@ -7,8 +7,8 @@ data class User(
val name: String = "",
val description: String = "",
val preferences: Preferences = Preferences(),
val dismissedUsers: List<String> = listOf(),
val likedUsers: List<String> = listOf(),
var dismissedUsers: List<String> = listOf(),
var likedUsers: List<String> = listOf(),
val contacts: String = "",
): Serializable
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment