From 560bf8c98da6b0e70dfd25dbe20abcc3f83bc475 Mon Sep 17 00:00:00 2001 From: Tangem Date: Thu, 11 Nov 2021 02:32:19 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tap/domain/extensions/MoonpayStatus.kt | 31 +++++++++++++---- .../note/redux/OnboardingNoteState.kt | 2 +- .../products/twins/redux/TwinCardsState.kt | 2 +- .../tap/features/wallet/redux/WalletState.kt | 12 +++++-- .../redux/reducers/OnWalletLoadedReducer.kt | 33 ++++++++++--------- .../wallet/redux/reducers/WalletReducer.kt | 31 +++++++++-------- 6 files changed, 71 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/extensions/MoonpayStatus.kt b/app/src/main/java/com/tangem/tap/domain/extensions/MoonpayStatus.kt index a07f7698c3..4407a0516f 100644 --- a/app/src/main/java/com/tangem/tap/domain/extensions/MoonpayStatus.kt +++ b/app/src/main/java/com/tangem/tap/domain/extensions/MoonpayStatus.kt @@ -1,19 +1,38 @@ package com.tangem.tap.domain.extensions import com.tangem.blockchain.common.Blockchain +import com.tangem.tap.features.wallet.redux.Currency import com.tangem.tap.network.moonpay.MoonpayStatus /** [REDACTED_AUTHOR] */ -fun MoonpayStatus.buyIsAllowed(blockchain: Blockchain): Boolean { - if (blockchain == Blockchain.Unknown || blockchain == Blockchain.BSC) return false +fun MoonpayStatus.buyIsAllowed(currency: Currency): Boolean { + if (!isBuyAllowed) return false - return isBuyAllowed && availableToBuy.contains(blockchain.currency) + return when (currency) { + is Currency.Blockchain -> { + if (currency.blockchain == Blockchain.Unknown || currency.blockchain == Blockchain.BSC) { + false + } else { + availableToBuy.contains(currency.currencySymbol) + } + } + is Currency.Token -> false + } } -fun MoonpayStatus.sellIsAllowed(blockchain: Blockchain): Boolean { - if (blockchain == Blockchain.Unknown || blockchain == Blockchain.BSC) return false +fun MoonpayStatus.sellIsAllowed(currency: Currency): Boolean { + if (!isSellAllowed) return false - return isSellAllowed && availableToSell.contains(blockchain.currency) + return when (currency) { + is Currency.Blockchain -> { + if (currency.blockchain == Blockchain.Unknown || currency.blockchain == Blockchain.BSC) { + false + } else { + availableToSell.contains(currency.currencySymbol) + } + } + is Currency.Token -> false + } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/onboarding/products/note/redux/OnboardingNoteState.kt b/app/src/main/java/com/tangem/tap/features/onboarding/products/note/redux/OnboardingNoteState.kt index 2a8b6f6623..150c551f0b 100644 --- a/app/src/main/java/com/tangem/tap/features/onboarding/products/note/redux/OnboardingNoteState.kt +++ b/app/src/main/java/com/tangem/tap/features/onboarding/products/note/redux/OnboardingNoteState.kt @@ -27,7 +27,7 @@ data class OnboardingNoteState( get() = steps.indexOf(currentStep) val isBuyAllowed: Boolean by ReadOnlyProperty { thisRef, property -> - store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency.blockchain) ?: false + store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency) ?: false } } diff --git a/app/src/main/java/com/tangem/tap/features/onboarding/products/twins/redux/TwinCardsState.kt b/app/src/main/java/com/tangem/tap/features/onboarding/products/twins/redux/TwinCardsState.kt index a041508dc0..9d68635c08 100644 --- a/app/src/main/java/com/tangem/tap/features/onboarding/products/twins/redux/TwinCardsState.kt +++ b/app/src/main/java/com/tangem/tap/features/onboarding/products/twins/redux/TwinCardsState.kt @@ -57,7 +57,7 @@ data class TwinCardsState( get() = currentStep == TwinCardsStep.CreateSecondWallet || currentStep == TwinCardsStep.CreateThirdWallet val isBuyAllowed: Boolean by ReadOnlyProperty { thisRef, property -> - store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency.blockchain) ?: false + store.state.globalState.moonpayStatus?.buyIsAllowed(walletBalance.currency) ?: false } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt index d76c22251b..96044f0e1c 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt @@ -160,6 +160,14 @@ data class WalletState( return updatedWallets + remainingWallets } + fun updateTradeCryptoState(moonpayStatus: MoonpayStatus?, walletData: WalletData): WalletData { + return walletData.copy(tradeCryptoState = TradeCryptoState.from(moonpayStatus, walletData)) + } + + fun updateTradeCryptoState(moonpayStatus: MoonpayStatus?, walletDataList: List): List { + return walletDataList.map { it.copy(tradeCryptoState = TradeCryptoState.from(moonpayStatus, it)) } + } + fun addWalletManagers(newWalletManagers: List): WalletState { val updatedWalletManagers = this.walletManagers + newWalletManagers.filterNot { this.blockchains.contains(it.wallet.blockchain) } @@ -220,9 +228,9 @@ data class TradeCryptoState( companion object { fun from(moonpayStatus: MoonpayStatus?, walletData: WalletData): TradeCryptoState { val status = moonpayStatus ?: return walletData.tradeCryptoState - val blockchain = walletData.currency?.blockchain ?: return walletData.tradeCryptoState + val currency = walletData.currency - return TradeCryptoState(status.sellIsAllowed(blockchain), status.buyIsAllowed(blockchain)) + return TradeCryptoState(status.sellIsAllowed(currency), status.buyIsAllowed(currency)) } } } diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt index 017597cbb0..e7c3e0a1a0 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/OnWalletLoadedReducer.kt @@ -10,10 +10,7 @@ import com.tangem.tap.common.extensions.toFormattedFiatValue import com.tangem.tap.domain.getFirstToken import com.tangem.tap.features.wallet.models.removeUnknownTransactions import com.tangem.tap.features.wallet.models.toPendingTransactions -import com.tangem.tap.features.wallet.redux.Currency -import com.tangem.tap.features.wallet.redux.ProgressState -import com.tangem.tap.features.wallet.redux.WalletMainButton -import com.tangem.tap.features.wallet.redux.WalletState +import com.tangem.tap.features.wallet.redux.* import com.tangem.tap.features.wallet.ui.BalanceStatus import com.tangem.tap.features.wallet.ui.BalanceWidgetData import com.tangem.tap.features.wallet.ui.TokenData @@ -22,18 +19,18 @@ import java.math.RoundingMode class OnWalletLoadedReducer { - fun reduce(wallet: Wallet, walletState: WalletState, topUpAllowed: Boolean? = null): WalletState { + fun reduce(wallet: Wallet, walletState: WalletState): WalletState { return if (!walletState.isMultiwalletAllowed) { - onSingleWalletLoaded(wallet, walletState, topUpAllowed) + onSingleWalletLoaded(wallet, walletState) } else { - onMultiWalletLoaded(wallet, walletState, topUpAllowed) + onMultiWalletLoaded(wallet, walletState) } } - private fun onMultiWalletLoaded( - wallet: Wallet, walletState: WalletState, topUpAllowed: Boolean? = null - ): WalletState { + private fun onMultiWalletLoaded(wallet: Wallet, walletState: WalletState): WalletState { val fiatCurrencySymbol = store.state.globalState.appCurrency + val moonpayStatus = store.state.globalState.moonpayStatus + val amount = wallet.amounts[AmountType.Coin]?.value if (walletState.getWalletData(wallet.blockchain) == null) { return walletState @@ -64,7 +61,8 @@ class OnWalletLoadedReducer { ), pendingTransactions = pendingTransactions.removeUnknownTransactions(), mainButton = WalletMainButton.SendButton(sendButtonEnabled), - currency = Currency.Blockchain(wallet.blockchain) + currency = Currency.Blockchain(wallet.blockchain), + tradeCryptoState = TradeCryptoState.from(moonpayStatus, walletData) ) val tokens = wallet.getTokens().mapNotNull { token -> @@ -88,7 +86,9 @@ class OnWalletLoadedReducer { fiatAmountFormatted = tokenFiatAmount?.toFormattedFiatValue(fiatCurrencySymbol) ), pendingTransactions = tokenPendingTransactions.removeUnknownTransactions(), - mainButton = WalletMainButton.SendButton(sendButtonEnabled) + mainButton = WalletMainButton.SendButton(sendButtonEnabled), + tradeCryptoState = TradeCryptoState.from(moonpayStatus, tokenWalletData) + ) } val newWallets = (tokens + newWalletData).mapNotNull { it } @@ -104,12 +104,12 @@ class OnWalletLoadedReducer { ) } - private fun onSingleWalletLoaded( - wallet: Wallet, walletState: WalletState, topUpAllowed: Boolean? = null - ): WalletState { + private fun onSingleWalletLoaded(wallet: Wallet, walletState: WalletState): WalletState { if (wallet.blockchain != walletState.primaryBlockchain) return walletState val fiatCurrencySymbol = store.state.globalState.appCurrency + val moonpayStatus = store.state.globalState.moonpayStatus + val token = wallet.getFirstToken() val tokenData = if (token != null) { val tokenAmount = wallet.getTokenAmount(token) @@ -155,7 +155,8 @@ class OnWalletLoadedReducer { fiatAmount = fiatAmountRaw ), pendingTransactions = pendingTransactions.removeUnknownTransactions(), - mainButton = WalletMainButton.SendButton(sendButtonEnabled) + mainButton = WalletMainButton.SendButton(sendButtonEnabled), + tradeCryptoState = TradeCryptoState.from(moonpayStatus, walletState.primaryWallet) ) val wallets = walletData?.let { listOf(walletData) } ?: emptyList() return walletState.copy( diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt index b798b5be06..097fbf28e3 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/reducers/WalletReducer.kt @@ -16,6 +16,7 @@ import com.tangem.tap.domain.twins.TwinCardNumber import com.tangem.tap.features.wallet.redux.* import com.tangem.tap.features.wallet.ui.BalanceStatus import com.tangem.tap.features.wallet.ui.BalanceWidgetData +import com.tangem.tap.store import org.rekotlin.Action import java.math.BigDecimal import java.math.RoundingMode @@ -33,6 +34,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState { if (action !is WalletAction) return state.walletState + val moonpayStatus = store.state.globalState.moonpayStatus var newState = state.walletState when (action) { @@ -112,7 +114,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState { val walletManager = newState.getWalletManager(action.blockchain) ?: return newState val blockchain = walletManager.wallet.blockchain val currencies = listOf(Currency.Blockchain(blockchain)) + - walletManager.cardTokens.map { Currency.Token(it) } + walletManager.cardTokens.map { Currency.Token(it) } val newWallets = newState.wallets.filter { currencies.contains(it.currency) } .map { wallet -> wallet.copy( @@ -126,7 +128,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState { ) } val wallets = newState.replaceSomeWallets(newWallets) - newState = newState.copy(wallets = wallets) + newState = newState.copy(wallets = newState.updateTradeCryptoState(moonpayStatus, wallets)) } } is WalletAction.LoadWallet.Success -> newState = @@ -151,7 +153,7 @@ private fun internalReduce(action: Action, state: AppState): WalletState { } newState = newState.copy( state = progressState, - wallets = wallets + wallets = newState.updateTradeCryptoState(moonpayStatus, wallets) ) } is WalletAction.LoadWallet.Failure -> { @@ -185,7 +187,8 @@ private fun internalReduce(action: Action, state: AppState): WalletState { ProgressState.Done } newState = newState.copy( - state = progressState, wallets = wallets + state = progressState, + wallets = newState.updateTradeCryptoState(moonpayStatus, wallets) ) } is WalletAction.SetArtworkId -> { @@ -201,11 +204,11 @@ private fun internalReduce(action: Action, state: AppState): WalletState { newState = setNewFiatRate(action.fiatRate, state.globalState.appCurrency, newState) is WalletAction.LoadArtwork -> { val artworkUrl = action.card.getArtworkUrl(action.artworkId) - ?: when (state.twinCardsState.cardNumber) { - TwinCardNumber.First -> Artwork.TWIN_CARD_1 - TwinCardNumber.Second -> Artwork.TWIN_CARD_2 - else -> Artwork.DEFAULT_IMG_URL - } + ?: when (state.twinCardsState.cardNumber) { + TwinCardNumber.First -> Artwork.TWIN_CARD_1 + TwinCardNumber.Second -> Artwork.TWIN_CARD_2 + else -> Artwork.DEFAULT_IMG_URL + } newState = newState.copy(cardImage = Artwork(artworkId = artworkUrl)) } is WalletAction.ShowDialog.SignedHashesMultiWalletDialog -> { @@ -254,7 +257,7 @@ fun createAddressList(wallet: Wallet?, walletAddresses: WalletAddresses? = null) var indexOfSelectedWallet = 0 walletAddresses?.let { val index = - listOfAddressData.indexOfFirst { it.address == walletAddresses.selectedAddress.address } + listOfAddressData.indexOfFirst { it.address == walletAddresses.selectedAddress.address } if (index != -1) indexOfSelectedWallet = index } return WalletAddresses(listOfAddressData[indexOfSelectedWallet], listOfAddressData) @@ -265,10 +268,10 @@ fun Wallet.createAddressesData(): List { // put a defaultAddress at the first place addresses.forEach { val addressData = AddressData( - it.value, - it.type, - getShareUri(it.value), - getExploreUrl(it.value) + it.value, + it.type, + getShareUri(it.value), + getExploreUrl(it.value) ) if (it.type == blockchain.defaultAddressType()) { listOfAddressData.add(0, addressData)