From b2d7534c9806dc4f6c9aec069fa28e240960a2f7 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 8 Dec 2022 14:31:20 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/tap/common/extensions/Store.kt | 2 +- .../com/tangem/tap/domain/TapWalletManager.kt | 15 ++++--- .../middlewares/MultiWalletMiddleware.kt | 7 +++- .../redux/middlewares/WalletMiddleware.kt | 31 ++++++++++---- .../wallet/redux/reducers/WalletReducer.kt | 2 +- .../redux/WalletSelectorMiddleware.kt | 41 +++++++++++++------ 6 files changed, 69 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/common/extensions/Store.kt b/app/src/main/java/com/tangem/tap/common/extensions/Store.kt index ed66f4c74b..69c5f1c085 100644 --- a/app/src/main/java/com/tangem/tap/common/extensions/Store.kt +++ b/app/src/main/java/com/tangem/tap/common/extensions/Store.kt @@ -27,7 +27,7 @@ fun Store<*>.dispatchNotification(resId: Int) { dispatchOnMain(GlobalAction.ShowNotification(resId)) } -@Suppress("unused") // receiver type +@Suppress("UnusedReceiverParameter") suspend fun Store<*>.onUserWalletSelected(userWallet: UserWallet, refresh: Boolean = false) { store.state.globalState.tapWalletManager.onWalletSelected(userWallet, refresh) } diff --git a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt index 368ed91c74..21b0ef03bf 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt @@ -1,6 +1,11 @@ package com.tangem.tap.domain -import com.tangem.blockchain.common.* +import com.tangem.blockchain.common.Blockchain +import com.tangem.blockchain.common.BlockchainSdkConfig +import com.tangem.blockchain.common.Token +import com.tangem.blockchain.common.Wallet +import com.tangem.blockchain.common.WalletManager +import com.tangem.blockchain.common.WalletManagerFactory import com.tangem.common.doOnFailure import com.tangem.common.doOnSuccess import com.tangem.common.services.Result @@ -93,18 +98,16 @@ class TapWalletManager { updateConfigManager(scanResponse) withMainContext { + store.dispatch(WalletAction.UserWalletChanged(userWallet)) store.dispatch(TwinCardsAction.IfTwinsPrepareState(scanResponse)) store.dispatch(WalletConnectAction.ResetState) store.dispatch(GlobalAction.SaveScanNoteResponse(scanResponse)) store.dispatch(WalletConnectAction.RestoreSessions(scanResponse)) - store.dispatch(WalletAction.UserWalletChanged(userWallet)) store.dispatch(GlobalAction.SetIfCardVerifiedOnline(!attestationFailed)) store.dispatch(WalletAction.Warnings.CheckIfNeeded) - - if (refresh) { - loadData(userWallet, refresh = true) - } } + + loadData(userWallet, refresh) } suspend fun loadData(userWallet: UserWallet, refresh: Boolean = false) { diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt index 927205c938..b0f5307329 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/MultiWalletMiddleware.kt @@ -6,7 +6,6 @@ import com.tangem.blockchain.common.WalletManager import com.tangem.common.doOnSuccess import com.tangem.common.extensions.guard import com.tangem.domain.common.extensions.withMainContext -import com.tangem.tap.* import com.tangem.tap.common.analytics.Analytics import com.tangem.tap.common.analytics.events.AnalyticsParam import com.tangem.tap.common.analytics.events.Token.ButtonRemoveToken @@ -30,6 +29,12 @@ import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.features.wallet.redux.WalletState import com.tangem.tap.features.wallet.redux.models.WalletDialog import com.tangem.tap.features.wallet.redux.reducers.toWallet +import com.tangem.tap.preferencesStorage +import com.tangem.tap.scope +import com.tangem.tap.store +import com.tangem.tap.userTokensRepository +import com.tangem.tap.userWalletsListManager +import com.tangem.tap.walletCurrenciesManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt index 1bb5cd9b42..ea7c79345b 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/middlewares/WalletMiddleware.kt @@ -11,10 +11,18 @@ import com.tangem.common.services.Result import com.tangem.domain.common.extensions.withMainContext import com.tangem.operations.attestation.Attestation import com.tangem.operations.attestation.OnlineCardVerifier -import com.tangem.tap.* import com.tangem.tap.common.analytics.Analytics import com.tangem.tap.common.analytics.events.Token -import com.tangem.tap.common.extensions.* +import com.tangem.tap.common.extensions.copyToClipboard +import com.tangem.tap.common.extensions.dispatchDebugErrorNotification +import com.tangem.tap.common.extensions.dispatchErrorNotification +import com.tangem.tap.common.extensions.dispatchOnMain +import com.tangem.tap.common.extensions.dispatchOpenUrl +import com.tangem.tap.common.extensions.dispatchToastNotification +import com.tangem.tap.common.extensions.isGreaterThan +import com.tangem.tap.common.extensions.onCardScanned +import com.tangem.tap.common.extensions.shareText +import com.tangem.tap.common.extensions.stripZeroPlainString import com.tangem.tap.common.redux.AppState import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.common.redux.navigation.AppScreen @@ -27,13 +35,24 @@ import com.tangem.tap.domain.model.WalletStoreModel import com.tangem.tap.features.demo.DemoHelper import com.tangem.tap.features.home.redux.HomeAction import com.tangem.tap.features.send.redux.PrepareSendScreen -import com.tangem.tap.features.wallet.models.* +import com.tangem.tap.features.wallet.models.Currency +import com.tangem.tap.features.wallet.models.PendingTransactionType +import com.tangem.tap.features.wallet.models.filterByCoin +import com.tangem.tap.features.wallet.models.getPendingTransactions +import com.tangem.tap.features.wallet.models.getSendableAmounts import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.features.wallet.redux.WalletData import com.tangem.tap.features.wallet.redux.WalletState import com.tangem.tap.features.wallet.redux.WalletStore import com.tangem.tap.network.NetworkConnectivity import com.tangem.tap.network.NetworkStateChanged +import com.tangem.tap.preferencesStorage +import com.tangem.tap.scope +import com.tangem.tap.store +import com.tangem.tap.tangemSdkManager +import com.tangem.tap.totalFiatBalanceCalculator +import com.tangem.tap.userWalletsListManager +import com.tangem.tap.userWalletsListManagerSafe import com.tangem.wallet.R import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async @@ -273,11 +292,7 @@ class WalletMiddleware { is WalletAction.ChangeWallet -> { changeWallet() } - is WalletAction.UserWalletChanged -> { - scope.launch { - globalState.tapWalletManager.loadData(action.userWallet) - } - } + is WalletAction.UserWalletChanged -> Unit is WalletAction.WalletStoresChanged -> { scope.launch(Dispatchers.Default) { fetchTotalFiatBalance(action.walletStores, walletState) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt index 54833f79e2..e9658e14fa 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt @@ -25,8 +25,8 @@ import com.tangem.tap.features.wallet.models.WalletRent import com.tangem.tap.features.wallet.redux.* import com.tangem.tap.features.wallet.ui.BalanceStatus import com.tangem.tap.features.wallet.ui.BalanceWidgetData -import com.tangem.tap.proxy.AppStateHolder import com.tangem.tap.features.wallet.ui.TokenData +import com.tangem.tap.proxy.AppStateHolder import com.tangem.tap.store import org.rekotlin.Action import timber.log.Timber diff --git a/app/src/main/java/com/tangem/tap/features/walletSelector/redux/WalletSelectorMiddleware.kt b/app/src/main/java/com/tangem/tap/features/walletSelector/redux/WalletSelectorMiddleware.kt index 00540c19f2..bac210c6d4 100644 --- a/app/src/main/java/com/tangem/tap/features/walletSelector/redux/WalletSelectorMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/walletSelector/redux/WalletSelectorMiddleware.kt @@ -1,5 +1,6 @@ package com.tangem.tap.features.walletSelector.redux +import com.tangem.common.CompletionResult import com.tangem.common.doOnFailure import com.tangem.common.doOnSuccess import com.tangem.common.flatMap @@ -149,18 +150,8 @@ internal class WalletSelectorMiddleware { private fun removeWallets(walletIdsToRemove: List, state: WalletSelectorState) { scope.launch { when (walletIdsToRemove.size) { - state.wallets.size -> { // Removing all wallets - userWalletsListManager.clear() - .flatMap { walletStoresManager.clear() } - .doOnSuccess { - store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Home)) - } - } - - else -> { - userWalletsListManager.delete(walletIdsToRemove.map { UserWalletId(it) }) - .flatMap { walletStoresManager.delete(walletIdsToRemove) } - } + state.wallets.size -> clearUserWallets() + else -> removeUserWallets(walletIdsToRemove, state) } .doOnFailure { error -> store.dispatchOnMain(WalletSelectorAction.HandleError(error)) @@ -197,6 +188,31 @@ internal class WalletSelectorMiddleware { ) } + private suspend fun clearUserWallets(): CompletionResult { + return userWalletsListManager.clear() + .flatMap { walletStoresManager.clear() } + .doOnSuccess { + store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Home)) + } + } + + private suspend fun removeUserWallets( + walletIdsToRemove: List, + state: WalletSelectorState, + ): CompletionResult { + val prevSelectedWalletId = state.selectedWalletId + return userWalletsListManager.delete(walletIdsToRemove.map { UserWalletId(it) }) + .flatMap { walletStoresManager.delete(walletIdsToRemove) } + .doOnSuccess { + val selectedWallet = userWalletsListManager.selectedUserWalletSync ?: return@doOnSuccess + val isSelectedWalletRemoved = prevSelectedWalletId != selectedWallet.walletId.stringValue + + if (isSelectedWalletRemoved) { + store.onUserWalletSelected(selectedWallet) + } + } + } + private suspend fun UserWalletModel.updateWalletStoresAndCalculateFiatBalance( walletStores: List, ): UserWalletModel { @@ -205,6 +221,7 @@ internal class WalletSelectorMiddleware { is UserWalletModel.Type.MultiCurrency -> type.copy( tokensCount = walletStores.flatMap { it.walletsData }.size, ) + is UserWalletModel.Type.SingleCurrency -> type.copy( blockchainName = walletStores .firstOrNull()