Updated on 2026-08-14

This commit is contained in:
Tangem 2025-10-27 13:11:06 +05:00
parent d4e8d3f45f
commit 0334e93ec2
13 changed files with 145 additions and 118 deletions

View file

@ -2,6 +2,7 @@ package com.tangem.data.swap
import com.tangem.data.common.currency.ResponseCryptoCurrenciesFactory
import com.tangem.data.common.currency.UserTokensResponseFactory
import com.tangem.data.common.network.NetworkFactory
import com.tangem.data.swap.converter.transaction.SavedSwapStatusConverter
import com.tangem.data.swap.converter.transaction.SavedSwapTransactionConverter
import com.tangem.data.swap.converter.transaction.SavedSwapTransactionListConverter
@ -31,11 +32,15 @@ import kotlinx.coroutines.flow.flowOn
internal class DefaultSwapTransactionRepository(
private val appPreferencesStore: AppPreferencesStore,
private val responseCryptoCurrenciesFactory: ResponseCryptoCurrenciesFactory,
private val networkFactory: NetworkFactory,
private val dispatchers: CoroutineDispatcherProvider,
) : SwapTransactionRepository {
private val listConverter by lazy(LazyThreadSafetyMode.NONE) {
SavedSwapTransactionListConverter(responseCryptoCurrenciesFactory = responseCryptoCurrenciesFactory)
SavedSwapTransactionListConverter(
responseCryptoCurrenciesFactory = responseCryptoCurrenciesFactory,
networkFactory = networkFactory,
)
}
private val converter by lazy(LazyThreadSafetyMode.NONE) {
SavedSwapTransactionConverter(responseCryptoCurrenciesFactory = responseCryptoCurrenciesFactory)
@ -124,52 +129,25 @@ internal class DefaultSwapTransactionRepository(
}
}.flowOn(dispatchers.default)
override suspend fun removeTransaction(
userWalletId: UserWalletId,
fromCryptoCurrency: CryptoCurrency,
toCryptoCurrency: CryptoCurrency,
txId: String,
) {
override suspend fun removeTransaction(userWalletId: UserWalletId, txId: String) {
clearTransactionsStatuses(txId = txId)
appPreferencesStore.editData { mutablePreferences ->
val savedList: List<SwapTransactionListDTO>? = mutablePreferences.getObjectList(
key = PreferencesKeys.SWAP_TRANSACTIONS_KEY,
)
val tokenTransactions = savedList
?.firstOrNull {
it.checkId(
checkUserWalletId = userWalletId,
fromCurrencyId = fromCryptoCurrency.id,
toCurrencyId = toCryptoCurrency.id,
)
}
?.transactions
?.filterNot { it.txId == txId }
?.asSequence()
?.map {
it.copy(transactions = it.transactions.filterNot { it.txId == txId })
}?.filterNot { it.transactions.isEmpty() }
?.toList()
val editedList =
if (tokenTransactions.isNullOrEmpty()) {
savedList?.filterNot {
it.checkId(
checkUserWalletId = userWalletId,
fromCurrencyId = fromCryptoCurrency.id,
toCurrencyId = toCryptoCurrency.id,
)
}
} else {
savedList.updateList(
userWalletId = userWalletId,
fromCryptoCurrency = fromCryptoCurrency,
toCryptoCurrency = toCryptoCurrency,
transactions = tokenTransactions,
)
}
if (editedList.isNullOrEmpty()) {
if (tokenTransactions.isNullOrEmpty()) {
mutablePreferences.remove(key = PreferencesKeys.SWAP_TRANSACTIONS_KEY)
} else {
mutablePreferences.setObject(
key = PreferencesKeys.SWAP_TRANSACTIONS_KEY,
value = editedList,
value = tokenTransactions,
)
}
}

View file

@ -1,11 +1,16 @@
package com.tangem.data.swap.converter.transaction
import com.tangem.blockchain.common.Blockchain
import com.tangem.blockchainsdk.utils.fromNetworkId
import com.tangem.data.common.currency.ResponseCryptoCurrenciesFactory
import com.tangem.data.common.currency.UserTokensResponseFactory
import com.tangem.data.common.network.NetworkFactory
import com.tangem.data.swap.models.SwapStatusDTO
import com.tangem.data.swap.models.SwapTransactionDTO
import com.tangem.data.swap.models.SwapTransactionListDTO
import com.tangem.datasource.api.tangemTech.models.UserTokensResponse
import com.tangem.domain.models.currency.CryptoCurrency
import com.tangem.domain.models.network.Network
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.swap.models.SwapTransactionListModel
@ -13,6 +18,7 @@ import com.tangem.utils.converter.Converter
internal class SavedSwapTransactionListConverter(
private val responseCryptoCurrenciesFactory: ResponseCryptoCurrenciesFactory,
private val networkFactory: NetworkFactory,
) : Converter<SwapTransactionListModel, SwapTransactionListDTO> {
private val userTokensResponseFactory = UserTokensResponseFactory()
@ -45,13 +51,17 @@ internal class SavedSwapTransactionListConverter(
return if (fromToken == null || toToken == null) {
null
} else {
val fromNetwork = createSwapTransactionNetwork(fromToken, userWallet) ?: return null
val fromCryptoCurrency = responseCryptoCurrenciesFactory.createCurrency(
responseToken = fromToken,
userWallet = userWallet,
network = fromNetwork,
) ?: return null
val toNetwork = createSwapTransactionNetwork(fromToken, userWallet) ?: return null
val toCryptoCurrency = responseCryptoCurrenciesFactory.createCurrency(
responseToken = toToken,
userWallet = userWallet,
network = toNetwork,
) ?: return null
return SwapTransactionListModel(
@ -83,4 +93,22 @@ internal class SavedSwapTransactionListConverter(
toTokensResponse = userTokensResponseFactory.createResponseToken(currency = toCryptoCurrency, accountId = null),
transactions = tokenTransactions,
)
private fun createSwapTransactionNetwork(token: UserTokensResponse.Token, userWallet: UserWallet): Network? {
val blockchain = Blockchain.fromNetworkId(token.networkId) ?: return null
return if (token.derivationPath == null) {
networkFactory.create(
blockchain = blockchain,
derivationPath = Network.DerivationPath.None,
userWallet = userWallet,
)
} else {
networkFactory.create(
blockchain = blockchain,
extraDerivationPath = token.derivationPath,
userWallet = userWallet,
)
}
}
}

View file

@ -2,6 +2,7 @@ package com.tangem.data.swap.di
import com.squareup.moshi.Moshi
import com.tangem.data.common.currency.ResponseCryptoCurrenciesFactory
import com.tangem.data.common.network.NetworkFactory
import com.tangem.data.express.converter.ExpressErrorConverter
import com.tangem.data.swap.DefaultSwapErrorResolver
import com.tangem.data.swap.DefaultSwapRepositoryV2
@ -66,11 +67,13 @@ internal object SwapDataModule {
fun provideSwapTransactionRepository(
appPreferencesStore: AppPreferencesStore,
responseCryptoCurrenciesFactory: ResponseCryptoCurrenciesFactory,
networkFactory: NetworkFactory,
dispatchers: CoroutineDispatcherProvider,
): SwapTransactionRepository {
return DefaultSwapTransactionRepository(
appPreferencesStore = appPreferencesStore,
responseCryptoCurrenciesFactory = responseCryptoCurrenciesFactory,
networkFactory = networkFactory,
dispatchers = dispatchers,
)
}