Updated on 2026-08-14
This commit is contained in:
parent
fffd53dcf2
commit
598fa3b3ea
6 changed files with 215 additions and 218 deletions
|
|
@ -39,10 +39,23 @@ fun BigDecimal.toFormattedCurrencyString(
|
|||
return "$formattedAmount $currency"
|
||||
}
|
||||
|
||||
fun BigDecimal.toFiatString(rateValue: BigDecimal, fiatCurrencyName: String): String {
|
||||
fun BigDecimal.toFiatRateString(
|
||||
fiatCurrencyName: String
|
||||
): String {
|
||||
val value = this
|
||||
.setScale(2, RoundingMode.HALF_UP)
|
||||
.formatWithSpaces()
|
||||
return "$value $fiatCurrencyName"
|
||||
}
|
||||
|
||||
fun BigDecimal.toFiatString(
|
||||
rateValue: BigDecimal,
|
||||
fiatCurrencyName: String,
|
||||
formatWithSpaces: Boolean = false
|
||||
): String {
|
||||
var fiatValue = rateValue.multiply(this)
|
||||
fiatValue = fiatValue.setScale(2, RoundingMode.HALF_UP)
|
||||
return "≈ ${fiatCurrencyName} $fiatValue"
|
||||
return fiatValue.toFormattedFiatValue(fiatCurrencyName, formatWithSpaces)
|
||||
}
|
||||
|
||||
fun BigDecimal.toFiatValue(rateValue: BigDecimal): BigDecimal {
|
||||
|
|
@ -50,8 +63,12 @@ fun BigDecimal.toFiatValue(rateValue: BigDecimal): BigDecimal {
|
|||
return fiatValue.setScale(2, RoundingMode.HALF_UP)
|
||||
}
|
||||
|
||||
fun BigDecimal.toFormattedFiatValue(fiatCurrencyName: String): String {
|
||||
return "≈ ${fiatCurrencyName} $this"
|
||||
fun BigDecimal.toFormattedFiatValue(
|
||||
fiatCurrencyName: String,
|
||||
formatWithSpaces: Boolean = false
|
||||
): String {
|
||||
val fiatValue = if (formatWithSpaces) this.formatWithSpaces() else this
|
||||
return " ${fiatValue} $fiatCurrencyName"
|
||||
}
|
||||
|
||||
fun BigDecimal.stripZeroPlainString(): String = this.stripTrailingZeros().toPlainString()
|
||||
|
|
@ -111,4 +128,33 @@ fun BigDecimal.formatAmountAsSpannedString(
|
|||
append(' ')
|
||||
append(currencySymbol)
|
||||
}
|
||||
}
|
||||
|
||||
fun BigDecimal.formatWithSpaces(): String {
|
||||
val str = this.toString()
|
||||
var integerStr = str.substringBefore('.')
|
||||
val reminderStr = str.substringAfter('.')
|
||||
val packets = arrayListOf<String>()
|
||||
|
||||
var index: Int = integerStr.length
|
||||
while (0 < index) {
|
||||
if (index <= 3) {
|
||||
packets.add(0, integerStr)
|
||||
break
|
||||
}
|
||||
index -= 3
|
||||
packets.add(integerStr.substring(startIndex = index))
|
||||
integerStr = integerStr.substring(startIndex = 0, endIndex = index)
|
||||
}
|
||||
|
||||
return buildString {
|
||||
packets.forEachIndexed { index, packet ->
|
||||
append(packet)
|
||||
if (index != packets.lastIndex) append(' ')
|
||||
}
|
||||
if (reminderStr.isNotBlank()) {
|
||||
append('.')
|
||||
append(reminderStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@ class OnWalletLoadedReducer {
|
|||
amount = coinAmountValue,
|
||||
amountFormatted = formattedAmount,
|
||||
fiatAmount = fiatAmount,
|
||||
fiatAmountFormatted = fiatAmount?.toFormattedFiatValue(fiatCurrency.code)
|
||||
fiatAmountFormatted = fiatAmount?.toFormattedFiatValue(fiatCurrency.symbol)
|
||||
),
|
||||
pendingTransactions = pendingTransactions.removeUnknownTransactions(),
|
||||
mainButton = WalletMainButton.SendButton(coinSendButton),
|
||||
|
|
|
|||
|
|
@ -5,9 +5,9 @@ import com.tangem.blockchain.common.Blockchain
|
|||
import com.tangem.blockchain.common.Wallet
|
||||
import com.tangem.domain.common.TwinCardNumber
|
||||
import com.tangem.tap.common.entities.FiatCurrency
|
||||
import com.tangem.tap.common.extensions.toFiatRateString
|
||||
import com.tangem.tap.common.extensions.toFiatString
|
||||
import com.tangem.tap.common.extensions.toFiatValue
|
||||
import com.tangem.tap.common.extensions.toFormattedCurrencyString
|
||||
import com.tangem.tap.common.extensions.toFormattedFiatValue
|
||||
import com.tangem.tap.common.redux.AppState
|
||||
import com.tangem.tap.domain.TapError
|
||||
|
|
@ -20,7 +20,6 @@ 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
|
||||
|
||||
class WalletReducer {
|
||||
companion object {
|
||||
|
|
@ -383,10 +382,8 @@ private fun setNewFiatRate(
|
|||
state: WalletState
|
||||
): WalletState {
|
||||
val rate = fiatRate.second ?: return state
|
||||
val rateFormatted = rate.toFormattedCurrencyString(
|
||||
decimals = 2,
|
||||
currency = appCurrency.code,
|
||||
roundingMode = RoundingMode.HALF_UP
|
||||
val rateFormatted = rate.toFiatRateString(
|
||||
fiatCurrencyName = appCurrency.symbol
|
||||
)
|
||||
val currency = fiatRate.first
|
||||
|
||||
|
|
@ -415,7 +412,7 @@ private fun setMultiWalletFiatRate(
|
|||
is Currency.Token ->
|
||||
wallet?.getTokenAmount(currency.token)?.value?.toFiatValue(rate)
|
||||
}
|
||||
val fiatAmountFormatted = fiatAmount?.toFormattedFiatValue(appCurrency.code)
|
||||
val fiatAmountFormatted = fiatAmount?.toFormattedFiatValue(appCurrency.symbol)
|
||||
val newWalletData = state.getWalletData(currency)?.copy(
|
||||
currencyData = walletData.currencyData.copy(
|
||||
fiatAmountFormatted = fiatAmountFormatted,
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ import com.tangem.blockchain.common.Blockchain
|
|||
import com.tangem.blockchain.common.Token
|
||||
import com.tangem.domain.common.TapWorkarounds.derivationStyle
|
||||
import com.tangem.tap.common.extensions.getString
|
||||
import com.tangem.tap.common.extensions.hide
|
||||
import com.tangem.tap.common.extensions.loadCurrenciesIcon
|
||||
import com.tangem.tap.common.extensions.show
|
||||
import com.tangem.tap.features.wallet.redux.Currency
|
||||
|
|
@ -63,9 +62,7 @@ class WalletAdapter
|
|||
|
||||
fun bind(wallet: WalletData) = with(binding) {
|
||||
tvCurrency.text = wallet.currencyData.currency
|
||||
tvAmount.text = wallet.currencyData.amountFormatted?.takeWhile { !it.isWhitespace() }
|
||||
tvCurrencySymbol.text =
|
||||
wallet.currencyData.amountFormatted?.takeLastWhile { !it.isWhitespace() }
|
||||
tvAmount.text = wallet.currencyData.amountFormatted.orEmpty()
|
||||
tvAmountFiat.text = wallet.currencyData.fiatAmountFormatted
|
||||
tvExchangeRate.text = wallet.fiatRateString
|
||||
cardWallet.setOnClickListener {
|
||||
|
|
@ -74,8 +71,8 @@ class WalletAdapter
|
|||
val blockchain = wallet.currency.blockchain
|
||||
val token = (wallet.currency as? Currency.Token)?.token
|
||||
|
||||
val isCustom =
|
||||
wallet.currency.isCustomCurrency(store.state.globalState.scanResponse?.card?.derivationStyle)
|
||||
val isCustom = wallet.currency
|
||||
.isCustomCurrency(store.state.globalState.scanResponse?.card?.derivationStyle)
|
||||
tvExchangeRate.show(!isCustom)
|
||||
tvCustomCurrency.show(isCustom)
|
||||
|
||||
|
|
@ -86,43 +83,35 @@ class WalletAdapter
|
|||
)
|
||||
|
||||
when (wallet.currencyData.status) {
|
||||
BalanceStatus.VerifiedOnline, BalanceStatus.SameCurrencyTransactionInProgress -> hideWarning(isCustom)
|
||||
BalanceStatus.Loading -> {
|
||||
hideWarning(isCustom)
|
||||
if (wallet.currencyData.amountFormatted == null) {
|
||||
tvExchangeRate.text = root.getString(R.string.wallet_balance_loading)
|
||||
}
|
||||
BalanceStatus.VerifiedOnline,
|
||||
BalanceStatus.SameCurrencyTransactionInProgress -> hideMessage()
|
||||
BalanceStatus.Loading -> if (wallet.currencyData.amountFormatted == null) {
|
||||
showMessage(root.getString(R.string.wallet_balance_loading))
|
||||
}
|
||||
BalanceStatus.TransactionInProgress ->
|
||||
showWarning(root.getString(R.string.wallet_balance_tx_in_progress), isCustom)
|
||||
showMessage(root.getString(R.string.wallet_balance_tx_in_progress))
|
||||
BalanceStatus.Unreachable ->
|
||||
showWarning(root.getString(R.string.wallet_balance_blockchain_unreachable), isCustom)
|
||||
showMessage(root.getString(R.string.wallet_balance_blockchain_unreachable))
|
||||
|
||||
BalanceStatus.NoAccount ->
|
||||
showWarning(root.getString(R.string.wallet_error_no_account), isCustom)
|
||||
showMessage(root.getString(R.string.wallet_error_no_account))
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun showWarning(message: String, isCustom: Boolean = false) {
|
||||
toggleWarning(true, isCustom)
|
||||
binding.tvStatusErrorMessage.text = message
|
||||
private fun showMessage(message: String) {
|
||||
toggleMessage(true)
|
||||
binding.tvStatus.text = message
|
||||
}
|
||||
|
||||
private fun hideWarning(isCustom: Boolean = false) {
|
||||
toggleWarning(false, isCustom)
|
||||
private fun hideMessage() {
|
||||
toggleMessage(false)
|
||||
}
|
||||
|
||||
private fun toggleWarning(show: Boolean, isCustom: Boolean = false) {
|
||||
if (!show) {
|
||||
binding.tvExchangeRate.show(!isCustom)
|
||||
binding.tvCustomCurrency.show(isCustom)
|
||||
} else {
|
||||
binding.tvExchangeRate.hide()
|
||||
binding.tvCustomCurrency.hide()
|
||||
}
|
||||
binding.tvStatusErrorMessage.show(show)
|
||||
private fun toggleMessage(show: Boolean) {
|
||||
binding.tvAmount.show(!show)
|
||||
binding.tvStatus.show(show)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue