commit 95cbd616cf147adb0489028a3afac2e8ec96fee9
parent 284b28bc0673da9d086651d205ed3fdc4c35091b
Author: Marc Stibane <marc@taler.net>
Date: Fri, 18 Oct 2024 11:07:44 +0200
cleanup currencyInfo
Diffstat:
3 files changed, 37 insertions(+), 11 deletions(-)
diff --git a/TalerWallet1/Helper/CurrencySpecification.swift b/TalerWallet1/Helper/CurrencySpecification.swift
@@ -6,8 +6,22 @@
* @author Marc Stibane
*/
import Foundation
+import SwiftUI
import taler_swift
+private struct CurrencyInfoKey: EnvironmentKey {
+ static let defaultValue: CurrencyInfo = {
+ return CurrencyInfo.zero(UNKNOWN)
+ }()
+}
+
+extension EnvironmentValues {
+ var currencyInfo: CurrencyInfo {
+ get { self[CurrencyInfoKey.self] }
+ set { self[CurrencyInfoKey.self] = newValue }
+ }
+}
+
extension Locale {
static var preferredLanguageCode: String {
guard let preferredLanguage = preferredLanguages.first,
@@ -44,9 +58,9 @@ extension Amount {
}
}
- func formatted(isNegative: Bool, useISO: Bool = false, a11y: String? = nil) -> String {
+ func formatted(_ scope: ScopeInfo, isNegative: Bool, useISO: Bool = false, a11y: String? = nil) -> String {
let controller = Controller.shared
- if let currencyInfo = controller.info(for: self.currencyStr) {
+ if let currencyInfo = controller.info2(for: self.currencyStr) {
return self.formatted(currencyInfo, isNegative: isNegative, useISO: useISO, a11y: a11y)
}
return self.readableDescription
@@ -58,8 +72,10 @@ extension Amount {
let formatter = CurrencyFormatter.formatter(scope: myScope, specs: specs)
let currencyInfo = CurrencyInfo(scope: myScope, specs: specs, formatter: formatter)
return formatted(currencyInfo, isNegative: isNegative, useISO: useISO)
+ } else if let scope {
+ return formatted(scope, isNegative: isNegative, useISO: useISO)
}
- return formatted(isNegative: isNegative, useISO: useISO)
+ return self.readableDescription
}
func inputDigits(_ currencyInfo: CurrencyInfo) -> UInt {
@@ -83,7 +99,7 @@ extension Amount {
}
}
-public struct CurrencyInfo {
+public struct CurrencyInfo: Sendable {
let scope: ScopeInfo
let specs: CurrencySpecification
let formatter: CurrencyFormatter
@@ -125,6 +141,18 @@ public struct CurrencyInfo {
return CurrencyInfo(scope: scope, specs: specs, formatter: formatter)
}
+
+ var currency: String { scope.currency }
+ var name: String { specs.name }
+ var symbol: String { altUnitSymbol ?? specs.name } // fall back to name if no symbol defined
+ var hasSymbol: Bool {
+ if symbol != name {
+ let count = symbol.count
+ return count > 0 && count <= 3
+ }
+ return false
+ }
+
/// returns all characters left from the decimalSeparator
func integerPartStr(_ integerStr: String, decimalSeparator: String) -> String {
if let integerIndex = integerStr.endIndex(of: decimalSeparator) {
diff --git a/TalerWallet1/Views/HelperViews/BarGraph.swift b/TalerWallet1/Views/HelperViews/BarGraph.swift
@@ -11,8 +11,7 @@ struct BarGraphHeader: View {
private let symLog = SymLogV(0)
let stack: CallStack
let scopeInfo: ScopeInfo?
- @Binding var currencyName: String
- @Binding var currencySymbol: String
+ @Binding var currencyInfo: CurrencyInfo
@Binding var shouldReloadBalances: Int
@EnvironmentObject private var model: WalletModel
@@ -26,8 +25,8 @@ struct BarGraphHeader: View {
var body: some View {
HStack (alignment: .center, spacing: 10) {
- if !minimalistic || currencyName != currencySymbol {
- Text(currencyName)
+ if !minimalistic || currencyInfo.hasSymbol {
+ Text(currencyInfo.name)
.talerFont(.title2)
.foregroundColor(WalletColors().secondary(colorScheme, colorSchemeContrast))
}
@@ -44,8 +43,6 @@ struct BarGraphHeader: View {
scopeInfo: scopeInfo,
filterByState: .done,
limit: MAXBARS) {
-// let completed = WalletModel.completedTransactions(response)
-// completedTransactions = completed
completedTransactions = response
}
}
diff --git a/TalerWallet1/Views/Transactions/ManualDetailsV.swift b/TalerWallet1/Views/Transactions/ManualDetailsV.swift
@@ -90,7 +90,8 @@ struct AccountPicker: View {
ForEach(0..<accountDetails.count, id: \.self) { index in
let detail = accountDetails[index]
if let amount = detail.transferAmount {
- let amountStr = amount.formatted(isNegative: false, useISO: false)
+ let amountStr = amount.formatted(specs: detail.currencySpecification,
+ isNegative: false, useISO: false)
// let _ = print(amountStr)
if let bankName = detail.bankLabel {
Text(bankName + ": " + amountStr)