fix(couple): correct Firestore query and repository method signatures
This commit is contained in:
parent
6fe5e5048e
commit
29d512c679
|
|
@ -1,6 +1,7 @@
|
||||||
package com.couplesconnect.app.data.remote
|
package com.couplesconnect.app.data.remote
|
||||||
|
|
||||||
import com.couplesconnect.app.domain.model.Couple
|
import com.couplesconnect.app.domain.model.Couple
|
||||||
|
import com.google.firebase.firestore.DocumentSnapshot
|
||||||
import com.google.firebase.firestore.FirebaseFirestore
|
import com.google.firebase.firestore.FirebaseFirestore
|
||||||
import com.google.firebase.firestore.SetOptions
|
import com.google.firebase.firestore.SetOptions
|
||||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||||
|
|
@ -61,20 +62,44 @@ class FirestoreCoupleDataSource @Inject constructor() {
|
||||||
coupleRef(coupleId).get()
|
coupleRef(coupleId).get()
|
||||||
.addOnSuccessListener { snap ->
|
.addOnSuccessListener { snap ->
|
||||||
if (!snap.exists()) { cont.resume(null); return@addOnSuccessListener }
|
if (!snap.exists()) { cont.resume(null); return@addOnSuccessListener }
|
||||||
@Suppress("UNCHECKED_CAST")
|
cont.resume(snap.toCouple())
|
||||||
cont.resume(
|
|
||||||
Couple(
|
|
||||||
id = snap.id,
|
|
||||||
userIds = (snap.get("userIds") as? List<String>) ?: emptyList(),
|
|
||||||
inviteCode = snap.getString("inviteCode") ?: "",
|
|
||||||
createdAt = snap.getLong("createdAt") ?: 0L,
|
|
||||||
currentQuestionId = snap.getString("currentQuestionId"),
|
|
||||||
streakCount = (snap.getLong("streakCount") ?: 0L).toInt(),
|
|
||||||
lastAnsweredAt = snap.getLong("lastAnsweredAt"),
|
|
||||||
activePackId = snap.getString("activePackId")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
.addOnFailureListener { cont.resumeWithException(it) }
|
.addOnFailureListener { cont.resumeWithException(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun updateStreak(coupleId: String) {
|
||||||
|
val snap = suspendCancellableCoroutine<DocumentSnapshot> { cont ->
|
||||||
|
coupleRef(coupleId).get()
|
||||||
|
.addOnSuccessListener { cont.resume(it) }
|
||||||
|
.addOnFailureListener { cont.resumeWithException(it) }
|
||||||
|
}
|
||||||
|
val lastAnsweredAt = snap.getLong("lastAnsweredAt") ?: 0L
|
||||||
|
val now = System.currentTimeMillis()
|
||||||
|
val current = (snap.getLong("streakCount") ?: 0L).toInt()
|
||||||
|
val newStreak = if (lastAnsweredAt == 0L || now - lastAnsweredAt > TWO_DAYS_MS) 1 else current + 1
|
||||||
|
suspendCancellableCoroutine<Unit> { cont ->
|
||||||
|
coupleRef(coupleId).set(
|
||||||
|
mapOf("streakCount" to newStreak, "lastAnsweredAt" to now),
|
||||||
|
SetOptions.merge()
|
||||||
|
)
|
||||||
|
.addOnSuccessListener { cont.resume(Unit) }
|
||||||
|
.addOnFailureListener { cont.resumeWithException(it) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
private fun DocumentSnapshot.toCouple() = Couple(
|
||||||
|
id = id,
|
||||||
|
userIds = (get("userIds") as? List<String>) ?: emptyList(),
|
||||||
|
inviteCode = getString("inviteCode") ?: "",
|
||||||
|
createdAt = getLong("createdAt") ?: 0L,
|
||||||
|
currentQuestionId = getString("currentQuestionId"),
|
||||||
|
streakCount = (getLong("streakCount") ?: 0L).toInt(),
|
||||||
|
lastAnsweredAt = getLong("lastAnsweredAt"),
|
||||||
|
activePackId = getString("activePackId")
|
||||||
|
)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TWO_DAYS_MS = 48L * 60 * 60 * 1000
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,8 @@ class CoupleRepositoryImpl @Inject constructor(
|
||||||
override suspend fun createCouple(inviterUserId: String, acceptorUserId: String, inviteCode: String): Result<String> = runCatching {
|
override suspend fun createCouple(inviterUserId: String, acceptorUserId: String, inviteCode: String): Result<String> = runCatching {
|
||||||
coupleDataSource.createCouple(inviterUserId, acceptorUserId, inviteCode)
|
coupleDataSource.createCouple(inviterUserId, acceptorUserId, inviteCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun updateStreak(coupleId: String): Result<Unit> = runCatching {
|
||||||
|
coupleDataSource.updateStreak(coupleId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,5 @@ import com.couplesconnect.app.domain.model.Couple
|
||||||
interface CoupleRepository {
|
interface CoupleRepository {
|
||||||
suspend fun getCoupleForUser(userId: String): Couple?
|
suspend fun getCoupleForUser(userId: String): Couple?
|
||||||
suspend fun createCouple(inviterUserId: String, acceptorUserId: String, inviteCode: String): Result<String>
|
suspend fun createCouple(inviterUserId: String, acceptorUserId: String, inviteCode: String): Result<String>
|
||||||
|
suspend fun updateStreak(coupleId: String): Result<Unit>
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue