Updated on 2026-08-14

This commit is contained in:
Tangem 2022-12-07 16:45:43 +03:00
parent 3d8aba391f
commit 922b302cb1
14 changed files with 159 additions and 55 deletions

View file

@ -9,7 +9,6 @@ import com.tangem.tap.common.extensions.onUserWalletSelected
import com.tangem.tap.common.redux.AppState
import com.tangem.tap.common.redux.navigation.AppScreen
import com.tangem.tap.common.redux.navigation.NavigationAction
import com.tangem.tap.domain.model.UserWallet
import com.tangem.tap.domain.model.builders.UserWalletBuilder
import com.tangem.tap.preferencesStorage
import com.tangem.tap.scope
@ -67,7 +66,7 @@ internal class SaveWalletMiddleware {
scope.launch {
val userWallet = UserWalletBuilder(scanResponse)
.setBackupCardsIds(backupCardsIds = state.backupInfo?.backupCardsIds)
.backupCardsIds(state.backupInfo?.backupCardsIds)
.build()
saveAccessCodeIfNeeded(state.backupInfo?.accessCode, userWallet.cardsInWallet)

View file

@ -11,12 +11,12 @@ data class UserWalletModel(
) {
sealed interface Type {
data class SingleCurrency(
val blockchainName: String?,
val blockchainName: String? = null,
) : Type
data class MultiCurrency(
val tokensCount: Int,
val cardsInWallet: Int,
val tokensCount: Int = 0,
) : Type
}
}

View file

@ -6,7 +6,6 @@ import com.tangem.common.flatMap
import com.tangem.common.map
import com.tangem.domain.common.ScanResponse
import com.tangem.domain.common.util.UserWalletId
import com.tangem.tap.*
import com.tangem.tap.common.extensions.dispatchOnMain
import com.tangem.tap.common.extensions.onUserWalletSelected
import com.tangem.tap.common.redux.AppState
@ -16,6 +15,12 @@ import com.tangem.tap.domain.model.UserWallet
import com.tangem.tap.domain.model.WalletStoreModel
import com.tangem.tap.domain.model.builders.UserWalletBuilder
import com.tangem.tap.domain.scanCard.ScanCardProcessor
import com.tangem.tap.preferencesStorage
import com.tangem.tap.scope
import com.tangem.tap.store
import com.tangem.tap.totalFiatBalanceCalculator
import com.tangem.tap.userWalletsListManager
import com.tangem.tap.walletStoresManager
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.rekotlin.Middleware
@ -86,26 +91,7 @@ internal class WalletSelectorMiddleware {
scope.launch {
val updatedWallet = state.wallets
.find { it.id == walletId.stringValue }
?.let { wallet ->
wallet.copy(
type = when (val type = wallet.type) {
is UserWalletModel.Type.MultiCurrency -> type.copy(
tokensCount = walletStores.flatMap { it.walletsData }.size,
)
is UserWalletModel.Type.SingleCurrency -> type.copy(
blockchainName = walletStores
.firstOrNull()
?.blockchainNetwork
?.blockchain
?.fullName,
)
},
fiatBalance = totalFiatBalanceCalculator.calculate(
prevAmount = wallet.fiatBalance.amount,
walletStores = walletStores,
),
)
}
?.updateWalletStoresAndCalculateFiatBalance(walletStores)
if (updatedWallet != null) {
store.dispatchOnMain(WalletSelectorAction.BalanceLoaded(updatedWallet))
@ -138,6 +124,7 @@ internal class WalletSelectorMiddleware {
val selectedWallet = userWalletsListManager.selectedUserWallet.first()
val isSavedWalletSelected = userWallet == selectedWallet
store.dispatchOnMain(WalletSelectorAction.AddWallet.Success)
if (isSavedWalletSelected) {
store.dispatchOnMain(NavigationAction.PopBackTo())
store.onUserWalletSelected(selectedWallet)
@ -209,4 +196,27 @@ internal class WalletSelectorMiddleware {
},
)
}
private suspend fun UserWalletModel.updateWalletStoresAndCalculateFiatBalance(
walletStores: List<WalletStoreModel>,
): UserWalletModel {
return this.copy(
type = when (type) {
is UserWalletModel.Type.MultiCurrency -> type.copy(
tokensCount = walletStores.flatMap { it.walletsData }.size,
)
is UserWalletModel.Type.SingleCurrency -> type.copy(
blockchainName = walletStores
.firstOrNull()
?.blockchainNetwork
?.blockchain
?.fullName,
)
},
fiatBalance = totalFiatBalanceCalculator.calculate(
prevAmount = fiatBalance.amount,
walletStores = walletStores,
),
)
}
}

View file

@ -93,15 +93,12 @@ internal object WalletSelectorReducer {
private fun UserWallet.getType(): UserWalletModel.Type {
return if (scanResponse.card.isMultiwalletAllowed) {
UserWalletModel.Type.MultiCurrency(
tokensCount = 0,
cardsInWallet = (scanResponse.card.backupStatus as? CardDTO.BackupStatus.Active)
?.cardCount?.inc()
?: 1,
)
} else {
UserWalletModel.Type.SingleCurrency(
blockchainName = null,
)
UserWalletModel.Type.SingleCurrency()
}
}
}