11package com.advice.firebase.session
22
3+ import android.app.Activity
34import com.advice.core.audience.AudienceContext
45import com.advice.core.local.Conference
56import com.advice.core.local.FlowResult
@@ -9,6 +10,7 @@ import com.advice.data.sources.ConferencesDataSource
910import com.advice.play.AgeSignalsRepository
1011import com.google.firebase.auth.FirebaseAuth
1112import com.google.firebase.crashlytics.FirebaseCrashlytics
13+ import kotlinx.coroutines.CompletableDeferred
1214import kotlinx.coroutines.CoroutineScope
1315import kotlinx.coroutines.flow.Flow
1416import kotlinx.coroutines.flow.MutableStateFlow
@@ -33,6 +35,9 @@ class FirebaseUserSession(
3335 private val _conferenceFlow : MutableStateFlow <FlowResult <Conference >> =
3436 MutableStateFlow (FlowResult .Loading )
3537
38+ /* * Completes true once anonymous auth succeeds; false if auth failed. */
39+ private val authReady = CompletableDeferred <Boolean >()
40+
3641 override var isDeveloper: Boolean = false
3742
3843 init {
@@ -73,19 +78,31 @@ class FirebaseUserSession(
7378 val user = it.user
7479 if (user != null ) {
7580 Timber .d(" User uid: ${user.uid} " )
76- _audienceContext .value = ageSignals.get( )
81+ authReady.complete( true )
7782 } else {
7883 crashlytics.log(" user cannot be signed in" )
7984 Timber .e(" User could not be signed in" )
8085 _audienceContext .value = AudienceContext .Unavailable
86+ authReady.complete(false )
8187 }
8288 } catch (ex: Exception ) {
8389 Timber .e(ex, " Could not sign in anonymously" )
8490 _audienceContext .value = AudienceContext .Unavailable
91+ authReady.complete(false )
8592 }
8693 }
8794 }
8895
96+ override fun resolveAudienceContext (activity : Activity ) {
97+ if (_audienceContext .value !is AudienceContext .Unresolved ) return
98+
99+ applicationScope.launch {
100+ if (! authReady.await()) return @launch
101+ if (_audienceContext .value !is AudienceContext .Unresolved ) return @launch
102+ _audienceContext .value = ageSignals.get(activity)
103+ }
104+ }
105+
89106 private fun getActiveConference (
90107 previous : Conference ? ,
91108 conferences : List <Conference >,
0 commit comments