Updated on 2026-08-14

This commit is contained in:
Tangem 2023-05-17 22:32:28 +03:00
parent 2bab9057fa
commit 5c2b6f0502
4 changed files with 36 additions and 16 deletions

View file

@ -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())

View file

@ -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()

View file

@ -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,

View file

@ -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)