@@ -29,6 +29,7 @@ import com.flipcash.services.analytics.AnalyticsEvent
2929import com.flipcash.services.analytics.FlipcashAnalyticsService
3030import com.flipcash.services.billing.BillingClient
3131import com.flipcash.services.controllers.AccountController
32+ import com.flipcash.services.user.AuthState
3233import com.flipcash.services.user.UserManager
3334import com.getcode.manager.BottomBarAction
3435import com.getcode.manager.BottomBarManager
@@ -57,16 +58,21 @@ import com.kik.kikx.models.ScannableKikCode
5758import kotlinx.coroutines.CoroutineScope
5859import kotlinx.coroutines.Dispatchers
5960import kotlinx.coroutines.SupervisorJob
61+ import kotlinx.coroutines.channels.BufferOverflow
6062import kotlinx.coroutines.delay
6163import kotlinx.coroutines.flow.MutableStateFlow
6264import kotlinx.coroutines.flow.StateFlow
6365import kotlinx.coroutines.flow.asStateFlow
66+ import kotlinx.coroutines.flow.buffer
6467import kotlinx.coroutines.flow.distinctUntilChanged
6568import kotlinx.coroutines.flow.filter
69+ import kotlinx.coroutines.flow.filterIsInstance
6670import kotlinx.coroutines.flow.launchIn
6771import kotlinx.coroutines.flow.map
6872import kotlinx.coroutines.flow.mapNotNull
6973import kotlinx.coroutines.flow.onEach
74+ import kotlinx.coroutines.flow.scan
75+ import kotlinx.coroutines.flow.take
7076import kotlinx.coroutines.flow.update
7177import kotlinx.coroutines.launch
7278import kotlinx.coroutines.suspendCancellableCoroutine
@@ -108,9 +114,15 @@ class RealSessionController @Inject constructor(
108114
109115 private val scannedRendezvous = mutableListOf<String >()
110116
111- private var welcomeBonus: LocalFiat ? = null
112-
113117 init {
118+ // reset state on logouts
119+ userManager.state
120+ .map { it.authState }
121+ .filterIsInstance<AuthState .LoggedOut >()
122+ .onEach {
123+ _state .update { SessionState () }
124+ }.launchIn(scope)
125+
114126 userManager.state
115127 .map { it.isTimelockUnlocked }
116128 .onEach { _state .update { it.copy(restrictionType = RestrictionType .TIMELOCK_UNLOCKED ) } }
@@ -138,6 +150,18 @@ class RealSessionController @Inject constructor(
138150 featureFlagController.observe(FeatureFlag .VibrateOnScan )
139151 .onEach { enabled -> _state .update { it.copy(vibrateOnScan = enabled) } }
140152 .launchIn(scope)
153+
154+ state
155+ .map { it.isCameraUp }
156+ .distinctUntilChanged() // Emit only when value changes
157+ .scan(Pair (null as Boolean? , null as Boolean? )) { previousPair, current ->
158+ Pair (previousPair.second, current)
159+ }
160+ .mapNotNull { (previous, current) ->
161+ if (previous == null && current != null ) current else null
162+ }
163+ .onEach { checkForAirdrops() }
164+ .launchIn(scope)
141165 }
142166
143167 /* *
@@ -146,7 +170,6 @@ class RealSessionController @Inject constructor(
146170 * This function performs several actions to ensure the app is up-to-date and ready for user interaction:
147171 * 1. Starts polling for updates (e.g., balance, exchange rates, activity feed).
148172 * 2. Updates user flags.
149- * 3. Requests an airdrop if applicable.
150173 * 4. Checks for pending items in the activity feed.
151174 * 5. Brings the activity feed to the current state.
152175 * 6. Checks for any pending share actions via the share sheet.
@@ -160,7 +183,6 @@ class RealSessionController @Inject constructor(
160183 )
161184 startPolling()
162185 updateUserFlags()
163- checkForAirdrops()
164186 checkPendingItemsInFeed()
165187 bringActivityFeedCurrent()
166188 shareSheetController.checkForShare()
@@ -215,16 +237,15 @@ class RealSessionController @Inject constructor(
215237 }
216238 }
217239
218- private fun checkForAirdrops (onAirdropReceived : ( LocalFiat ) -> Unit = {} ) {
240+ private fun checkForAirdrops () {
219241 if (userManager.authState.canAccessAuthenticatedApis) {
220242 scope.launch {
221243 userManager.accountCluster?.let {
222244 transactionController.airdrop(
223245 type = AirdropType .WelcomeBonus ,
224246 destination = it.authority.keyPair
225247 ).onSuccess { amount ->
226- welcomeBonus = amount
227- onAirdropReceived(amount)
248+ presentWelcomeBonus(amount)
228249 }
229250 }
230251 }
@@ -280,19 +301,8 @@ class RealSessionController @Inject constructor(
280301 }
281302 }
282303
283- override fun onCameraVisible () {
284- if (welcomeBonus != null ) {
285- presentWelcomeBonus(welcomeBonus!! )
286- welcomeBonus = null
287- } else {
288- checkForAirdrops {
289- presentWelcomeBonus(it)
290- }
291- }
292- }
293-
294304 override fun onCameraScanning (scanning : Boolean ) {
295- _state .update { it.copy(isCameraScanEnabled = scanning) }
305+ _state .update { it.copy(isCameraUp = scanning) }
296306 }
297307
298308 override fun onCameraPermissionResult (result : PermissionResult ) {
0 commit comments