Skip to content

Commit b0a344b

Browse files
committed
fix: empty balance flash on restore
1 parent f4e5677 commit b0a344b

File tree

4 files changed

+24
-14
lines changed

4 files changed

+24
-14
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class BackupRepo @Inject constructor(
301301
type = Toast.ToastType.ERROR,
302302
title = context.getString(R.string.settings__backup__failed_title),
303303
description = context.getString(R.string.settings__backup__failed_message).formatPlural(
304-
mapOf("interval" to (BACKUP_CHECK_INTERVAL / 60_000)) // displayed in minutes
304+
mapOf("interval" to (BACKUP_CHECK_INTERVAL / MINUTE_IN_MS)) // displayed in minutes
305305
),
306306
)
307307
}
@@ -396,12 +396,22 @@ class BackupRepo @Inject constructor(
396396
BackupCategory.LIGHTNING_CONNECTIONS -> throw NotImplementedError("LIGHTNING backup is managed by ldk-node")
397397
}
398398

399-
suspend fun performFullRestoreFromLatestBackup(): Result<Unit> = withContext(ioDispatcher) {
399+
suspend fun performFullRestoreFromLatestBackup(
400+
onCacheRestored: suspend () -> Unit = {},
401+
): Result<Unit> = withContext(ioDispatcher) {
400402
Logger.debug("Full restore starting", context = TAG)
401403

402404
isRestoring = true
403405

404406
return@withContext try {
407+
performRestore(BackupCategory.METADATA) { dataBytes ->
408+
val parsed = json.decodeFromString<MetadataBackupV1>(String(dataBytes))
409+
db.tagMetadataDao().upsert(parsed.tagMetadata)
410+
cacheStore.update { parsed.cache }
411+
onCacheRestored()
412+
Logger.debug("Restored caches and ${parsed.tagMetadata.size} tags metadata records", TAG)
413+
}
414+
405415
performRestore(BackupCategory.SETTINGS) { dataBytes ->
406416
val parsed = json.decodeFromString<SettingsData>(String(dataBytes)).resetPin()
407417
settingsStore.update { parsed }
@@ -415,12 +425,6 @@ class BackupRepo @Inject constructor(
415425
db.transferDao().upsert(parsed.transfers)
416426
Logger.debug("Restored ${parsed.transfers.size} transfers", context = TAG)
417427
}
418-
performRestore(BackupCategory.METADATA) { dataBytes ->
419-
val parsed = json.decodeFromString<MetadataBackupV1>(String(dataBytes))
420-
db.tagMetadataDao().upsert(parsed.tagMetadata)
421-
cacheStore.update { parsed.cache }
422-
Logger.debug("Restored caches and ${parsed.tagMetadata.size} tags metadata records", TAG)
423-
}
424428
performRestore(BackupCategory.BLOCKTANK) { dataBytes ->
425429
val parsed = json.decodeFromString<BlocktankBackupV1>(String(dataBytes))
426430
blocktankRepo.restoreFromBackup(parsed).onSuccess {
@@ -485,6 +489,7 @@ class BackupRepo @Inject constructor(
485489
companion object {
486490
private const val TAG = "BackupRepo"
487491

492+
private const val MINUTE_IN_MS = 60_000
488493
private const val BACKUP_DEBOUNCE = 5000L // 5 seconds
489494
private const val BACKUP_CHECK_INTERVAL = 60 * 1000L // 1 minute
490495
private const val FAILED_BACKUP_CHECK_TIME = 30 * 60 * 1000L // 30 minutes

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ class WalletRepo @Inject constructor(
6262
private val _balanceState = MutableStateFlow(BalanceState())
6363
val balanceState = _balanceState.asStateFlow()
6464

65-
init {
66-
// Load from cache once on init
67-
loadFromCache()
68-
}
69-
7065
fun loadFromCache() {
7166
// TODO try keeping in sync with cache if performant and reliable
7267
repoScope.launch {

app/src/main/java/to/bitkit/viewmodels/WalletViewModel.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class WalletViewModel @Inject constructor(
6969
private fun walletEffect(effect: WalletViewModelEffects) = viewModelScope.launch { _walletEffect.emit(effect) }
7070

7171
init {
72+
if (walletExists) {
73+
walletRepo.loadFromCache()
74+
}
7275
collectStates()
7376
}
7477

@@ -110,7 +113,7 @@ class WalletViewModel @Inject constructor(
110113

111114
private suspend fun restoreFromBackup() {
112115
restoreState = RestoreState.RestoringBackups
113-
backupRepo.performFullRestoreFromLatestBackup()
116+
backupRepo.performFullRestoreFromLatestBackup(onCacheRestored = walletRepo::loadFromCache)
114117
// data backup is not critical and mostly for user convenience so there is no reason to propagate errors up
115118
restoreState = RestoreState.BackupRestoreCompleted
116119
}

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class WalletRepoTest : BaseUnitTest() {
4848
private val cacheStore: CacheStore = mock()
4949
private val deriveBalanceStateUseCase: DeriveBalanceStateUseCase = mock()
5050
private val vssStoreIdProvider = mock<VssStoreIdProvider>()
51+
private val backupRepo = mock<BackupRepo>()
5152

5253
@Before
5354
fun setUp() {
@@ -78,6 +79,7 @@ class WalletRepoTest : BaseUnitTest() {
7879
cacheStore = cacheStore,
7980
deriveBalanceStateUseCase = deriveBalanceStateUseCase,
8081
vssStoreIdProvider = vssStoreIdProvider,
82+
backupRepo = backupRepo,
8183
)
8284

8385
@Test
@@ -196,6 +198,7 @@ class WalletRepoTest : BaseUnitTest() {
196198
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = existingAddress)))
197199
whenever(addressChecker.getAddressInfo(any())).thenReturn(mockAddressInfo())
198200
sut = createSut()
201+
sut.loadFromCache()
199202

200203
val result = sut.refreshBip21()
201204

@@ -266,6 +269,7 @@ class WalletRepoTest : BaseUnitTest() {
266269
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
267270
whenever(lightningRepo.createInvoice(anyOrNull(), any(), any())).thenReturn(Result.success("testInvoice"))
268271
sut = createSut()
272+
sut.loadFromCache()
269273

270274
sut.updateBip21Invoice(amountSats = 1000uL, description = "test").let { result ->
271275
assertTrue(result.isSuccess)
@@ -524,6 +528,7 @@ class WalletRepoTest : BaseUnitTest() {
524528
val testAddress = "testAddress"
525529
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
526530
sut = createSut()
531+
sut.loadFromCache()
527532
sut.setBolt11("existingInvoice")
528533
whenever(lightningRepo.canReceive()).thenReturn(false)
529534

@@ -570,6 +575,7 @@ class WalletRepoTest : BaseUnitTest() {
570575
)
571576
whenever(lightningRepo.newAddress()).thenReturn(Result.success("newAddress"))
572577
sut = createSut()
578+
sut.loadFromCache()
573579

574580
sut.refreshBip21ForEvent(
575581
Event.PaymentReceived(
@@ -589,6 +595,7 @@ class WalletRepoTest : BaseUnitTest() {
589595
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData(onchainAddress = testAddress)))
590596
whenever(addressChecker.getAddressInfo(any())).thenReturn(mockAddressInfo())
591597
sut = createSut()
598+
sut.loadFromCache()
592599

593600
sut.refreshBip21ForEvent(
594601
Event.PaymentReceived(

0 commit comments

Comments
 (0)