@@ -65,7 +65,6 @@ import kotlinx.coroutines.flow.StateFlow
6565import kotlinx.coroutines.flow.asStateFlow
6666import kotlinx.coroutines.flow.distinctUntilChanged
6767import kotlinx.coroutines.flow.filter
68- import kotlinx.coroutines.flow.filterIsInstance
6968import kotlinx.coroutines.flow.launchIn
7069import kotlinx.coroutines.flow.map
7170import kotlinx.coroutines.flow.mapNotNull
@@ -136,33 +135,30 @@ class RealSessionController @Inject constructor(
136135
137136 private val scannedRendezvous = mutableMapOf<String , Long >()
138137
139- @Volatile
140- private var lastForegroundTimestamp = 0L
141-
142138 private val giftCardClaimInProgress = MutableStateFlow <String ?>(null )
143139
144140 init {
145- // reset state on logouts
141+ // handle auth state transitions: cleanup on logout, start polling on login
146142 userManager.state
147143 .map { it.authState }
148- .filterIsInstance<AuthState .LoggedOut >()
149- .onEach {
150- _state .update { SessionState () }
151- lastForegroundTimestamp = 0L
144+ .distinctUntilChanged()
145+ .onEach { authState ->
146+ when {
147+ authState is AuthState .LoggedOut -> {
148+ stopPolling()
149+ _state .update { SessionState () }
150+ }
151+ authState.canAccessAuthenticatedApis -> {
152+ onAppInForeground()
153+ }
154+ }
152155 }.launchIn(scope)
153156
154157 userManager.state
155158 .map { it.isTimelockUnlocked }
156159 .onEach { _state .update { it.copy(restrictionType = RestrictionType .TIMELOCK_UNLOCKED ) } }
157160 .launchIn(scope)
158161
159- userManager.state
160- .mapNotNull { it.authState }
161- .filter { it.canAccessAuthenticatedApis }
162- .distinctUntilChanged()
163- .onEach { onAppInForeground() }
164- .launchIn(scope)
165-
166162 userManager.state
167163 .mapNotNull { it.authState }
168164 .filter { it.isAtLeastRegistered }
@@ -204,13 +200,6 @@ class RealSessionController @Inject constructor(
204200 * 7. If the user is registered, connects to the billing client.
205201 */
206202 override fun onAppInForeground () {
207- val now = Clock .System .now().toEpochMilliseconds()
208- if (now - lastForegroundTimestamp < FOREGROUND_DEDUP_WINDOW_MS ) {
209- trace(tag = " Session" , message = " onAppInForeground skipped (dedup)" , type = TraceType .Process )
210- return
211- }
212- lastForegroundTimestamp = now
213-
214203 trace(
215204 tag = " Session" ,
216205 message = " onAppInForeground" ,
@@ -868,5 +857,4 @@ class RealSessionController @Inject constructor(
868857}
869858
870859private val AIRDROP_INITIAL_DELAY = 1 .seconds
871- private val CASH_LINK_CONFIRMATION_DELAY = 500 .milliseconds
872- private const val FOREGROUND_DEDUP_WINDOW_MS = 2_000L
860+ private val CASH_LINK_CONFIRMATION_DELAY = 500 .milliseconds
0 commit comments