Skip to content

Commit 27e670c

Browse files
committed
feat(flipcash): add profile syncing to session
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 80a6b65 commit 27e670c

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.flipcash.app.core.internal.updater
2+
3+
import com.flipcash.app.core.updater.NetworkUpdater
4+
import com.flipcash.services.controllers.ProfileController
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.launch
7+
import javax.inject.Inject
8+
import kotlin.concurrent.fixedRateTimer
9+
import kotlin.time.Duration
10+
11+
class ProfileUpdater @Inject constructor(
12+
private val profileController: ProfileController,
13+
) : NetworkUpdater() {
14+
override fun poll(
15+
key: Any?,
16+
scope: CoroutineScope,
17+
frequency: Duration,
18+
startIn: Duration
19+
) {
20+
stop()
21+
updater = fixedRateTimer(
22+
name = "update profile",
23+
initialDelay = startIn.inWholeMilliseconds,
24+
period = frequency.inWholeMilliseconds
25+
) {
26+
scope.launch {
27+
profileController.updateUserProfile()
28+
}
29+
}
30+
}
31+
}

apps/flipcash/shared/session/src/main/kotlin/com/flipcash/app/session/internal/RealSessionController.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.flipcash.app.core.internal.bill.BillController
1212
import com.flipcash.app.core.internal.errors.showNetworkError
1313
import com.flipcash.app.core.internal.updater.BalanceUpdater
1414
import com.flipcash.app.core.internal.updater.ExchangeUpdater
15+
import com.flipcash.app.core.internal.updater.ProfileUpdater
1516
import com.flipcash.app.featureflags.FeatureFlag
1617
import com.flipcash.app.featureflags.FeatureFlagController
1718
import com.flipcash.app.pools.PoolUpdater
@@ -95,6 +96,7 @@ class RealSessionController @Inject constructor(
9596
private val activityFeedUpdater: ActivityFeedUpdater,
9697
private val poolsUpdater: PoolsUpdater,
9798
private val poolUpdater: PoolUpdater,
99+
private val profileUpdater: ProfileUpdater,
98100
private val shareSheetController: ShareSheetController,
99101
private val shareConfirmationController: ShareableConfirmationController,
100102
private val toastController: ToastController,
@@ -243,6 +245,8 @@ class RealSessionController @Inject constructor(
243245
poolsCoordinator.openPool.value?.let { id ->
244246
poolUpdater.poll(id, scope = scope, frequency = 10.seconds, startIn = 10.seconds)
245247
}
248+
249+
profileUpdater.poll(scope = scope, frequency = 60.seconds, startIn = 0.seconds)
246250
}
247251
}
248252

services/flipcash/src/main/kotlin/com/flipcash/services/controllers/ProfileController.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@ import com.flipcash.services.models.UserProfile
88
import com.flipcash.services.repository.ProfileRepository
99
import com.flipcash.services.user.UserManager
1010
import com.getcode.opencode.model.core.ID
11+
import com.getcode.utils.TraceType
12+
import com.getcode.utils.trace
1113
import javax.inject.Inject
14+
import javax.inject.Singleton
1215

16+
@Singleton
1317
class ProfileController @Inject constructor(
1418
private val repository: ProfileRepository,
1519
private val userManager: UserManager,
1620
) {
21+
suspend fun updateUserProfile() {
22+
val accountId = userManager.accountId ?: return
23+
24+
getProfileForUser(accountId)
25+
.onSuccess {
26+
trace(
27+
tag = "Profile",
28+
message = "Updated user profile",
29+
type = TraceType.Process
30+
)
31+
userManager.set(it)
32+
}.onFailure {
33+
trace(
34+
tag = "Profile",
35+
message = "Failed to update user profile",
36+
type = TraceType.Error
37+
)
38+
}
39+
}
40+
1741
suspend fun getProfileForUser(
18-
userId: ID = userManager.accountId.orEmpty(),
42+
userId: ID,
1943
): Result<UserProfile> {
2044
val owner = userManager.accountCluster?.authority?.keyPair
2145
?: return Result.failure(Throwable("No account cluster in UserManager"))

services/flipcash/src/main/kotlin/com/flipcash/services/user/UserManager.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.flipcash.services.user
22

33
import com.bugsnag.android.Bugsnag
4+
import com.flipcash.services.controllers.ProfileController
45
import com.flipcash.services.internal.model.account.UserFlags
6+
import com.flipcash.services.models.UserProfile
57
import com.getcode.crypt.DerivePath
68
import com.getcode.crypt.DerivedKey
79
import com.getcode.crypt.MnemonicPhrase
@@ -93,6 +95,7 @@ class UserManager @Inject constructor(
9395
val isTimelockUnlocked: Boolean = false,
9496
val pushToken: String? = null,
9597
val nextPoolIndex: Long = 0L,
98+
val userProfile: UserProfile? = null,
9699
)
97100

98101
init {
@@ -159,6 +162,10 @@ class UserManager @Inject constructor(
159162
_state.update { it.copy(pushToken = pushToken) }
160163
}
161164

165+
internal fun set(userProfile: UserProfile) {
166+
_state.update { it.copy(userProfile = userProfile) }
167+
}
168+
162169
fun poolAccountAt(index: Long): PoolAccount {
163170
return PoolAccount.create(
164171
mnemonic = mnemnonic!!,
@@ -195,6 +202,7 @@ class UserManager @Inject constructor(
195202
cluster = null,
196203
accountId = NoId,
197204
isTimelockUnlocked = false,
205+
userProfile = null,
198206
)
199207
}
200208
}

0 commit comments

Comments
 (0)