diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4a8ef50d..e16c76f6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,6 +24,7 @@ diff --git a/app/src/main/java/app/closer/data/repository/QuestionSessionRepositoryImpl.kt b/app/src/main/java/app/closer/data/repository/QuestionSessionRepositoryImpl.kt index b10ff5d3..c4985bf2 100644 --- a/app/src/main/java/app/closer/data/repository/QuestionSessionRepositoryImpl.kt +++ b/app/src/main/java/app/closer/data/repository/QuestionSessionRepositoryImpl.kt @@ -7,6 +7,7 @@ import app.closer.domain.model.QuestionSession import app.closer.domain.repository.QuestionSessionRepository import app.closer.domain.repository.SessionStartOutcome import com.google.firebase.firestore.FirebaseFirestore +import com.google.firebase.firestore.Source import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow @@ -156,14 +157,22 @@ class QuestionSessionRepositoryImpl @Inject constructor( } } + // Prefer a SERVER read so a session a partner JUST created — e.g. when we arrive from a + // "partner started a game" notification before our local cache has synced — is seen and + // joined, instead of dropping the user on the game's setup screen (or letting them start a + // duplicate). Fall back to CACHE when the server is unreachable (offline). override suspend fun getActiveSessionForCouple(coupleId: String): QuestionSession? = + fetchActiveSessionForCouple(coupleId, Source.SERVER) + ?: fetchActiveSessionForCouple(coupleId, Source.CACHE) + + private suspend fun fetchActiveSessionForCouple(coupleId: String, source: Source): QuestionSession? = runCatching { firestore.collection(FirestoreCollections.COUPLES) .document(coupleId) .collection(FirestoreCollections.Couples.SESSIONS) .whereEqualTo("status", "active") .limit(1) - .get() + .get(source) .await() .documents .firstOrNull()