From 1e9ee99cb07c595132fa96935e3f2b7c88dd586a Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 12 Feb 2024 16:43:50 -0600 Subject: [wallet] DD51: initial rendering based on currency spec bug 0008329 (cherry picked from commit 4789103406f44ba797c7bfee3112cc6aaa228683) --- .../src/main/java/net/taler/common/Amount.kt | 46 +++++++++++++++++++++- .../java/net/taler/common/CurrencySpecification.kt | 36 +++++++++++++++++ 2 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt (limited to 'taler-kotlin-android') diff --git a/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt b/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt index 5fb36fa..d6188cf 100644 --- a/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt +++ b/taler-kotlin-android/src/main/java/net/taler/common/Amount.kt @@ -16,11 +16,14 @@ package net.taler.common +import android.os.Build import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.Serializer import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder +import java.text.DecimalFormat +import java.text.NumberFormat import kotlin.math.floor import kotlin.math.pow import kotlin.math.roundToInt @@ -54,6 +57,11 @@ public data class Amount( * of 50_000_000 would correspond to 50 cents. */ val fraction: Int, + + /** + * Currency specification for amount + */ + val spec: CurrencySpecification? = null, ) : Comparable { public companion object { @@ -170,6 +178,8 @@ public data class Amount( return Amount(checkCurrency(currency), this.value, this.fraction) } + fun withSpec(spec: CurrencySpecification?) = copy(spec = spec) + public operator fun minus(other: Amount): Amount { check(currency == other.currency) { "Can only subtract from same currency" } var resultValue = value @@ -196,8 +206,40 @@ public data class Amount( return "$currency:$amountStr" } - override fun toString(): String { - return "$amountStr $currency" + override fun toString() = toString( + showSymbol = true, + negative = false, + ) + + fun toString( + showSymbol: Boolean = true, + negative: Boolean = false, + ): String { + val symbols = DecimalFormat().decimalFormatSymbols + + val format = if (showSymbol) { + NumberFormat.getCurrencyInstance() + } else { + // Make sure monetary separators are the ones used! + symbols.decimalSeparator = symbols.monetaryDecimalSeparator + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + symbols.groupingSeparator = symbols.monetaryGroupingSeparator + } + + NumberFormat.getInstance() + } + + if (spec != null) { + symbols.currencySymbol = spec.symbol(this) + format.maximumFractionDigits = spec.numFractionalNormalDigits + format.minimumFractionDigits = spec.numFractionalTrailingZeroDigits + } else { + symbols.currencySymbol = currency + } + + (format as DecimalFormat).decimalFormatSymbols = symbols + + return format.format((if (negative) "-$amountStr" else amountStr).toBigDecimal()) } override fun compareTo(other: Amount): Int { diff --git a/taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt b/taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt new file mode 100644 index 0000000..4aa8968 --- /dev/null +++ b/taler-kotlin-android/src/main/java/net/taler/common/CurrencySpecification.kt @@ -0,0 +1,36 @@ +/* + * This file is part of GNU Taler + * (C) 2024 Taler Systems S.A. + * + * GNU Taler is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 3, or (at your option) any later version. + * + * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * GNU Taler; see the file COPYING. If not, see + */ + +package net.taler.common + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class CurrencySpecification( + val name: String, + @SerialName("num_fractional_input_digits") + val numFractionalInputDigits: Int, + @SerialName("num_fractional_normal_digits") + val numFractionalNormalDigits: Int, + @SerialName("num_fractional_trailing_zero_digits") + val numFractionalTrailingZeroDigits: Int, + @SerialName("alt_unit_names") + val altUnitNames: Map, +) { + // TODO: add support for alt units + fun symbol(amount: Amount): String = altUnitNames[0] ?: amount.currency +} \ No newline at end of file -- cgit v1.2.3