@@ -7,10 +7,14 @@ import kotlinx.coroutines.CoroutineScope
77import kotlinx.coroutines.Job
88import kotlinx.coroutines.SupervisorJob
99import kotlinx.coroutines.delay
10+ import kotlinx.coroutines.flow.MutableStateFlow
11+ import kotlinx.coroutines.flow.StateFlow
12+ import kotlinx.coroutines.flow.asStateFlow
1013import kotlinx.coroutines.flow.distinctUntilChanged
1114import kotlinx.coroutines.flow.drop
1215import kotlinx.coroutines.flow.first
1316import kotlinx.coroutines.flow.map
17+ import kotlinx.coroutines.flow.update
1418import kotlinx.coroutines.launch
1519import kotlinx.coroutines.withContext
1620import kotlinx.datetime.Clock
@@ -63,7 +67,8 @@ class BackupRepo @Inject constructor(
6367 private val dataListenerJobs = mutableListOf<Job >()
6468 private var periodicCheckJob: Job ? = null
6569 private var isObserving = false
66- private var isRestoring = false
70+ private val _isRestoring = MutableStateFlow (false )
71+ val isRestoring: StateFlow <Boolean > = _isRestoring .asStateFlow()
6772
6873 private var lastNotificationTime = 0L
6974
@@ -119,7 +124,7 @@ class BackupRepo @Inject constructor(
119124 old.synced == new.synced && old.required == new.required
120125 }
121126 .collect { status ->
122- if (status.isRequired && ! status.running && ! isRestoring) {
127+ if (status.isRequired && ! status.running && ! isRestoring.value ) {
123128 scheduleBackup(category)
124129 }
125130 }
@@ -137,7 +142,7 @@ class BackupRepo @Inject constructor(
137142 .distinctUntilChanged()
138143 .drop(1 )
139144 .collect {
140- if (isRestoring) return @collect
145+ if (isRestoring.value ) return @collect
141146 markBackupRequired(BackupCategory .SETTINGS )
142147 }
143148 }
@@ -148,7 +153,7 @@ class BackupRepo @Inject constructor(
148153 .distinctUntilChanged()
149154 .drop(1 )
150155 .collect {
151- if (isRestoring) return @collect
156+ if (isRestoring.value ) return @collect
152157 markBackupRequired(BackupCategory .WIDGETS )
153158 }
154159 }
@@ -160,7 +165,7 @@ class BackupRepo @Inject constructor(
160165 .distinctUntilChanged()
161166 .drop(1 )
162167 .collect {
163- if (isRestoring) return @collect
168+ if (isRestoring.value ) return @collect
164169 markBackupRequired(BackupCategory .WALLET )
165170 }
166171 }
@@ -172,7 +177,7 @@ class BackupRepo @Inject constructor(
172177 .distinctUntilChanged()
173178 .drop(1 )
174179 .collect {
175- if (isRestoring) return @collect
180+ if (isRestoring.value ) return @collect
176181 markBackupRequired(BackupCategory .METADATA )
177182 }
178183 }
@@ -185,7 +190,7 @@ class BackupRepo @Inject constructor(
185190 .distinctUntilChanged()
186191 .drop(1 )
187192 .collect {
188- if (isRestoring) return @collect
193+ if (isRestoring.value ) return @collect
189194 markBackupRequired(BackupCategory .METADATA )
190195 }
191196 }
@@ -196,7 +201,7 @@ class BackupRepo @Inject constructor(
196201 blocktankRepo.blocktankState
197202 .drop(1 )
198203 .collect {
199- if (isRestoring) return @collect
204+ if (isRestoring.value ) return @collect
200205 markBackupRequired(BackupCategory .BLOCKTANK )
201206 }
202207 }
@@ -207,7 +212,7 @@ class BackupRepo @Inject constructor(
207212 activityRepo.activitiesChanged
208213 .drop(1 )
209214 .collect {
210- if (isRestoring) return @collect
215+ if (isRestoring.value ) return @collect
211216 markBackupRequired(BackupCategory .ACTIVITY )
212217 }
213218 }
@@ -220,7 +225,7 @@ class BackupRepo @Inject constructor(
220225 val lastSync = lightningService.status?.latestLightningWalletSyncTimestamp?.toLong()
221226 ?.let { it * 1000 } // Convert seconds to millis
222227 ? : return @collect
223- if (isRestoring) return @collect
228+ if (isRestoring.value ) return @collect
224229 cacheStore.updateBackupStatus(BackupCategory .LIGHTNING_CONNECTIONS ) {
225230 it.copy(required = lastSync, synced = lastSync, running = false )
226231 }
@@ -265,7 +270,7 @@ class BackupRepo @Inject constructor(
265270
266271 // Double-check if backup is still needed
267272 val status = cacheStore.backupStatuses.first()[category] ? : BackupItemStatus ()
268- if (status.isRequired && ! isRestoring) {
273+ if (status.isRequired && ! isRestoring.value ) {
269274 triggerBackup(category)
270275 } else {
271276 // Backup no longer needed, reset running flag
@@ -407,12 +412,13 @@ class BackupRepo @Inject constructor(
407412 ): Result <Unit > = withContext(ioDispatcher) {
408413 Logger .debug(" Full restore starting" , context = TAG )
409414
410- isRestoring = true
415+ _isRestoring .update { true }
411416
412417 return @withContext try {
413418 performRestore(BackupCategory .METADATA ) { dataBytes ->
414419 val parsed = json.decodeFromString<MetadataBackupV1 >(String (dataBytes))
415- cacheStore.update { parsed.cache }
420+ val caches = parsed.cache.copy(onchainAddress = " " ) // Force onchain address rotation
421+ cacheStore.update { caches }
416422 Logger .debug(" Restored caches: ${jsonLogOf(parsed.cache.copy(cachedRates = emptyList()))} " , TAG )
417423 onCacheRestored()
418424 db.tagMetadataDao().upsert(parsed.tagMetadata)
@@ -454,7 +460,7 @@ class BackupRepo @Inject constructor(
454460 Logger .warn(" Full restore error" , e = e, context = TAG )
455461 Result .failure(e)
456462 } finally {
457- isRestoring = false
463+ _isRestoring .update { false }
458464 }
459465 }
460466
0 commit comments