taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

commit a941f5673e3fcff7b657c2487472c48466224535
parent bd41a0e0bef739b60ae6d0f7b7cb5ccd2493dd4b
Author: Marc Stibane <marc@taler.net>
Date:   Mon, 10 Jun 2024 22:32:37 +0200

DD51 bugfixes

Diffstat:
MTalerWallet1/Helper/CurrencySpecification.swift | 62+++++++++++++++++++++++++++++++++++++++++++-------------------
MTalerWallet1/Views/Transactions/ManualDetailsV.swift | 21+++++++--------------
2 files changed, 50 insertions(+), 33 deletions(-)

diff --git a/TalerWallet1/Helper/CurrencySpecification.swift b/TalerWallet1/Helper/CurrencySpecification.swift @@ -49,6 +49,16 @@ extension Amount { return self.readableDescription } + func formatted(specs: CurrencySpecification?, scope: ScopeInfo? = nil) -> String { + if let specs { + let myScope = scope ?? ScopeInfo(type: .madeUp, currency: currencyStr) + let formatter = CurrencyFormatter.formatter(scope: myScope, specs: specs) + let currencyInfo = CurrencyInfo(scope: myScope, specs: specs, formatter: formatter) + return formatted(currencyInfo) + } + return formatted() + } + func inputDigits(_ currencyInfo: CurrencyInfo) -> UInt { let inputDigits = currencyInfo.specs.fractionalInputDigits if inputDigits < 0 { return 0 } @@ -128,12 +138,18 @@ public struct CurrencyInfo { } func symbol() -> String? { - if formatter.hasAltUnitName0 { - if let symbol = specs.altUnitNames?[0] { - return symbol + formatter.altUnitName0 + } + + func currencyString(_ euroString: String, useSymbol: Bool = true) -> String { + if useSymbol { + if let altUnitName0 = formatter.altUnitName0 { + let symbolString = euroString.replacingOccurrences(of: formatter.currencySymbol, with: altUnitName0) + return symbolString.replacingOccurrences(of: formatter.currencyCode, with: altUnitName0) } } - return nil + let nameString = euroString.replacingOccurrences(of: formatter.currencySymbol, with: formatter.currencyName) + return nameString.replacingOccurrences(of: formatter.currencyCode, with: formatter.currencyName) } // TODO: use valueAsDecimalTuple instead of valueAsFloatTuple @@ -141,7 +157,9 @@ public struct CurrencyInfo { formatter.setUseSymbol(useSymbol) let (integer, fraction) = valueTuple if let integerStr = formatter.string(for: integer) { - if fraction == 0 { return integerStr.nbs() } // formatter already added trailing zeroes + if fraction == 0 { + return currencyString(integerStr.nbs(), useSymbol: useSymbol) // formatter already added trailing zeroes + } if let fractionStr = formatter.string(for: fraction) { if let decimalSeparator = formatter.currencyDecimalSeparator { if let fractionIndex = fractionStr.endIndex(of: decimalSeparator) { @@ -167,7 +185,7 @@ public struct CurrencyInfo { } } // print(resultStr) - return resultStr.nbs() + return currencyString(resultStr.nbs(), useSymbol: useSymbol) } // if we arrive here then fractionStr doesn't have a decimal separator. Yikes! } @@ -219,24 +237,30 @@ public struct CurrencySpecification: Codable, Sendable { public class CurrencyFormatter: NumberFormatter { - var hasAltUnitName0: Bool // specs.altUnitNames[0] should have the Symbol ($,€,¥) + var longName: String + var altUnitName0: String? // specs.altUnitNames[0] should have the Symbol ($,€,¥) + var currencyName: String var leadingCurrencySymbol: Bool /// factory static func formatter(scope: ScopeInfo, specs: CurrencySpecification) -> CurrencyFormatter { let formatter = CurrencyFormatter() - formatter.setCode(to: scope.currency) + formatter.longName = specs.name + formatter.altUnitName0 = specs.altUnitNames?[0] + formatter.currencyName = scope.currency +// formatter.setCode(to: "EUR") +// formatter.setSymbol(to: "€") formatter.setMinimumFractionDigits(specs.fractionalTrailingZeroDigits) - if let symbol = specs.altUnitNames?[0] { - formatter.setSymbol(to: symbol) - formatter.hasAltUnitName0 = true - } return formatter } public override init() { - self.hasAltUnitName0 = false + self.longName = "Euro" + self.altUnitName0 = "€" + self.currencyName = "EUR" self.leadingCurrencySymbol = false super.init() + self.currencyCode = "EUR" + self.currencySymbol = "€" self.locale = Locale.autoupdatingCurrent self.usesGroupingSeparator = true self.numberStyle = .currencyISOCode // .currency @@ -259,13 +283,13 @@ public class CurrencyFormatter: NumberFormatter { numberStyle = useSymbol ? .currency : .currencyISOCode } - func setCode(to code:String) { - currencyCode = code - } +// func setCode(to code:String) { +// currencyCode = code +// } - func setSymbol(to symbol:String) { - currencySymbol = symbol - } +// func setSymbol(to symbol:String) { +// currencySymbol = symbol +// } func setMinimumFractionDigits(_ digits: Int) { minimumFractionDigits = digits diff --git a/TalerWallet1/Views/Transactions/ManualDetailsV.swift b/TalerWallet1/Views/Transactions/ManualDetailsV.swift @@ -33,10 +33,12 @@ struct SegmentControl: View { HStack(spacing: 0) { ForEach((0..<count), id: \.self) { index in let detail = accountDetails[index] + let specs = detail.currencySpecification let amount = detail.transferAmount + let amountStr = amount?.formatted(specs: specs) ?? "" let bankName = detail.bankLabel - let amountStr = amount?.readableDescription ?? "" let a11yLabel = bankName != nil ? (bankName! + " " + amountStr) : amountStr +// let _ = print(amountStr) VStack(spacing: 6) { Text(amountStr) .talerFont(.title3) @@ -83,7 +85,8 @@ struct AccountPicker: View { ForEach(0..<accountDetails.count, id: \.self, content: { index in let detail = accountDetails[index] if let amount = detail.transferAmount { - let amountStr = amount.readableDescription + let amountStr = amount.formatted() +// let _ = print(amountStr) if let bankName = detail.bankLabel { Text(bankName + ": " + amountStr) .tag(index) @@ -159,17 +162,6 @@ struct ManualDetailsV: View { } } - func amountString(_ amount: Amount, specs: CurrencySpecification?, scope: ScopeInfo? = nil) -> String { - if let specs { - let myScope = scope ?? ScopeInfo(type: .madeUp, currency: amount.currencyStr) - let currencyInfo = CurrencyInfo(scope: myScope, specs: specs, - formatter: CurrencyFormatter.formatter(scope: myScope, - specs: specs)) - return amount.formatted(currencyInfo) - } - return amount.formatted() - } - var body: some View { if let accountDetails = details.exchangeCreditAccountDetails { let validDetails = validDetails(accountDetails) @@ -177,8 +169,9 @@ struct ManualDetailsV: View { let account = validDetails[accountID] if let amount = account.transferAmount { let specs = account.currencySpecification - let amountStr = amountString(amount, specs: specs) + let amountStr = amount.formatted(specs: specs) let obtainStr = common.amountRaw.formatted() +// let _ = print(amountStr, " | ", obtainStr) if !minimalistic { Text("The Payment Service Provider is waiting for your wire-transfer.") .bold()