diff --git a/app/src/main/java/to/bitkit/repositories/CurrencyRepo.kt b/app/src/main/java/to/bitkit/repositories/CurrencyRepo.kt index 0e0d077e4b..951d90903a 100644 --- a/app/src/main/java/to/bitkit/repositories/CurrencyRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/CurrencyRepo.kt @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import to.bitkit.App import to.bitkit.async.appScope import to.bitkit.data.CacheStore import to.bitkit.data.SettingsStore @@ -84,6 +85,8 @@ class CurrencyRepo @Inject constructor( private fun startPolling() { repoScope.launch { fxRatePollingFlow.collect { + // Android blocks a backgrounded app's network, so a refresh there only fails; ON_START refreshes + if (App.currentActivity?.hasStartedActivity == false) return@collect refresh() } } diff --git a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt index aa4408f4f9..1dda53976a 100644 --- a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt @@ -291,8 +291,12 @@ class LightningRepo @Inject constructor( delay(SYNC_RETRY_DELAY_MS) if (restartStoppedNode()) return@launch Logger.info("Retrying sync after failure", context = TAG) - sync().onSuccess { - Logger.info("Sync retry succeeded", context = TAG) + runSync().onSuccess { + when (it) { + SyncRun.Synced -> Logger.info("Sync retry succeeded", context = TAG) + // The sync in progress runs once more for it, and its outcome decides the next check + SyncRun.Deferred -> Logger.info("Sync retry deferred to the sync in progress", context = TAG) + } }.onFailure { Logger.warn("Sync retry failed, will retry in ${SYNC_RETRY_DELAY_MS / 1000}s", it, context = TAG) } @@ -946,12 +950,17 @@ class LightningRepo @Inject constructor( } } - suspend fun sync(): Result = executeWhenNodeRunning("sync") { + suspend fun sync(): Result = runSync().map { } + + /** Whether a sync ran, or was left to the one in progress, which runs once more after it ends. */ + private enum class SyncRun { Synced, Deferred } + + private suspend fun runSync(): Result = executeWhenNodeRunning("sync") { // If sync is in progress, mark pending and skip if (!syncMutex.tryLock()) { syncPending.set(true) Logger.verbose("Sync in progress, pending sync marked", context = TAG) - return@executeWhenNodeRunning Result.success(Unit) + return@executeWhenNodeRunning Result.success(SyncRun.Deferred) } runCatching { @@ -969,6 +978,7 @@ class LightningRepo @Inject constructor( } if (syncPending.get()) delay(MS_SYNC_LOOP_DEBOUNCE) } while (syncPending.getAndSet(false)) + SyncRun.Synced }.also { _lightningState.update { state -> state.copy(isSyncingWallet = false) } syncMutex.unlock() diff --git a/app/src/main/java/to/bitkit/repositories/SparkRepo.kt b/app/src/main/java/to/bitkit/repositories/SparkRepo.kt index 1f3272fc0b..1c33eef3b9 100644 --- a/app/src/main/java/to/bitkit/repositories/SparkRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/SparkRepo.kt @@ -48,6 +48,7 @@ import kotlinx.serialization.Serializable import org.lightningdevkit.ldknode.Event import org.lightningdevkit.ldknode.PaymentFailureReason import org.lightningdevkit.ldknode.PaymentId +import to.bitkit.App import to.bitkit.async.appScope import to.bitkit.data.CacheStore import to.bitkit.data.SettingsStore @@ -1024,7 +1025,15 @@ class SparkRepo @Inject constructor( val info = runSuspendCatching { sparkService.getInfo() } .onFailure { Logger.warn("Failed to read Spark's sync state", it, context = TAG) } .getOrNull() ?: return false - info.lastSyncError?.let { Logger.warn("Spark's last sync failed: '$it'", context = TAG) } + info.lastSyncError?.let { + // Spark keeps syncing in the background, where Android blocks the app's network, so a failure there is + // expected and not a fault to warn about + if (App.currentActivity?.hasStartedActivity == false) { + Logger.debug("Spark's last sync failed in the background: '$it'", context = TAG) + } else { + Logger.warn("Spark's last sync failed: '$it'", context = TAG) + } + } val syncedAt = info.lastSyncedAt return info.lastSyncError == null && syncedAt != null && (seconds == null || syncedAt > seconds) }