Updated on 2026-08-14

This commit is contained in:
Tangem 2021-11-03 10:28:03 +03:00
parent 8dd2e578b4
commit 66217c1493
2 changed files with 24 additions and 34 deletions

View file

@ -132,12 +132,31 @@ data class WalletState(
fun replaceWalletInWallets(walletData: WalletData?): List<WalletData> {
if (walletData == null) return wallets
return wallets.filter { it.currencyData.currency != walletData.currencyData.currency } + walletData
var changed = false
val updatedWallets = wallets.map {
if (it.currencyData.currency == walletData.currencyData.currency) {
changed = true
walletData
} else {
it
}
}
return if (changed) updatedWallets else wallets + walletData
}
fun replaceSomeWallets(newWallets: List<WalletData>): List<WalletData> {
val currencies = newWallets.map { it.currencyData.currency }
return wallets.filter { !currencies.contains(it.currencyData.currency) } + newWallets
val remainingWallets: MutableList<WalletData> = newWallets.toMutableList()
val updatedWallets = wallets.map { wallet ->
val newWallet = newWallets
.firstOrNull { wallet.currencyData.currency == it.currencyData.currency }
if (newWallet == null) {
wallet
} else {
remainingWallets.remove(newWallet)
newWallet
}
}
return updatedWallets + remainingWallets
}
fun addWalletManagers(newWalletManagers: List<WalletManager>): WalletState {

View file

@ -18,7 +18,6 @@ import com.tangem.tap.features.wallet.ui.BalanceStatus
import com.tangem.tap.store
import com.tangem.wallet.R
import kotlinx.android.synthetic.main.item_currency_wallet.view.*
import java.math.BigDecimal
class WalletAdapter
: ListAdapter<WalletData, WalletAdapter.WalletsViewHolder>(DiffUtilCallback) {
@ -28,36 +27,8 @@ class WalletAdapter
}
fun submitList(list: List<WalletData>, primaryBlockchain: Blockchain?, primaryToken: Token? = null) {
val listModified = list.toMutableList()
val primaryBlockchainWallet = when (
val index = listModified.indexOfFirst {
(it.currency as? Currency.Blockchain)?.blockchain == primaryBlockchain
}
) {
-1 -> null
else -> listModified.removeAt(index)
}
val primaryTokenWallet = if (primaryToken == null) null else when (
val index = listModified.indexOfFirst { (it.currency as? Currency.Token)?.token == primaryToken }
) {
-1 -> null
else -> listModified.removeAt(index)
}
if (list.all { it.currencyData.fiatAmountFormatted == null }) {
val sortedList = listOfNotNull(primaryBlockchainWallet, primaryTokenWallet) + listModified
super.submitList(sortedList)
return
}
val sorted = listModified.sortedWith(
compareByDescending<WalletData> { it.currencyData.fiatAmount ?: BigDecimal.ZERO }
.thenBy { it.currencyData.currency }
)
val sortedList = listOfNotNull(primaryBlockchainWallet, primaryTokenWallet) + sorted
super.submitList(sortedList)
// We used this method to sort the list of currencies. Sorting is disabled for now.
super.submitList(list)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WalletsViewHolder {