diff options
Diffstat (limited to 'taler-kotlin-android/src/main/java/net/taler')
4 files changed, 115 insertions, 13 deletions
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 750a1de..3e3bd0a 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,15 @@ 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.DecimalFormatSymbols +import java.text.NumberFormat import kotlin.math.floor import kotlin.math.pow import kotlin.math.roundToInt @@ -54,6 +58,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<Amount> { public companion object { @@ -65,12 +74,6 @@ public data class Amount( private const val MAX_FRACTION_LENGTH = 8 public const val MAX_FRACTION: Int = 99_999_999 - public fun fromDouble(currency: String, value: Double): Amount { - val intPart = floor(value).toLong() - val fraPart = floor((value - intPart) * FRACTIONAL_BASE).toInt() - return Amount(currency, intPart, fraPart) - } - public fun zero(currency: String): Amount { return Amount(checkCurrency(currency), 0, 0) } @@ -96,6 +99,7 @@ public data class Amount( } public fun isValidAmountStr(str: String): Boolean { + if (str.count { it == '.' } > 1) return false val split = str.split(".") try { checkValue(split[0].toLongOrNull()) @@ -175,6 +179,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 @@ -201,8 +207,50 @@ 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, + symbols: DecimalFormatSymbols = DecimalFormat().decimalFormatSymbols, + ): String { + // We clone the object to safely/cleanly modify it + val s = symbols.clone() as DecimalFormatSymbols + val amount = (if (negative) "-$amountStr" else amountStr).toBigDecimal() + + // No currency spec, so we render normally + if (spec == null) { + val format = NumberFormat.getInstance() + format.maximumFractionDigits = MAX_FRACTION_LENGTH + format.minimumFractionDigits = 0 + if (Build.VERSION.SDK_INT >= 34) { + s.groupingSeparator = s.monetaryGroupingSeparator + } + s.decimalSeparator = s.monetaryDecimalSeparator + (format as DecimalFormat).decimalFormatSymbols = s + + val fmt = format.format(amount) + return if (showSymbol) "$fmt $currency" else fmt + } + + // There is currency spec, so we can do things right + val format = NumberFormat.getCurrencyInstance() + format.maximumFractionDigits = spec.numFractionalNormalDigits + format.minimumFractionDigits = spec.numFractionalTrailingZeroDigits + s.currencySymbol = spec.symbol ?: "" + (format as DecimalFormat).decimalFormatSymbols = s + + val fmt = format.format(amount) + return if (showSymbol) { + // If no symbol, then we use the currency string + if (spec.symbol != null) fmt else "$fmt $currency" + } else { + // We should do better than manually removing the symbol here + fmt.replace(s.currencySymbol, "").trim() + } } override fun compareTo(other: Amount): Int { diff --git a/taler-kotlin-android/src/main/java/net/taler/common/AndroidUtils.kt b/taler-kotlin-android/src/main/java/net/taler/common/AndroidUtils.kt index 066184c..8f3e5d5 100644 --- a/taler-kotlin-android/src/main/java/net/taler/common/AndroidUtils.kt +++ b/taler-kotlin-android/src/main/java/net/taler/common/AndroidUtils.kt @@ -23,6 +23,7 @@ import android.content.Context.CONNECTIVITY_SERVICE import android.content.Intent import android.net.ConnectivityManager import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET +import android.net.Uri import android.os.Build.VERSION.SDK_INT import android.os.Looper import android.text.format.DateUtils.DAY_IN_MILLIS @@ -120,6 +121,23 @@ fun Context.startActivitySafe(intent: Intent) { } } +fun Context.openUri(uri: String, title: String) { + val intent = Intent(Intent.ACTION_VIEW).apply { + data = Uri.parse(uri) + } + + startActivitySafe(Intent.createChooser(intent, title)) +} + +fun Context.shareText(text: String) { + val intent = Intent(Intent.ACTION_SEND).apply { + putExtra(Intent.EXTRA_TEXT, text) + type = "text/plain" + } + + startActivitySafe(Intent.createChooser(intent, null)) +} + fun Fragment.navigate(directions: NavDirections) = findNavController().navigate(directions) fun Long.toRelativeTime(context: Context): CharSequence { diff --git a/taler-kotlin-android/src/main/java/net/taler/common/ContractTerms.kt b/taler-kotlin-android/src/main/java/net/taler/common/ContractTerms.kt index 910cc36..5b614fe 100644 --- a/taler-kotlin-android/src/main/java/net/taler/common/ContractTerms.kt +++ b/taler-kotlin-android/src/main/java/net/taler/common/ContractTerms.kt @@ -42,7 +42,7 @@ abstract class Product { abstract val productId: String? abstract val description: String abstract val descriptionI18n: Map<String, String>? - abstract val price: Amount + abstract val price: Amount? abstract val location: String? abstract val image: String? val localizedDescription: String @@ -60,14 +60,14 @@ data class ContractProduct( override val description: String, @SerialName("description_i18n") override val descriptionI18n: Map<String, String>? = null, - override val price: Amount, + override val price: Amount? = null, @SerialName("delivery_location") override val location: String? = null, override val image: String? = null, - val quantity: Int + val quantity: Int = 1, ) : Product() { - val totalPrice: Amount by lazy { - price * quantity + val totalPrice: Amount? by lazy { + price?.let { price * quantity } } } 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..02113f4 --- /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 <http://www.gnu.org/licenses/> + */ + +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<Int, String>, +) { + // TODO: add support for alt units + val symbol: String? get() = altUnitNames[0] +}
\ No newline at end of file |