diff --git a/app/src/main/java/com/tangem/tap/features/details/redux/DetailsMiddleware.kt b/app/src/main/java/com/tangem/tap/features/details/redux/DetailsMiddleware.kt index 0d40b9bca5..f429e3423f 100644 --- a/app/src/main/java/com/tangem/tap/features/details/redux/DetailsMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/details/redux/DetailsMiddleware.kt @@ -119,28 +119,24 @@ class DetailsMiddleware { doBeforeErase() tangemSdkManager.resetToFactorySettings(card.cardId, true) + .flatMap { userWalletsListManager.delete(listOfNotNull(userWalletId)) } .flatMap { tangemSdkManager.deleteSavedUserCodes(setOf(card.cardId)) } .doOnSuccess { Analytics.send(Settings.CardSettings.FactoryResetFinished()) val selectedUserWallet = userWalletsListManager.selectedUserWalletSync if (selectedUserWallet != null) { + store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Wallet)) + store.onUserWalletSelected(selectedUserWallet) + } else { if (userWalletsListManager.isLockedSync) { - userWalletsListManager.delete(listOfNotNull(userWalletId)) store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Welcome)) } else { - store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Wallet)) - delay(timeMillis = 500) - userWalletsListManager.delete(listOfNotNull(userWalletId)) - store.onUserWalletSelected(selectedUserWallet) + store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Home)) } - } else { - userWalletsListManager.delete(listOfNotNull(userWalletId)) - store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Home)) } } .doOnFailure { error -> - userWalletsListManager.delete(listOfNotNull(userWalletId)) if (error is TangemSdkError && error !is TangemSdkError.UserCancelled) { Analytics.send(Settings.CardSettings.FactoryResetFinished(error)) } diff --git a/data/card/src/main/java/com/tangem/data/card/DefaultCardRepository.kt b/data/card/src/main/java/com/tangem/data/card/DefaultCardRepository.kt index 0d868f3f10..d0250fd7c3 100644 --- a/data/card/src/main/java/com/tangem/data/card/DefaultCardRepository.kt +++ b/data/card/src/main/java/com/tangem/data/card/DefaultCardRepository.kt @@ -42,9 +42,19 @@ internal class DefaultCardRepository( appPreferencesStore.editData { mutablePreferences -> val usedCards = mutablePreferences.getUsedCards() - val updatedUsedCards = cardIds.map { cardId -> - usedCards.updateCard(cardId) { - it.copy(isActivationStarted = true, isActivationFinished = true) + val newCards = cardIds.mapNotNull { newCardId -> + if (usedCards.none { it.cardId == newCardId }) { + createDefaultUsedCardInfo(cardId = newCardId) + } else { + null + } + } + + val updatedUsedCards = (usedCards + newCards).map { card -> + if (cardIds.contains(card.cardId)) { + card.copy(isActivationStarted = true, isActivationFinished = true) + } else { + card } } @@ -108,7 +118,7 @@ internal class DefaultCardRepository( cardId: String, update: (UsedCardInfo) -> UsedCardInfo, ): List { - val card = find { it.cardId == cardId } ?: UsedCardInfo(cardId = cardId) + val card = find { it.cardId == cardId } ?: createDefaultUsedCardInfo(cardId = cardId) return addOrReplace(item = update(card), predicate = { it.cardId == cardId }) } @@ -127,4 +137,6 @@ internal class DefaultCardRepository( else -> null } } + + private fun createDefaultUsedCardInfo(cardId: String) = UsedCardInfo(cardId = cardId) } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt index f710ba4c16..62d6a03cfd 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt @@ -178,7 +178,7 @@ internal class WalletViewModel @Inject constructor( } } - private fun updateWallets(action: WalletsUpdateActionResolver.Action) { + private suspend fun updateWallets(action: WalletsUpdateActionResolver.Action) { when (action) { is WalletsUpdateActionResolver.Action.InitializeWallets -> initializeWallets(action) is WalletsUpdateActionResolver.Action.ReinitializeWallets -> { @@ -203,7 +203,7 @@ internal class WalletViewModel @Inject constructor( } } - private fun initializeWallets(action: WalletsUpdateActionResolver.Action.InitializeWallets) { + private suspend fun initializeWallets(action: WalletsUpdateActionResolver.Action.InitializeWallets) { walletScreenContentLoader.load( userWallet = action.selectedWallet, clickIntents = clickIntents, @@ -219,100 +219,96 @@ internal class WalletViewModel @Inject constructor( ), ) - viewModelScope.launch(dispatchers.main) { - if (action.wallets.size > 1 && isWalletsScrollPreviewEnabled()) { - withContext(dispatchers.io) { - delay(timeMillis = 1_800) - } + if (action.wallets.size > 1 && isWalletsScrollPreviewEnabled()) { + withContext(dispatchers.io) { delay(timeMillis = 1_800) } - walletEventSender.send( - event = WalletEvent.DemonstrateWalletsScrollPreview( - direction = if (action.selectedWalletIndex == action.wallets.lastIndex) { - Direction.RIGHT - } else { - Direction.LEFT - }, - ), - ) - } + walletEventSender.send( + event = WalletEvent.DemonstrateWalletsScrollPreview( + direction = if (action.selectedWalletIndex == action.wallets.lastIndex) { + Direction.RIGHT + } else { + Direction.LEFT + }, + ), + ) } } private fun reinitializeWallet(action: WalletsUpdateActionResolver.Action.ReinitializeWallet) { - viewModelScope.launch(dispatchers.main) { - walletScreenContentLoader.cancel(action.prevWalletId) + walletScreenContentLoader.cancel(action.prevWalletId) - walletScreenContentLoader.load( - userWallet = action.selectedWallet, - clickIntents = clickIntents, - coroutineScope = viewModelScope, - ) + walletScreenContentLoader.load( + userWallet = action.selectedWallet, + clickIntents = clickIntents, + coroutineScope = viewModelScope, + ) - stateHolder.update( - ReinitializeWalletTransformer(userWallet = action.selectedWallet, clickIntents = clickIntents), - ) - } + stateHolder.update( + ReinitializeWalletTransformer(userWallet = action.selectedWallet, clickIntents = clickIntents), + ) } - private fun addWallet(action: WalletsUpdateActionResolver.Action.AddWallet) { - viewModelScope.launch(dispatchers.main) { - stateHolder.update( - AddWalletTransformer( - userWallet = action.selectedWallet, - clickIntents = clickIntents, - ), - ) - - walletScreenContentLoader.load( + private suspend fun addWallet(action: WalletsUpdateActionResolver.Action.AddWallet) { + stateHolder.update( + AddWalletTransformer( userWallet = action.selectedWallet, clickIntents = clickIntents, - coroutineScope = viewModelScope, - ) + ), + ) + walletScreenContentLoader.load( + userWallet = action.selectedWallet, + clickIntents = clickIntents, + coroutineScope = viewModelScope, + ) + + withContext(dispatchers.io) { delay(timeMillis = 700) } + + scrollToWallet(index = action.selectedWalletIndex) + } + + private suspend fun deleteWallet(action: WalletsUpdateActionResolver.Action.DeleteWallet) { + walletScreenContentLoader.load( + userWallet = action.selectedWallet, + clickIntents = clickIntents, + coroutineScope = viewModelScope, + ) + + if (action.selectedWalletIndex != 0) { + /* + * If card is reset to factory settings, then Compose need some time to draw the WalletScreen. + * Otherwise, scroll isn't happened + */ withContext(dispatchers.io) { delay(timeMillis = 700) } scrollToWallet(index = action.selectedWalletIndex) + + withContext(dispatchers.io) { delay(timeMillis = 1000) } } + + stateHolder.update( + DeleteWalletTransformer( + selectedWalletIndex = action.selectedWalletIndex, + deletedWalletId = action.deletedWalletId, + ), + ) } - private fun deleteWallet(action: WalletsUpdateActionResolver.Action.DeleteWallet) { - viewModelScope.launch(dispatchers.main) { - walletScreenContentLoader.load( - userWallet = action.selectedWallet, + private suspend fun unlockWallet(action: WalletsUpdateActionResolver.Action.UnlockWallet) { + withContext(dispatchers.io) { delay(timeMillis = 700) } + + stateHolder.update( + transformer = UnlockWalletTransformer( + unlockedWallets = action.unlockedWallets, clickIntents = clickIntents, - coroutineScope = viewModelScope, - ) + ), + ) - scrollToWallet(index = action.selectedWalletIndex) - - withContext(dispatchers.io) { delay(timeMillis = 700) } - - stateHolder.update( - DeleteWalletTransformer( - selectedWalletIndex = action.selectedWalletIndex, - deletedWalletId = action.deletedWalletId, - ), - ) - } - } - - private fun unlockWallet(action: WalletsUpdateActionResolver.Action.UnlockWallet) { - viewModelScope.launch(dispatchers.main) { - withContext(dispatchers.io) { delay(timeMillis = 700) } - - stateHolder.update( - transformer = UnlockWalletTransformer( - unlockedWallets = action.unlockedWallets, - clickIntents = clickIntents, - ), - ) - - walletScreenContentLoader.load( - userWallet = action.selectedWallet, - clickIntents = clickIntents, - coroutineScope = viewModelScope, - ) - } + walletScreenContentLoader.load( + userWallet = action.selectedWallet, + clickIntents = clickIntents, + coroutineScope = viewModelScope, + ) } private fun scrollToWallet(index: Int) {