@@ -29,6 +29,7 @@ import com.flipcash.services.analytics.AnalyticsEvent
29
29
import com.flipcash.services.analytics.FlipcashAnalyticsService
30
30
import com.flipcash.services.billing.BillingClient
31
31
import com.flipcash.services.controllers.AccountController
32
+ import com.flipcash.services.user.AuthState
32
33
import com.flipcash.services.user.UserManager
33
34
import com.getcode.manager.BottomBarAction
34
35
import com.getcode.manager.BottomBarManager
@@ -57,16 +58,21 @@ import com.kik.kikx.models.ScannableKikCode
57
58
import kotlinx.coroutines.CoroutineScope
58
59
import kotlinx.coroutines.Dispatchers
59
60
import kotlinx.coroutines.SupervisorJob
61
+ import kotlinx.coroutines.channels.BufferOverflow
60
62
import kotlinx.coroutines.delay
61
63
import kotlinx.coroutines.flow.MutableStateFlow
62
64
import kotlinx.coroutines.flow.StateFlow
63
65
import kotlinx.coroutines.flow.asStateFlow
66
+ import kotlinx.coroutines.flow.buffer
64
67
import kotlinx.coroutines.flow.distinctUntilChanged
65
68
import kotlinx.coroutines.flow.filter
69
+ import kotlinx.coroutines.flow.filterIsInstance
66
70
import kotlinx.coroutines.flow.launchIn
67
71
import kotlinx.coroutines.flow.map
68
72
import kotlinx.coroutines.flow.mapNotNull
69
73
import kotlinx.coroutines.flow.onEach
74
+ import kotlinx.coroutines.flow.scan
75
+ import kotlinx.coroutines.flow.take
70
76
import kotlinx.coroutines.flow.update
71
77
import kotlinx.coroutines.launch
72
78
import kotlinx.coroutines.suspendCancellableCoroutine
@@ -108,9 +114,15 @@ class RealSessionController @Inject constructor(
108
114
109
115
private val scannedRendezvous = mutableListOf<String >()
110
116
111
- private var welcomeBonus: LocalFiat ? = null
112
-
113
117
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
+
114
126
userManager.state
115
127
.map { it.isTimelockUnlocked }
116
128
.onEach { _state .update { it.copy(restrictionType = RestrictionType .TIMELOCK_UNLOCKED ) } }
@@ -138,6 +150,18 @@ class RealSessionController @Inject constructor(
138
150
featureFlagController.observe(FeatureFlag .VibrateOnScan )
139
151
.onEach { enabled -> _state .update { it.copy(vibrateOnScan = enabled) } }
140
152
.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)
141
165
}
142
166
143
167
/* *
@@ -146,7 +170,6 @@ class RealSessionController @Inject constructor(
146
170
* This function performs several actions to ensure the app is up-to-date and ready for user interaction:
147
171
* 1. Starts polling for updates (e.g., balance, exchange rates, activity feed).
148
172
* 2. Updates user flags.
149
- * 3. Requests an airdrop if applicable.
150
173
* 4. Checks for pending items in the activity feed.
151
174
* 5. Brings the activity feed to the current state.
152
175
* 6. Checks for any pending share actions via the share sheet.
@@ -160,7 +183,6 @@ class RealSessionController @Inject constructor(
160
183
)
161
184
startPolling()
162
185
updateUserFlags()
163
- checkForAirdrops()
164
186
checkPendingItemsInFeed()
165
187
bringActivityFeedCurrent()
166
188
shareSheetController.checkForShare()
@@ -215,16 +237,15 @@ class RealSessionController @Inject constructor(
215
237
}
216
238
}
217
239
218
- private fun checkForAirdrops (onAirdropReceived : ( LocalFiat ) -> Unit = {} ) {
240
+ private fun checkForAirdrops () {
219
241
if (userManager.authState.canAccessAuthenticatedApis) {
220
242
scope.launch {
221
243
userManager.accountCluster?.let {
222
244
transactionController.airdrop(
223
245
type = AirdropType .WelcomeBonus ,
224
246
destination = it.authority.keyPair
225
247
).onSuccess { amount ->
226
- welcomeBonus = amount
227
- onAirdropReceived(amount)
248
+ presentWelcomeBonus(amount)
228
249
}
229
250
}
230
251
}
@@ -280,19 +301,8 @@ class RealSessionController @Inject constructor(
280
301
}
281
302
}
282
303
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
-
294
304
override fun onCameraScanning (scanning : Boolean ) {
295
- _state .update { it.copy(isCameraScanEnabled = scanning) }
305
+ _state .update { it.copy(isCameraUp = scanning) }
296
306
}
297
307
298
308
override fun onCameraPermissionResult (result : PermissionResult ) {
0 commit comments