Skip to content

Commit cf7f89e

Browse files
committed
Fix sync and PIN issues
1 parent bb8de2a commit cf7f89e

File tree

4 files changed

+77
-65
lines changed

4 files changed

+77
-65
lines changed

app/src/main/java/to/bitkit/services/MigrationService.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import kotlinx.serialization.json.jsonPrimitive
3131
import kotlinx.serialization.json.put
3232
import to.bitkit.data.SettingsStore
3333
import to.bitkit.data.WidgetsStore
34-
import to.bitkit.data.resetPin
3534
import to.bitkit.data.keychain.Keychain
35+
import to.bitkit.data.resetPin
3636
import to.bitkit.di.json
3737
import to.bitkit.env.Env
3838
import to.bitkit.models.BitcoinDisplayUnit
@@ -374,10 +374,6 @@ class MigrationService @Inject constructor(
374374
it[stringPreferencesKey(RN_MIGRATION_COMPLETED_KEY)] = "true"
375375
it[stringPreferencesKey(RN_MIGRATION_CHECKED_KEY)] = "true"
376376
}
377-
378-
if (!hasRNLdkData()) {
379-
setShowingMigrationLoading(false)
380-
}
381377
} else {
382378
markMigrationChecked()
383379
setShowingMigrationLoading(false)

app/src/main/java/to/bitkit/ui/onboarding/TermsOfUseScreen.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ private fun TermsText(
135135
}
136136
}
137137

138-
139138
@Preview(showSystemUi = true)
140139
@Composable
141140
private fun TermsPreview() {

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

Lines changed: 73 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ class AppViewModel @Inject constructor(
220220
}
221221
viewModelScope.launch {
222222
// Delays are required for auth check on launch functionality
223-
delay(1000)
223+
delay(AUTH_CHECK_INITIAL_DELAY_MS)
224224
resetIsAuthenticatedState()
225-
delay(500)
225+
delay(AUTH_CHECK_SPLASH_DELAY_MS)
226226
splashVisible = false
227227
}
228228
viewModelScope.launch {
@@ -249,7 +249,7 @@ class AppViewModel @Inject constructor(
249249
} catch (e: TimeoutCancellationException) {
250250
if (!isCompletingMigration) {
251251
Logger.warn(
252-
"Migration loading screen timeout after 2.5 minutes, completing migration anyway",
252+
"Migration loading screen timeout, completing migration anyway",
253253
context = TAG
254254
)
255255
completeMigration()
@@ -319,18 +319,14 @@ class AppViewModel @Inject constructor(
319319
}
320320

321321
private suspend fun handleSyncCompleted() {
322-
walletRepo.debounceSyncByEvent()
323-
324322
val isShowingLoading = migrationService.isShowingMigrationLoading.value
325323
val isRestoringRemote = migrationService.isRestoringFromRNRemoteBackup.value
326324

327325
when {
328-
isShowingLoading && !isCompletingMigration -> {
329-
completeMigration()
330-
}
331-
isRestoringRemote -> {
332-
completeRNRemoteBackupRestore()
333-
}
326+
isShowingLoading && !isCompletingMigration -> completeMigration()
327+
isRestoringRemote -> completeRNRemoteBackupRestore()
328+
!isShowingLoading && !isCompletingMigration -> walletRepo.debounceSyncByEvent()
329+
else -> Unit
334330
}
335331
}
336332

@@ -351,9 +347,7 @@ class AppViewModel @Inject constructor(
351347
}
352348

353349
private suspend fun completeMigration() {
354-
if (isCompletingMigration) {
355-
return
356-
}
350+
if (isCompletingMigration) return
357351
isCompletingMigration = true
358352

359353
try {
@@ -368,61 +362,79 @@ class AppViewModel @Inject constructor(
368362
lightningRepo.stop().onFailure {
369363
Logger.error("Failed to stop node during migration restart", it, context = TAG)
370364
}
371-
delay(500)
365+
delay(MIGRATION_NODE_RESTART_DELAY_MS)
372366
lightningRepo.start(channelMigration = channelMigration, shouldRetry = false)
373367
.onSuccess {
374368
migrationService.consumePendingChannelMigration()
375369
walletRepo.syncNodeAndWallet()
376370
.onSuccess {
377-
lightningRepo.getPayments().onSuccess { payments ->
378-
activityRepo.syncLdkNodePayments(payments)
379-
}
380-
transferRepo.syncTransferStates()
381-
migrationService.reapplyMetadataAfterSync()
382-
383-
migrationService.setShowingMigrationLoading(false)
384-
385-
toast(
386-
type = Toast.ToastType.SUCCESS,
387-
title = "Migration Complete",
388-
description = "Your wallet has been successfully migrated"
389-
)
371+
finishMigrationSuccessfully()
390372
}
391373
.onFailure { e ->
392374
Logger.warn("Sync failed after restart during migration: $e", e, context = TAG)
393-
walletRepo.syncBalances()
394-
lightningRepo.getPayments().onSuccess { payments ->
395-
activityRepo.syncLdkNodePayments(payments)
396-
}
397-
transferRepo.syncTransferStates()
398-
migrationService.reapplyMetadataAfterSync()
399-
400-
migrationService.setShowingMigrationLoading(false)
401-
402-
toast(
403-
type = Toast.ToastType.SUCCESS,
404-
title = "Migration Complete",
405-
description = "Your wallet has been successfully migrated"
406-
)
375+
finishMigrationWithFallbackSync()
407376
}
408377
}
409378
.onFailure { e ->
410379
Logger.error("Failed to restart node after migration: $e", e, context = TAG)
411-
migrationService.setShowingMigrationLoading(false)
412-
toast(
413-
type = Toast.ToastType.ERROR,
414-
title = "Migration Warning",
415-
description = "Migration completed but node restart failed. Please restart the app."
416-
)
380+
finishMigrationWithError()
417381
}
418382
} catch (e: Exception) {
419383
Logger.error("Migration completion error: $e", e, context = TAG)
420-
migrationService.setShowingMigrationLoading(false)
384+
finishMigrationWithError()
421385
} finally {
422386
isCompletingMigration = false
423387
}
424388
}
425389

390+
private suspend fun finishMigrationSuccessfully() {
391+
lightningRepo.getPayments().onSuccess { payments ->
392+
activityRepo.syncLdkNodePayments(payments)
393+
}
394+
transferRepo.syncTransferStates()
395+
migrationService.reapplyMetadataAfterSync()
396+
397+
migrationService.setShowingMigrationLoading(false)
398+
delay(MIGRATION_AUTH_RESET_DELAY_MS)
399+
resetIsAuthenticatedStateInternal()
400+
401+
toast(
402+
type = Toast.ToastType.SUCCESS,
403+
title = "Migration Complete",
404+
description = "Your wallet has been successfully migrated"
405+
)
406+
}
407+
408+
private suspend fun finishMigrationWithFallbackSync() {
409+
walletRepo.syncBalances()
410+
lightningRepo.getPayments().onSuccess { payments ->
411+
activityRepo.syncLdkNodePayments(payments)
412+
}
413+
transferRepo.syncTransferStates()
414+
migrationService.reapplyMetadataAfterSync()
415+
416+
migrationService.setShowingMigrationLoading(false)
417+
delay(MIGRATION_AUTH_RESET_DELAY_MS)
418+
resetIsAuthenticatedStateInternal()
419+
420+
toast(
421+
type = Toast.ToastType.SUCCESS,
422+
title = "Migration Complete",
423+
description = "Your wallet has been successfully migrated"
424+
)
425+
}
426+
427+
private suspend fun finishMigrationWithError() {
428+
migrationService.setShowingMigrationLoading(false)
429+
delay(MIGRATION_AUTH_RESET_DELAY_MS)
430+
resetIsAuthenticatedStateInternal()
431+
toast(
432+
type = Toast.ToastType.ERROR,
433+
title = "Migration Warning",
434+
description = "Migration completed but node restart failed. Please restart the app."
435+
)
436+
}
437+
426438
private suspend fun handleOnchainTransactionConfirmed(event: Event.OnchainTransactionConfirmed) {
427439
activityRepo.handleOnchainTransactionConfirmed(event.txid, event.details)
428440
}
@@ -1767,13 +1779,15 @@ class AppViewModel @Inject constructor(
17671779
// endregion
17681780

17691781
// region security
1782+
private suspend fun resetIsAuthenticatedStateInternal() {
1783+
val settings = settingsStore.data.first()
1784+
val needsAuth = settings.isPinEnabled && settings.isPinOnLaunchEnabled
1785+
_isAuthenticated.value = !needsAuth
1786+
}
1787+
17701788
fun resetIsAuthenticatedState() {
17711789
viewModelScope.launch {
1772-
val settings = settingsStore.data.first()
1773-
val needsAuth = settings.isPinEnabled && settings.isPinOnLaunchEnabled
1774-
if (!needsAuth) {
1775-
_isAuthenticated.value = true
1776-
}
1790+
resetIsAuthenticatedStateInternal()
17771791
}
17781792
}
17791793

@@ -2130,7 +2144,11 @@ class AppViewModel @Inject constructor(
21302144
private const val MAX_BALANCE_FRACTION = 0.5
21312145
private const val MAX_FEE_AMOUNT_RATIO = 0.5
21322146
private const val SCREEN_TRANSITION_DELAY_MS = 300L
2133-
private const val MIGRATION_LOADING_TIMEOUT_MS = 150_000L
2147+
private const val MIGRATION_LOADING_TIMEOUT_MS = 300_000L
2148+
private const val MIGRATION_NODE_RESTART_DELAY_MS = 500L
2149+
private const val MIGRATION_AUTH_RESET_DELAY_MS = 500L
2150+
private const val AUTH_CHECK_INITIAL_DELAY_MS = 1000L
2151+
private const val AUTH_CHECK_SPLASH_DELAY_MS = 500L
21342152

21352153
/**How high the balance must be to show this warning to the user (in USD)*/
21362154
private const val BALANCE_THRESHOLD_USD = 500L

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import to.bitkit.repositories.RecoveryModeException
3434
import to.bitkit.repositories.SyncSource
3535
import to.bitkit.repositories.WalletRepo
3636
import to.bitkit.services.MigrationService
37-
import to.bitkit.services.PendingChannelMigration
3837
import to.bitkit.ui.onboarding.LOADING_MS
3938
import to.bitkit.ui.shared.toast.ToastEventBus
4039
import to.bitkit.utils.Logger
@@ -57,7 +56,6 @@ class WalletViewModel @Inject constructor(
5756
companion object {
5857
private const val TAG = "WalletViewModel"
5958
private val RESTORE_WAIT_TIMEOUT = 30.seconds
60-
private const val NODE_RESTART_DELAY_MS = 500L
6159
}
6260

6361
val lightningState = lightningRepo.lightningState
@@ -123,7 +121,9 @@ class WalletViewModel @Inject constructor(
123121
walletRepo.setWalletExistsState()
124122
walletExists = walletRepo.walletExists()
125123
loadCacheIfWalletExists()
126-
if (!walletExists) {
124+
if (walletExists) {
125+
startNode(0, channelMigration = null)
126+
} else {
127127
migrationService.setShowingMigrationLoading(false)
128128
}
129129
} catch (e: Exception) {
@@ -288,7 +288,6 @@ class WalletViewModel @Inject constructor(
288288
}
289289
}
290290

291-
292291
fun stop() {
293292
if (!walletExists) return
294293

0 commit comments

Comments
 (0)