Skip to content

Commit 6b8a8dc

Browse files
committed
refactor: make isSyncingLdkNodePayments to collect state changes
1 parent ba9b211 commit 6b8a8dc

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

app/src/main/java/to/bitkit/repositories/ActivityRepo.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.synonym.bitkitcore.PaymentType
77
import com.synonym.bitkitcore.SortDirection
88
import kotlinx.coroutines.CoroutineDispatcher
99
import kotlinx.coroutines.TimeoutCancellationException
10-
import kotlinx.coroutines.delay
10+
import kotlinx.coroutines.flow.MutableStateFlow
1111
import kotlinx.coroutines.flow.first
1212
import kotlinx.coroutines.flow.map
1313
import kotlinx.coroutines.withContext
@@ -35,7 +35,8 @@ class ActivityRepo @Inject constructor(
3535
private val lightningRepo: LightningRepo,
3636
private val cacheStore: CacheStore,
3737
) {
38-
var isSyncingLdkNodePayments = false
38+
var isSyncingLdkNodePayments = MutableStateFlow(false)
39+
private set
3940

4041
val inProgressTransfers = cacheStore.data.map { it.inProgressTransfers }
4142

@@ -45,38 +46,37 @@ class ActivityRepo @Inject constructor(
4546
return@withContext runCatching {
4647

4748
withTimeout(SYNC_TIMEOUT_MS) {
48-
while (isSyncingLdkNodePayments) {
49-
Logger.debug("LDK-node payments are already being synced, waiting for completion", context = TAG)
50-
delay(SYNC_CHECK_DELAY_MS)
51-
}
49+
Logger.debug("isSyncingLdkNodePayments = ${isSyncingLdkNodePayments.value}", context = TAG)
50+
isSyncingLdkNodePayments.first { !it }
5251
}
5352

54-
deletePendingActivities()
53+
isSyncingLdkNodePayments = MutableStateFlow(true)
5554

56-
isSyncingLdkNodePayments = true
55+
deletePendingActivities()
5756
return@withContext lightningRepo.getPayments()
5857
.onSuccess { payments ->
5958
Logger.debug("Got payments with success, syncing activities", context = TAG)
6059
syncLdkNodePayments(payments = payments)
6160
updateActivitiesMetadata()
6261
boostPendingActivities()
6362
updateInProgressTransfers()
64-
isSyncingLdkNodePayments = false
63+
isSyncingLdkNodePayments = MutableStateFlow(false)
6564
return@withContext Result.success(Unit)
6665
}.onFailure { e ->
6766
Logger.error("Failed to sync ldk-node payments", e, context = TAG)
68-
isSyncingLdkNodePayments = false
67+
isSyncingLdkNodePayments = MutableStateFlow(false)
6968
return@withContext Result.failure(e)
7069
}.map { Unit }
7170
}.onFailure { e ->
7271
when (e) {
7372
is TimeoutCancellationException -> {
73+
isSyncingLdkNodePayments = MutableStateFlow(false)
7474
Logger.error("Timeout waiting for sync to complete, forcing reset", e, context = TAG)
75-
isSyncingLdkNodePayments = false
7675
}
76+
7777
else -> {
78+
isSyncingLdkNodePayments = MutableStateFlow(false)
7879
Logger.error("syncActivities error", e, context = TAG)
79-
isSyncingLdkNodePayments = false
8080
}
8181
}
8282
}

0 commit comments

Comments
 (0)