fix(couple): correct Firestore query and repository method signatures

This commit is contained in:
null 2026-06-16 00:40:00 -05:00
parent 6fe5e5048e
commit 29d512c679
3 changed files with 43 additions and 13 deletions

View File

@ -1,6 +1,7 @@
package com.couplesconnect.app.data.remote
import com.couplesconnect.app.domain.model.Couple
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.SetOptions
import kotlinx.coroutines.suspendCancellableCoroutine
@ -61,20 +62,44 @@ class FirestoreCoupleDataSource @Inject constructor() {
coupleRef(coupleId).get()
.addOnSuccessListener { snap ->
if (!snap.exists()) { cont.resume(null); return@addOnSuccessListener }
@Suppress("UNCHECKED_CAST")
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")
)
)
cont.resume(snap.toCouple())
}
.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
}
}

View File

@ -21,4 +21,8 @@ class CoupleRepositoryImpl @Inject constructor(
override suspend fun createCouple(inviterUserId: String, acceptorUserId: String, inviteCode: String): Result<String> = runCatching {
coupleDataSource.createCouple(inviterUserId, acceptorUserId, inviteCode)
}
override suspend fun updateStreak(coupleId: String): Result<Unit> = runCatching {
coupleDataSource.updateStreak(coupleId)
}
}

View File

@ -5,4 +5,5 @@ import com.couplesconnect.app.domain.model.Couple
interface CoupleRepository {
suspend fun getCoupleForUser(userId: String): Couple?
suspend fun createCouple(inviterUserId: String, acceptorUserId: String, inviteCode: String): Result<String>
suspend fun updateStreak(coupleId: String): Result<Unit>
}