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 cb9553ad37..29f4c4d74c 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,10 @@ 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.WalletManagerFactory import com.tangem.common.doOnFailure import com.tangem.common.doOnSuccess import com.tangem.core.analytics.Analytics @@ -24,17 +28,39 @@ import com.tangem.tap.preferencesStorage import com.tangem.tap.store import com.tangem.tap.tangemSdkManager import com.tangem.tap.walletStoresManager +import com.tangem.utils.coroutines.AppCoroutineDispatcherProvider +import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch import timber.log.Timber -class TapWalletManager { - val walletManagerFactory: WalletManagerFactory - by lazy { WalletManagerFactory(blockchainSdkConfig) } +class TapWalletManager( + private val dispatchers: CoroutineDispatcherProvider = AppCoroutineDispatcherProvider(), +) { private val blockchainSdkConfig by lazy { store.state.globalState.configManager?.config?.blockchainSdkConfig ?: BlockchainSdkConfig() } + private var loadUserWalletDataJob: Job? = null + set(value) { + field?.cancel() + field = value + } + + val walletManagerFactory: WalletManagerFactory + by lazy { WalletManagerFactory(blockchainSdkConfig) } + suspend fun onWalletSelected(userWallet: UserWallet, refresh: Boolean, sendAnalyticsEvent: Boolean) { + // If a previous job was running, it gets cancelled before the new one starts, + // ensuring that only one job is active at any given time. + loadUserWalletDataJob = CoroutineScope(dispatchers.io) + .launch { loadUserWalletData(userWallet, refresh, sendAnalyticsEvent) } + .also { it.join() } + } + + private suspend fun loadUserWalletData(userWallet: UserWallet, refresh: Boolean, sendAnalyticsEvent: Boolean) { Analytics.setContext(userWallet.scanResponse) if (sendAnalyticsEvent) { Analytics.send(Basic.WalletOpened()) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt index 18bf0dbc24..5a8e0249b0 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletAction.kt @@ -34,7 +34,6 @@ sealed class WalletAction : Action { sealed class MultiWallet : WalletAction() { data class SelectWallet(val currency: Currency?) : MultiWallet() - data class SetSingleWalletCurrency(val currency: Currency?) : MultiWallet() data class TryToRemoveWallet(val currency: Currency) : MultiWallet() data class RemoveWallet(val currency: Currency) : MultiWallet() diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt index 12d2923899..7432f3fc41 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/MultiWalletReducer.kt @@ -11,9 +11,6 @@ class MultiWalletReducer { is WalletAction.MultiWallet.SelectWallet -> { state.copy(selectedCurrency = action.currency) } - is WalletAction.MultiWallet.SetSingleWalletCurrency -> { - state.copy(selectedCurrency = action.currency) - } is WalletAction.MultiWallet.TryToRemoveWallet -> state is WalletAction.MultiWallet.AddMissingDerivations -> state.copy( missingDerivations = action.blockchains, 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 66d82deb98..bf7d468298 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 @@ -95,6 +95,11 @@ private fun internalReduce(action: Action, state: AppState, appStateHolder: AppS is WalletAction.WalletStoresChanged -> { newState = newState.copy( walletsStores = action.walletStores, + selectedCurrency = findSelectedCurrency( + walletsStores = action.walletStores, + currentSelectedCurrency = newState.selectedCurrency, + isMultiWalletAllowed = newState.isMultiwalletAllowed, + ), ) } is WalletAction.TotalFiatBalanceChanged -> { @@ -103,14 +108,7 @@ private fun internalReduce(action: Action, state: AppState, appStateHolder: AppS ) } is WalletAction.LoadData.Success -> { - newState = newState.copy( - state = ProgressState.Done, - selectedCurrency = findSelectedCurrency( - walletsStores = newState.walletsStores, - currentSelectedCurrency = newState.selectedCurrency, - isMultiWalletAllowed = newState.isMultiwalletAllowed, - ), - ) + newState = newState.copy(state = ProgressState.Done) } is WalletAction.UpdateCanSaveUserWallets -> { newState = newState.copy(canSaveUserWallets = action.canSaveUserWallets)