From 1995610c6a778f219dcfd9c12061e3663e9bc3e1 Mon Sep 17 00:00:00 2001 From: Tangem Date: Tue, 26 Dec 2023 15:10:32 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../swap/ui/ChooseProviderBottomSheet.kt | 2 +- .../viewmodels/ExchangeStatusFactory.kt | 53 ++++++++++++------- .../viewmodels/TokenDetailsViewModel.kt | 5 ++ 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/ChooseProviderBottomSheet.kt b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/ChooseProviderBottomSheet.kt index 07091dfb12..36c705d5fb 100644 --- a/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/ChooseProviderBottomSheet.kt +++ b/features/swap/presentation/src/main/java/com/tangem/feature/swap/ui/ChooseProviderBottomSheet.kt @@ -42,7 +42,7 @@ private fun ChooseProviderBottomSheetContent(content: ChooseProviderBottomSheetC style = TangemTheme.typography.subtitle1, color = TangemTheme.colors.text.primary1, modifier = Modifier - .padding(top = TangemTheme.dimens.spacing10) + .padding(top = TangemTheme.dimens.spacing10), ) Text( text = stringResource(R.string.express_choose_providers_subtitle), diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt index 4c78d11155..8ff627eaec 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/ExchangeStatusFactory.kt @@ -18,7 +18,9 @@ import com.tangem.feature.swap.domain.models.domain.ExchangeStatus import com.tangem.feature.swap.domain.models.domain.ExchangeStatusModel import com.tangem.feature.swap.domain.models.domain.SavedSwapTransactionListModel import com.tangem.feature.tokendetails.presentation.tokendetails.state.SwapTransactionsState +import com.tangem.feature.tokendetails.presentation.tokendetails.state.TokenDetailsState import com.tangem.feature.tokendetails.presentation.tokendetails.state.factory.TokenDetailsSwapTransactionsStateConverter +import com.tangem.feature.tokendetails.presentation.tokendetails.ui.components.exchange.ExchangeStatusBottomSheetConfig import com.tangem.utils.coroutines.CoroutineDispatcherProvider import kotlinx.collections.immutable.PersistentList import kotlinx.collections.immutable.persistentListOf @@ -41,6 +43,7 @@ internal class ExchangeStatusFactory( private val clickIntents: TokenDetailsClickIntents, private val appCurrencyProvider: Provider, private val analyticsEventsHandlerProvider: Provider, + private val currentStateProvider: Provider, private val userWalletId: UserWalletId, private val cryptoCurrency: CryptoCurrency, ) { @@ -64,6 +67,27 @@ internal class ExchangeStatusFactory( ) } + suspend fun removeTransactionOnBottomSheetClosed(): TokenDetailsState { + val state = currentStateProvider() + val bottomSheetConfig = state.bottomSheetConfig?.content as? ExchangeStatusBottomSheetConfig ?: return state + val selectedTx = bottomSheetConfig.value + + return if (selectedTx.activeStatus.isTerminal()) { + swapTransactionRepository.removeTransaction( + userWalletId = userWalletId, + fromCryptoCurrencyId = selectedTx.fromCryptoCurrencyId, + toCryptoCurrencyId = selectedTx.toCryptoCurrencyId, + txId = selectedTx.txId, + ) + val filteredTxs = state.swapTxs + .filterNot { it.txId == selectedTx.txId } + .toPersistentList() + state.copy(swapTxs = filteredTxs) + } else { + state + } + } + private fun getWalletCryptoCurrencies() = flow { val selectedWallet = getSelectedWalletSyncUseCase().fold( ifLeft = { null }, @@ -80,14 +104,16 @@ internal class ExchangeStatusFactory( suspend fun updateSwapTxStatuses(swapTxList: PersistentList) = withContext(dispatchers.io) { swapTxList.map { tx -> async { - val statusModel = getExchangeStatus(tx.txId) - swapTransactionsStateConverter - .updateTxStatus(tx, statusModel) - .removeIfFinished(statusModel?.status) + if (tx.activeStatus.isTerminal()) { + tx + } else { + val statusModel = getExchangeStatus(tx.txId) + swapTransactionsStateConverter + .updateTxStatus(tx, statusModel) + } } } .awaitAll() - .filterNotNull() .toPersistentList() } @@ -130,21 +156,8 @@ internal class ExchangeStatusFactory( ) } - private suspend fun SwapTransactionsState.removeIfFinished(status: ExchangeStatus?) = when (status) { - null -> null // not found - ExchangeStatus.Refunded, ExchangeStatus.Finished -> { - swapTransactionRepository.removeTransaction( - userWalletId = userWalletId, - fromCryptoCurrencyId = fromCryptoCurrencyId, - toCryptoCurrencyId = toCryptoCurrencyId, - txId = txId, - ) - null - } - else -> { - this - } - } + private fun ExchangeStatus?.isTerminal() = + this == ExchangeStatus.Refunded || this == ExchangeStatus.Finished || this == ExchangeStatus.Cancelled private fun toAnalyticStatus(status: ExchangeStatus?): ExchangeAnalyticsStatus? { return when (status) { diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt index fee7ee1a64..b2d4ba321e 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsViewModel.kt @@ -548,6 +548,11 @@ internal class TokenDetailsViewModel @Inject constructor( } override fun onDismissBottomSheet() { + if (uiState.bottomSheetConfig?.content is ExchangeStatusBottomSheetConfig) { + viewModelScope.launch(dispatchers.main) { + uiState = exchangeStatusFactory.removeTransactionOnBottomSheetClosed() + } + } uiState = stateFactory.getStateWithClosedBottomSheet() }