Updated on 2026-08-14
This commit is contained in:
parent
3bda096aad
commit
704ebd7608
3 changed files with 43 additions and 42 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package com.tangem.tap.domain.walletCurrencies.implementation
|
||||
|
||||
import com.tangem.blockchain.common.Blockchain
|
||||
import com.tangem.blockchain.common.DerivationStyle
|
||||
import com.tangem.common.CompletionResult
|
||||
import com.tangem.common.flatMap
|
||||
|
|
@ -54,28 +55,17 @@ internal class DefaultWalletCurrenciesManager(
|
|||
currenciesToAdd: List<Currency>,
|
||||
): CompletionResult<Unit> = withContext(Dispatchers.Default) {
|
||||
val card = userWallet.scanResponse.card
|
||||
val updatedBlockchainNetworks = currenciesToAdd
|
||||
.addMissingBlockchains(card)
|
||||
.toBlockchainNetworks()
|
||||
val newCurrencies = (getSavedCurrencies(userWallet.walletId) + currenciesToAdd)
|
||||
.addMissingBlockchains(card)
|
||||
|
||||
updateWalletStores(userWallet, updatedBlockchainNetworks)
|
||||
updateWalletStores(userWallet, newCurrencies.toBlockchainNetworks())
|
||||
.map {
|
||||
saveUserCurrencies(card, newCurrencies)
|
||||
}
|
||||
.flatMap {
|
||||
val updatedBlockchains = updatedBlockchainNetworks
|
||||
.map { it.blockchain }
|
||||
val updatedWalletStores = walletStoresRepository.get(userWallet.walletId)
|
||||
.firstOrNull()
|
||||
?.filter { it.blockchain in updatedBlockchains }
|
||||
?: return@flatMap CompletionResult.Success(Unit)
|
||||
|
||||
walletAmountsRepository.updateAmountsForWalletStores(
|
||||
walletStores = updatedWalletStores,
|
||||
updateWalletStoresAmounts(
|
||||
userWallet = userWallet,
|
||||
fiatCurrency = appCurrencyProvider(),
|
||||
updatedBlockchains = currenciesToAdd.map { it.blockchain }.distinct(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -161,6 +151,10 @@ internal class DefaultWalletCurrenciesManager(
|
|||
return newCurrencies
|
||||
}
|
||||
|
||||
private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? {
|
||||
return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath
|
||||
}
|
||||
|
||||
private suspend fun updateWalletStores(
|
||||
userWallet: UserWallet,
|
||||
blockchainNetworks: List<BlockchainNetwork>,
|
||||
|
|
@ -184,7 +178,19 @@ internal class DefaultWalletCurrenciesManager(
|
|||
.fold()
|
||||
}
|
||||
|
||||
private fun findDerivationPath(currency: Currency, cardDerivationStyle: DerivationStyle?): String? {
|
||||
return currency.derivationPath ?: currency.blockchain.derivationPath(cardDerivationStyle)?.rawPath
|
||||
private suspend fun updateWalletStoresAmounts(
|
||||
userWallet: UserWallet,
|
||||
updatedBlockchains: List<Blockchain>,
|
||||
): CompletionResult<Unit> {
|
||||
val updatedWalletStores = walletStoresRepository.get(userWallet.walletId)
|
||||
.firstOrNull()
|
||||
?.filter { it.blockchain in updatedBlockchains }
|
||||
?: return CompletionResult.Success(Unit)
|
||||
|
||||
return walletAmountsRepository.updateAmountsForWalletStores(
|
||||
walletStores = updatedWalletStores,
|
||||
userWallet = userWallet,
|
||||
fiatCurrency = appCurrencyProvider(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -158,12 +158,12 @@ internal fun List<WalletDataModel>.updateWithUnreachable(): List<WalletDataModel
|
|||
}
|
||||
|
||||
internal fun List<WalletDataModel>.updateWithSelf(
|
||||
walletsData: List<WalletDataModel>,
|
||||
newWalletsData: List<WalletDataModel>,
|
||||
): List<WalletDataModel> {
|
||||
val oldWalletsData = this
|
||||
val updatedWalletsData = arrayListOf<WalletDataModel>()
|
||||
|
||||
walletsData.forEach { newWalletData ->
|
||||
newWalletsData.forEach { newWalletData ->
|
||||
val walletDataToUpdate = oldWalletsData.find(newWalletData::isSameWalletData)
|
||||
if (walletDataToUpdate != null) {
|
||||
updatedWalletsData.add(walletDataToUpdate.updateWithSelf(newWalletData))
|
||||
|
|
|
|||
|
|
@ -88,39 +88,34 @@ internal fun WalletStoreModel.updateWithRent(rent: WalletStoreModel.WalletRent?)
|
|||
)
|
||||
}
|
||||
|
||||
internal fun WalletStoreModel.isSameWalletStore(other: WalletStoreModel): Boolean {
|
||||
return this.blockchain == other.blockchain &&
|
||||
this.derivationPath == other.derivationPath
|
||||
}
|
||||
|
||||
private inline fun List<WalletStoreModel>.replaceWalletStores(
|
||||
walletStoresToUpdate: List<WalletStoreModel>,
|
||||
update: (walletStore: WalletStoreModel) -> WalletStoreModel,
|
||||
): List<WalletStoreModel> {
|
||||
val mutableStores = ArrayList<WalletStoreModel>(this)
|
||||
|
||||
walletStoresToUpdate
|
||||
.asSequence()
|
||||
.map { it.blockchain to it.derivationPath }
|
||||
.forEach { (blockchain, derivationPath) ->
|
||||
val index = mutableStores.indexOfFirst {
|
||||
it.blockchain == blockchain && it.derivationPath == derivationPath
|
||||
}
|
||||
val currentWalletStore = mutableStores[index]
|
||||
val updatedWalletStore = update(currentWalletStore)
|
||||
walletStoresToUpdate.forEach { walletStoreToUpdate ->
|
||||
val index = mutableStores.indexOfFirst(walletStoreToUpdate::isSameWalletStore)
|
||||
val currentWalletStore = mutableStores[index]
|
||||
val updatedWalletStore = update(currentWalletStore)
|
||||
|
||||
if (currentWalletStore != updatedWalletStore) {
|
||||
Timber.d(
|
||||
"""
|
||||
Update wallet store in storage
|
||||
|- User wallet ID: ${updatedWalletStore.userWalletId}
|
||||
|- Blockchain: ${updatedWalletStore.blockchain}
|
||||
""".trimIndent(),
|
||||
)
|
||||
if (currentWalletStore != updatedWalletStore) {
|
||||
Timber.d(
|
||||
"""
|
||||
Update wallet store in storage
|
||||
|- User wallet ID: ${updatedWalletStore.userWalletId}
|
||||
|- Blockchain: ${updatedWalletStore.blockchain}
|
||||
""".trimIndent(),
|
||||
)
|
||||
|
||||
mutableStores[index] = updatedWalletStore
|
||||
}
|
||||
mutableStores[index] = updatedWalletStore
|
||||
}
|
||||
}
|
||||
|
||||
return mutableStores
|
||||
}
|
||||
|
||||
internal fun WalletStoreModel.isSameWalletStore(other: WalletStoreModel): Boolean {
|
||||
return this.blockchain == other.blockchain &&
|
||||
this.derivationPath == other.derivationPath
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue