Updated on 2026-08-14

This commit is contained in:
Tangem 2022-12-28 11:45:28 +04:00
commit fa41036fca
6 changed files with 32 additions and 27 deletions

View file

@ -106,11 +106,7 @@ internal class DefaultWalletManagersRepository(
override suspend fun delete(userWalletIds: List<UserWalletId>): CompletionResult<Unit> = catching {
walletManagersStorage.update { prevManagers ->
prevManagers.apply {
userWalletIds.forEach { userWalletId ->
remove(userWalletId)
}
}
prevManagers.filterKeys { it !in userWalletIds } as HashMap<UserWalletId, List<WalletManager>>
}
}

View file

@ -305,7 +305,7 @@ class WalletMiddleware {
val reduxWalletStores = wallStores.mapToReduxModels(state.isMultiwalletAllowed)
store.dispatchOnMain(
WalletAction.WalletStoresChanged.UpdateWalletStores(
reduxWalletStores = reduxWalletStores.toList(),
reduxWalletStores = reduxWalletStores,
),
)
}

View file

@ -31,7 +31,7 @@ class MultiWalletReducer {
fun reduce(action: WalletAction.MultiWallet, state: WalletState): WalletState {
return when (action) {
is WalletAction.MultiWallet.AddBlockchains -> {
val walletStores: List<WalletStore> = action.blockchains.mapNotNull { blockchain ->
val walletStores: List<WalletStore> = action.blockchains.map { blockchain ->
val walletManager = action.walletManagers.firstOrNull {
it.wallet.blockchain == blockchain.blockchain &&
(it.wallet.publicKey.derivationPath?.rawPath == blockchain.derivationPath)
@ -65,10 +65,10 @@ class MultiWalletReducer {
)
}
val selectedCurrency = if (!state.isMultiwalletAllowed) {
walletStores.firstOrNull()?.walletsData?.firstOrNull()?.currency
val selectedCurrency = if (state.isMultiwalletAllowed) {
state.selectedCurrency
} else {
state.selectedWalletData?.currency
walletStores.firstOrNull()?.walletsData?.firstOrNull()?.currency
}
state.copy(
walletsStores = walletStores,

View file

@ -20,7 +20,6 @@ import com.tangem.tap.domain.extensions.isMultiwalletAllowed
import com.tangem.tap.domain.getFirstToken
import com.tangem.tap.domain.tokens.models.BlockchainNetwork
import com.tangem.tap.features.wallet.models.Currency
import com.tangem.tap.features.wallet.models.TotalBalance
import com.tangem.tap.features.wallet.models.WalletRent
import com.tangem.tap.features.wallet.redux.AddressData
import com.tangem.tap.features.wallet.redux.Artwork
@ -36,7 +35,6 @@ import com.tangem.tap.features.wallet.redux.replaceSomeWalletsData
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.store
import org.rekotlin.Action
import timber.log.Timber
import java.math.BigDecimal
@ -344,8 +342,9 @@ private fun internalReduce(action: Action, state: AppState, appStateHolder: AppS
card.settings.isBackupAllowed &&
card.backupStatus == CardDTO.BackupStatus.NoBackup,
walletCardsCount = card.findCardsCount(),
walletsStores = newState.walletsStores,
totalBalance = if (isMultiCurrency) {
TotalBalance(ProgressState.Loading, BigDecimal.ZERO, store.state.globalState.appCurrency)
newState.totalBalance
} else {
null
},
@ -362,13 +361,13 @@ private fun internalReduce(action: Action, state: AppState, appStateHolder: AppS
)
}
is WalletAction.LoadData.Success -> {
val selectedCurrency = if (!newState.isMultiwalletAllowed) {
val selectedCurrency = if (newState.isMultiwalletAllowed) {
newState.selectedCurrency
} else {
newState.walletsStores.firstOrNull()
?.walletsData
?.firstOrNull()
?.currency
} else {
newState.selectedWalletData?.currency
}
newState = newState.copy(

View file

@ -8,8 +8,10 @@ import com.tangem.tap.store
import com.tangem.tap.userWalletsListManager
import com.tangem.tap.walletStoresManager
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
@ -21,8 +23,10 @@ internal class WalletViewModel : ViewModel() {
private fun bootstrapSelectedWalletStoresChanges() {
userWalletsListManager.selectedUserWallet
.flatMapLatest { selectedWallet ->
walletStoresManager.get(selectedWallet.walletId)
.map { it.walletId }
.distinctUntilChanged()
.flatMapLatest { selectedUserWalletId ->
walletStoresManager.get(selectedUserWalletId)
}
.onEach { walletStores ->
store.dispatch(WalletAction.WalletStoresChanged(walletStores))

View file

@ -277,26 +277,32 @@ internal class WalletSelectorMiddleware {
.flatMap { walletStoresManager.delete(userWalletsIds) }
.flatMap { deleteAccessCodes(userWalletsIds) }
.doOnSuccess {
val selectedWallet = userWalletsListManager.selectedUserWalletSync
val selectedUserWallet = userWalletsListManager.selectedUserWalletSync
when {
selectedWallet == null -> {
selectedUserWallet == null -> {
store.dispatchOnMain(NavigationAction.PopBackTo(AppScreen.Welcome))
}
currentSelectedWalletId != selectedWallet.walletId -> {
store.onUserWalletSelected(selectedWallet)
currentSelectedWalletId != selectedUserWallet.walletId -> {
store.onUserWalletSelected(selectedUserWallet)
}
}
}
}
private suspend fun deleteAccessCodes(userWalletsIds: List<UserWalletId>): CompletionResult<Unit> {
val cardsIds = userWalletsListManager.userWallets.firstOrNull().orEmpty()
.asSequence()
.filter { it.walletId in userWalletsIds }
.flatMap { it.cardsInWallet }
val cardsIds = userWalletsListManager.userWallets.firstOrNull()
?.asSequence()
?.filter { it.walletId in userWalletsIds }
?.flatMap { it.cardsInWallet }
?.toSet()
return tangemSdkManager.deleteSavedUserCodes(cardsIds.toSet())
return if (cardsIds.isNullOrEmpty()) {
CompletionResult.Success(Unit)
} else {
tangemSdkManager.deleteSavedUserCodes(cardsIds.toSet())
}
}
private suspend fun UserWalletModel.updateWalletStoresAndCalculateFiatBalance(