From 70d2f5ee06bcc1c91570d1c50e0467e2c90001a7 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 13 Sep 2023 13:34:38 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../intents/TxHistoryClickIntents.kt | 10 ------ .../tokens/GetCryptoCurrencyActionsUseCase.kt | 14 +++++--- .../domain/tokens/model/TokenActionsState.kt | 3 +- .../TokenDetailsLoadedTxHistoryConverter.kt | 4 +-- .../TokenDetailsLoadingTxHistoryConverter.kt | 4 +-- .../TokenDetailsTxHistoryItemFlowConverter.kt | 9 ++++-- .../viewmodels/TokenDetailsClickIntents.kt | 10 ++++-- .../viewmodels/TokenDetailsViewModel.kt | 10 +++--- .../state/factory/TokenActionsProvider.kt | 26 +++++++-------- .../WalletCryptoCurrencyActionsConverter.kt | 24 +++++++++----- .../state/factory/WalletStateFactory.kt | 16 +++++++--- .../WalletLoadingTxHistoryConverter.kt | 27 ++++++++++------ .../wallet/viewmodels/WalletClickIntents.kt | 15 ++++++--- .../wallet/viewmodels/WalletViewModel.kt | 32 +++++++++++-------- 14 files changed, 120 insertions(+), 84 deletions(-) delete mode 100644 core/ui/src/main/java/com/tangem/core/ui/components/transactions/intents/TxHistoryClickIntents.kt diff --git a/core/ui/src/main/java/com/tangem/core/ui/components/transactions/intents/TxHistoryClickIntents.kt b/core/ui/src/main/java/com/tangem/core/ui/components/transactions/intents/TxHistoryClickIntents.kt deleted file mode 100644 index 4abce062be..0000000000 --- a/core/ui/src/main/java/com/tangem/core/ui/components/transactions/intents/TxHistoryClickIntents.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.tangem.core.ui.components.transactions.intents - -interface TxHistoryClickIntents { - - fun onBuyClick() - - fun onReloadClick() - - fun onExploreClick() -} \ No newline at end of file diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCryptoCurrencyActionsUseCase.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCryptoCurrencyActionsUseCase.kt index 8f0f5499af..095b4f721e 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCryptoCurrencyActionsUseCase.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/GetCryptoCurrencyActionsUseCase.kt @@ -1,6 +1,7 @@ package com.tangem.domain.tokens import com.tangem.domain.exchange.RampStateManager +import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.TokenActionsState import com.tangem.domain.tokens.models.CryptoCurrency import com.tangem.domain.tokens.repository.MarketCryptoCurrencyRepository @@ -21,21 +22,24 @@ class GetCryptoCurrencyActionsUseCase( private val dispatchers: CoroutineDispatcherProvider, ) { - operator fun invoke(userWalletId: UserWalletId, cryptoCurrency: CryptoCurrency): Flow { + operator fun invoke( + userWalletId: UserWalletId, + cryptoCurrencyStatus: CryptoCurrencyStatus, + ): Flow { return flow { - val actionStates = createTokenActionsState(userWalletId, cryptoCurrency) + val actionStates = createTokenActionsState(userWalletId, cryptoCurrencyStatus) emit(actionStates) }.flowOn(dispatchers.io) } private suspend fun createTokenActionsState( userWalletId: UserWalletId, - cryptoCurrency: CryptoCurrency, + cryptoCurrencyStatus: CryptoCurrencyStatus, ): TokenActionsState { return TokenActionsState( walletId = userWalletId, - cryptoCurrencyId = cryptoCurrency.id, - states = createListOfActions(userWalletId, cryptoCurrency), + cryptoCurrencyStatus = cryptoCurrencyStatus, + states = createListOfActions(userWalletId, cryptoCurrencyStatus.currency), ) } diff --git a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenActionsState.kt b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenActionsState.kt index 81c3d9a53f..d755f0c68b 100644 --- a/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenActionsState.kt +++ b/domain/tokens/src/main/kotlin/com/tangem/domain/tokens/model/TokenActionsState.kt @@ -1,11 +1,10 @@ package com.tangem.domain.tokens.model -import com.tangem.domain.tokens.models.CryptoCurrency import com.tangem.domain.wallets.models.UserWalletId data class TokenActionsState( val walletId: UserWalletId, - val cryptoCurrencyId: CryptoCurrency.ID, + val cryptoCurrencyStatus: CryptoCurrencyStatus, val states: List, ) { diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadedTxHistoryConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadedTxHistoryConverter.kt index f9146ee1d3..4c5fbc9fa2 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadedTxHistoryConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadedTxHistoryConverter.kt @@ -3,17 +3,17 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory. import androidx.paging.PagingData import arrow.core.Either import com.tangem.common.Provider -import com.tangem.core.ui.components.transactions.intents.TxHistoryClickIntents import com.tangem.core.ui.components.transactions.state.TxHistoryState import com.tangem.domain.txhistory.models.TxHistoryItem import com.tangem.domain.txhistory.models.TxHistoryListError import com.tangem.feature.tokendetails.presentation.tokendetails.state.TokenDetailsState +import com.tangem.feature.tokendetails.presentation.tokendetails.viewmodels.TokenDetailsClickIntents import com.tangem.utils.converter.Converter import kotlinx.coroutines.flow.Flow internal class TokenDetailsLoadedTxHistoryConverter( private val currentStateProvider: Provider, - private val clickIntents: TxHistoryClickIntents, + private val clickIntents: TokenDetailsClickIntents, symbol: String, decimals: Int, ) : Converter>>, TokenDetailsState> { diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadingTxHistoryConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadingTxHistoryConverter.kt index 9f98c1ccf3..2f82ffe2ce 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadingTxHistoryConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsLoadingTxHistoryConverter.kt @@ -3,17 +3,17 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory. import androidx.paging.PagingData import arrow.core.Either import com.tangem.common.Provider -import com.tangem.core.ui.components.transactions.intents.TxHistoryClickIntents import com.tangem.core.ui.components.transactions.state.TransactionState import com.tangem.core.ui.components.transactions.state.TxHistoryState import com.tangem.domain.txhistory.models.TxHistoryStateError import com.tangem.feature.tokendetails.presentation.tokendetails.state.TokenDetailsState +import com.tangem.feature.tokendetails.presentation.tokendetails.viewmodels.TokenDetailsClickIntents import com.tangem.utils.converter.Converter import kotlinx.coroutines.flow.update internal class TokenDetailsLoadingTxHistoryConverter( private val currentStateProvider: Provider, - private val clickIntents: TxHistoryClickIntents, + private val clickIntents: TokenDetailsClickIntents, ) : Converter, TokenDetailsState> { override fun convert(value: Either): TokenDetailsState { diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryItemFlowConverter.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryItemFlowConverter.kt index a8f2d95f25..d1de3a2c01 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryItemFlowConverter.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/state/factory/txhistory/TokenDetailsTxHistoryItemFlowConverter.kt @@ -3,13 +3,13 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.state.factory. import android.text.format.DateUtils import androidx.paging.* import com.tangem.common.Provider -import com.tangem.core.ui.components.transactions.intents.TxHistoryClickIntents import com.tangem.core.ui.components.transactions.state.TransactionState import com.tangem.core.ui.components.transactions.state.TxHistoryState import com.tangem.core.ui.components.transactions.state.TxHistoryState.TxHistoryItemState import com.tangem.core.ui.extensions.TextReference import com.tangem.domain.txhistory.models.TxHistoryItem import com.tangem.feature.tokendetails.presentation.tokendetails.state.TokenDetailsState +import com.tangem.feature.tokendetails.presentation.tokendetails.viewmodels.TokenDetailsClickIntents import com.tangem.features.tokendetails.impl.R import com.tangem.utils.converter.Converter import com.tangem.utils.extensions.isToday @@ -18,7 +18,10 @@ import com.tangem.utils.toBriefAddressFormat import com.tangem.utils.toFormattedCurrencyString import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.* +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.update import org.joda.time.DateTime import org.joda.time.DateTimeZone import org.joda.time.format.DateTimeFormatterBuilder @@ -29,7 +32,7 @@ internal class TokenDetailsTxHistoryItemFlowConverter( private val currentStateProvider: Provider, private val symbol: String, private val decimals: Int, - private val clickIntents: TxHistoryClickIntents, + private val clickIntents: TokenDetailsClickIntents, ) : Converter>, TxHistoryState> { /** Example, 2 Aug, 2023 */ diff --git a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsClickIntents.kt b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsClickIntents.kt index 0e1eab9fc1..52b554feb9 100644 --- a/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsClickIntents.kt +++ b/features/tokendetails/impl/src/main/kotlin/com/tangem/feature/tokendetails/presentation/tokendetails/viewmodels/TokenDetailsClickIntents.kt @@ -1,8 +1,6 @@ package com.tangem.feature.tokendetails.presentation.tokendetails.viewmodels -import com.tangem.core.ui.components.transactions.intents.TxHistoryClickIntents - -interface TokenDetailsClickIntents : TxHistoryClickIntents { +interface TokenDetailsClickIntents { fun onBackClick() @@ -21,4 +19,10 @@ interface TokenDetailsClickIntents : TxHistoryClickIntents { fun onHideConfirmed() fun onRefreshSwipe() + + fun onBuyClick() + + fun onReloadClick() + + fun onExploreClick() } \ No newline at end of file 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 599c54b7c1..baf1dfaa9f 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 @@ -88,12 +88,11 @@ internal class TokenDetailsViewModel @Inject constructor( private fun updateContent(selectedWallet: UserWallet) { updateMarketPrice(selectedWallet = selectedWallet) - updateButtons(userWalletId = selectedWallet.walletId, currency = cryptoCurrency) updateTxHistory() } - private fun updateButtons(userWalletId: UserWalletId, currency: CryptoCurrency) { - getCryptoCurrencyActionsUseCase(userWalletId = userWalletId, cryptoCurrency = currency) + private fun updateButtons(userWalletId: UserWalletId, currencyStatus: CryptoCurrencyStatus) { + getCryptoCurrencyActionsUseCase(userWalletId = userWalletId, cryptoCurrencyStatus = currencyStatus) .distinctUntilChanged() .onEach { uiState = stateFactory.getManageButtonsState(actions = it.states) } .flowOn(dispatchers.io) @@ -108,7 +107,10 @@ internal class TokenDetailsViewModel @Inject constructor( .distinctUntilChanged() .onEach { either -> uiState = stateFactory.getCurrencyLoadedBalanceState(either) - either.onRight { status -> cryptoCurrencyStatus = status } + either.onRight { status -> + cryptoCurrencyStatus = status + updateButtons(userWalletId = selectedWallet.walletId, currencyStatus = status) + } } .flowOn(dispatchers.io) .launchIn(viewModelScope) diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/TokenActionsProvider.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/TokenActionsProvider.kt index 8b8c5f5086..d3e51d1d76 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/TokenActionsProvider.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/TokenActionsProvider.kt @@ -2,6 +2,7 @@ package com.tangem.feature.wallet.presentation.wallet.state.factory import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.resourceReference +import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.model.TokenActionsState import com.tangem.feature.wallet.impl.R import com.tangem.feature.wallet.presentation.common.state.TokenItemState @@ -20,18 +21,15 @@ import kotlinx.collections.immutable.toImmutableList internal class TokenActionsProvider(private val clickIntents: WalletClickIntents) { fun provideActions(tokenActions: TokenActionsState): ImmutableList { - return convertTokenActionsState(tokenActions) - } - - private fun convertTokenActionsState( - tokenActionsState: TokenActionsState, - ): ImmutableList { - return tokenActionsState.states - .map(::tokenActionStateMapper) + return tokenActions.states + .map { mapTokenActionState(it, tokenActions.cryptoCurrencyStatus) } .toImmutableList() } - private fun tokenActionStateMapper(actionsState: TokenActionsState.ActionState): TokenActionButtonConfig { + private fun mapTokenActionState( + actionsState: TokenActionsState.ActionState, + cryptoCurrencyStatus: CryptoCurrencyStatus, + ): TokenActionButtonConfig { val title: TextReference val icon: Int val action: () -> Unit @@ -39,27 +37,27 @@ internal class TokenActionsProvider(private val clickIntents: WalletClickIntents is TokenActionsState.ActionState.Buy -> { title = resourceReference(R.string.common_buy) icon = R.drawable.ic_plus_24 - action = { } + action = { clickIntents.onBuyClick(cryptoCurrencyStatus) } } is TokenActionsState.ActionState.Receive -> { title = resourceReference(R.string.common_receive) icon = R.drawable.ic_arrow_down_24 - action = { } + action = { clickIntents.onReceiveClick(cryptoCurrencyStatus) } } is TokenActionsState.ActionState.Sell -> { title = resourceReference(R.string.common_sell) icon = R.drawable.ic_currency_24 - action = { } + action = { clickIntents.onSellClick(cryptoCurrencyStatus) } } is TokenActionsState.ActionState.Send -> { title = resourceReference(R.string.common_send) icon = R.drawable.ic_arrow_up_24 - action = { } + action = { clickIntents.onMultiCurrencySendClick(cryptoCurrencyStatus) } } is TokenActionsState.ActionState.Swap -> { title = resourceReference(R.string.common_swap) icon = R.drawable.ic_exchange_horizontal_24 - action = { } + action = { clickIntents.onSwapClick(cryptoCurrencyStatus) } } } return TokenActionButtonConfig( diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletCryptoCurrencyActionsConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletCryptoCurrencyActionsConverter.kt index b4b8f00fbf..e347773cc5 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletCryptoCurrencyActionsConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletCryptoCurrencyActionsConverter.kt @@ -14,9 +14,9 @@ import kotlinx.collections.immutable.toPersistentList internal class WalletCryptoCurrencyActionsConverter( private val currentStateProvider: Provider, private val clickIntents: WalletClickIntents, -) : Converter, WalletState> { +) : Converter { - override fun convert(value: List): WalletState { + override fun convert(value: TokenActionsState): WalletState { return when (val state = currentStateProvider()) { is WalletSingleCurrencyState.Content -> state.copy(buttons = value.mapToManageButtons()) is WalletSingleCurrencyState.Locked, @@ -26,23 +26,31 @@ internal class WalletCryptoCurrencyActionsConverter( } } - private fun List.mapToManageButtons(): PersistentList { - return this + private fun TokenActionsState.mapToManageButtons(): PersistentList { + return this.states .mapNotNull { action -> when (action) { is TokenActionsState.ActionState.Buy -> { - WalletManageButton.Buy(enabled = action.enabled, onClick = clickIntents::onBuyClick) + WalletManageButton.Buy( + enabled = action.enabled, + onClick = { clickIntents.onBuyClick(cryptoCurrencyStatus) }, + ) } is TokenActionsState.ActionState.Receive -> { - WalletManageButton.Receive(onClick = clickIntents::onReceiveClick) + WalletManageButton.Receive( + onClick = { clickIntents.onReceiveClick(cryptoCurrencyStatus) }, + ) } is TokenActionsState.ActionState.Sell -> { - WalletManageButton.Sell(enabled = action.enabled, onClick = clickIntents::onSellClick) + WalletManageButton.Sell( + enabled = action.enabled, + onClick = { clickIntents.onSellClick(cryptoCurrencyStatus) }, + ) } is TokenActionsState.ActionState.Send -> { WalletManageButton.Send( enabled = action.enabled, - onClick = clickIntents::onSingleCurrencySendClick, + onClick = { clickIntents.onSingleCurrencySendClick(cryptoCurrencyStatus) }, ) } is TokenActionsState.ActionState.Swap -> null diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletStateFactory.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletStateFactory.kt index 00e8cbc09a..2923db5694 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletStateFactory.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/WalletStateFactory.kt @@ -215,8 +215,16 @@ internal class WalletStateFactory( } } - fun getLoadingTxHistoryState(itemsCountEither: Either): WalletState { - return loadingTransactionsStateConverter.convert(value = itemsCountEither) + fun getLoadingTxHistoryState( + itemsCountEither: Either, + cryptoCurrencyStatus: CryptoCurrencyStatus, + ): WalletState { + return loadingTransactionsStateConverter.convert( + WalletLoadingTxHistoryConverter.WalletLoadingTxHistoryModel( + historyLoadingState = itemsCountEither, + cryptoCurrencyStatus = cryptoCurrencyStatus, + ), + ) } fun getLoadedTxHistoryState( @@ -233,8 +241,8 @@ internal class WalletStateFactory( return singleCurrencyLoadedBalanceConverter.convert(maybeCryptoCurrencyStatus) } - fun getSingleCurrencyManageButtonsState(actions: List): WalletState { - return cryptoCurrencyActionsConverter.convert(value = actions) + fun getSingleCurrencyManageButtonsState(actionsState: TokenActionsState): WalletState { + return cryptoCurrencyActionsConverter.convert(value = actionsState) } fun getStateByCurrencyStatusError(error: CurrencyStatusError): WalletState { diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/txhistory/WalletLoadingTxHistoryConverter.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/txhistory/WalletLoadingTxHistoryConverter.kt index 027a2bf2aa..09c79f69f3 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/txhistory/WalletLoadingTxHistoryConverter.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/state/factory/txhistory/WalletLoadingTxHistoryConverter.kt @@ -2,41 +2,45 @@ package com.tangem.feature.wallet.presentation.wallet.state.factory.txhistory import androidx.paging.PagingData import arrow.core.Either +import com.tangem.common.Converter import com.tangem.common.Provider import com.tangem.core.ui.components.transactions.state.TransactionState import com.tangem.core.ui.components.transactions.state.TxHistoryState.* +import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.txhistory.models.TxHistoryStateError import com.tangem.feature.wallet.presentation.wallet.state.WalletSingleCurrencyState import com.tangem.feature.wallet.presentation.wallet.state.WalletState import com.tangem.feature.wallet.presentation.wallet.viewmodels.WalletClickIntents -import com.tangem.utils.converter.Converter import kotlinx.coroutines.flow.update /** * Converter from loading tx history state to [WalletSingleCurrencyState.Content] * - * @property currentStateProvider current state provider - * @property clickIntents screen click intents + * @property currentStateProvider current state provider + * @property clickIntents screen click intents * [REDACTED_AUTHOR] */ internal class WalletLoadingTxHistoryConverter( private val currentStateProvider: Provider, private val clickIntents: WalletClickIntents, -) : Converter, WalletState> { +) : Converter { - override fun convert(value: Either): WalletState { - return value.fold(ifLeft = ::convertError, ifRight = ::convert) + override fun convert(value: WalletLoadingTxHistoryModel): WalletState { + return value.historyLoadingState.fold( + ifLeft = { convertError(it, value.cryptoCurrencyStatus) }, + ifRight = ::convertRight, + ) } - private fun convertError(error: TxHistoryStateError): WalletState { + private fun convertError(error: TxHistoryStateError, cryptoCurrencyStatus: CryptoCurrencyStatus): WalletState { val state = currentStateProvider() return if (state is WalletSingleCurrencyState.Content) { state.copy( txHistoryState = when (error) { is TxHistoryStateError.EmptyTxHistories -> { - Empty(onBuyClick = clickIntents::onBuyClick) + Empty(onBuyClick = { clickIntents.onBuyClick(cryptoCurrencyStatus) }) } is TxHistoryStateError.DataError -> { Error(onReloadClick = clickIntents::onReloadClick) @@ -51,7 +55,7 @@ internal class WalletLoadingTxHistoryConverter( } } - private fun convert(value: Int): WalletState { + private fun convertRight(value: Int): WalletState { val state = currentStateProvider() val txHistoryContent = (state as? WalletSingleCurrencyState.Content)?.txHistoryState as? Content @@ -71,4 +75,9 @@ internal class WalletLoadingTxHistoryConverter( return state } + + data class WalletLoadingTxHistoryModel( + val historyLoadingState: Either, + val cryptoCurrencyStatus: CryptoCurrencyStatus, + ) } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletClickIntents.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletClickIntents.kt index 0dc8e4b35b..b5267bd065 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletClickIntents.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletClickIntents.kt @@ -1,12 +1,11 @@ package com.tangem.feature.wallet.presentation.wallet.viewmodels -import com.tangem.core.ui.components.transactions.intents.TxHistoryClickIntents import com.tangem.domain.tokens.model.CryptoCurrencyStatus import com.tangem.domain.tokens.models.CryptoCurrency import com.tangem.domain.wallets.models.UserWalletId @Suppress("TooManyFunctions") -internal interface WalletClickIntents : TxHistoryClickIntents { +internal interface WalletClickIntents { fun onBackClick() @@ -56,9 +55,17 @@ internal interface WalletClickIntents : TxHistoryClickIntents { fun onMultiCurrencySendClick(cryptoCurrencyStatus: CryptoCurrencyStatus) - fun onReceiveClick() + fun onReceiveClick(cryptoCurrencyStatus: CryptoCurrencyStatus) - fun onSellClick() + fun onSellClick(cryptoCurrencyStatus: CryptoCurrencyStatus) fun onManageTokensClick() + + fun onBuyClick(cryptoCurrencyStatus: CryptoCurrencyStatus) + + fun onSwapClick(cryptoCurrencyStatus: CryptoCurrencyStatus) + + fun onReloadClick() + + fun onExploreClick() } \ No newline at end of file diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt index 1c8e0dd7f9..4c6416f4bf 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/presentation/wallet/viewmodels/WalletViewModel.kt @@ -366,20 +366,23 @@ internal class WalletViewModel @Inject constructor( router.openOrganizeTokensScreen(walletId) } - override fun onBuyClick() { + override fun onBuyClick(cryptoCurrencyStatus: CryptoCurrencyStatus) { val state = uiState as? WalletState.ContentState ?: return - val status = singleWalletCryptoCurrencyStatus ?: return val wallet = getWallet(index = state.walletsListConfig.selectedWalletIndex) reduxStateHolder.dispatch( TradeCryptoAction.New.Buy( userWallet = wallet, - cryptoCurrencyStatus = status, + cryptoCurrencyStatus = cryptoCurrencyStatus, appCurrencyCode = selectedAppCurrencyFlow.value.code, ), ) } + override fun onSwapClick(cryptoCurrencyStatus: CryptoCurrencyStatus) { + // todo implement onSwapClick [REDACTED_JIRA] + } + override fun onSingleCurrencySendClick(cryptoCurrencyStatus: CryptoCurrencyStatus?) { val state = uiState as? WalletState.ContentState ?: return @@ -424,16 +427,14 @@ internal class WalletViewModel @Inject constructor( } } - override fun onReceiveClick() { + override fun onReceiveClick(cryptoCurrencyStatus: CryptoCurrencyStatus) { // TODO: [REDACTED_JIRA] } - override fun onSellClick() { - val status = singleWalletCryptoCurrencyStatus ?: return - + override fun onSellClick(cryptoCurrencyStatus: CryptoCurrencyStatus) { reduxStateHolder.dispatch( TradeCryptoAction.New.Sell( - cryptoCurrencyStatus = status, + cryptoCurrencyStatus = cryptoCurrencyStatus, appCurrencyCode = selectedAppCurrencyFlow.value.code, ), ) @@ -501,7 +502,7 @@ internal class WalletViewModel @Inject constructor( val userWallet = getWallet(state.walletsListConfig.selectedWalletIndex) viewModelScope.launch(dispatchers.io) { getCryptoCurrencyActionsUseCase - .invoke(userWallet.walletId, cryptoCurrencyStatus.currency) + .invoke(userWallet.walletId, cryptoCurrencyStatus) .take(count = 1) .collectLatest { uiState = stateFactory.getStateWithTokenActionBottomSheet(it) @@ -591,7 +592,10 @@ internal class WalletViewModel @Inject constructor( viewModelScope.launch(dispatchers.io) { val txHistoryItemsCountEither = txHistoryItemsCountUseCase(network) - uiState = stateFactory.getLoadingTxHistoryState(itemsCountEither = txHistoryItemsCountEither) + uiState = stateFactory.getLoadingTxHistoryState( + itemsCountEither = txHistoryItemsCountEither, + cryptoCurrencyStatus = singleWalletCryptoCurrencyStatus ?: return@launch, + ) txHistoryItemsCountEither.onRight { uiState = stateFactory.getLoadedTxHistoryState( @@ -613,7 +617,7 @@ internal class WalletViewModel @Inject constructor( maybeCryptoCurrencyStatus.onRight { status -> singleWalletCryptoCurrencyStatus = status - updateButtons(userWalletId = userWalletId, currency = status.currency) + updateButtons(userWalletId = userWalletId, currencyStatus = status) updateTxHistory(status.currency.network) } } @@ -622,10 +626,10 @@ internal class WalletViewModel @Inject constructor( .saveIn(marketPriceJobHolder) } - private fun updateButtons(userWalletId: UserWalletId, currency: CryptoCurrency) { - getCryptoCurrencyActionsUseCase(userWalletId = userWalletId, cryptoCurrency = currency) + private fun updateButtons(userWalletId: UserWalletId, currencyStatus: CryptoCurrencyStatus) { + getCryptoCurrencyActionsUseCase(userWalletId = userWalletId, cryptoCurrencyStatus = currencyStatus) .distinctUntilChanged() - .onEach { uiState = stateFactory.getSingleCurrencyManageButtonsState(actions = it.states) } + .onEach { uiState = stateFactory.getSingleCurrencyManageButtonsState(actionsState = it) } .flowOn(dispatchers.io) .launchIn(viewModelScope) .saveIn(buttonsJobHolder)