commit 1ff1bc377c13eb9411ea9ed0fd5b971f699b0871
parent 9ce01d591c46a9b3f0377b1bc6b70154529a8fd7
Author: Marc Stibane <marc@taler.net>
Date: Sun, 12 Nov 2023 12:57:47 +0100
@Published currencyTicker
Diffstat:
6 files changed, 40 insertions(+), 19 deletions(-)
diff --git a/TalerWallet1/Controllers/Controller.swift b/TalerWallet1/Controllers/Controller.swift
@@ -33,7 +33,7 @@ class Controller: ObservableObject {
private let symLog = SymLogC()
@Published var backendState: BackendState = .none // only used for launch animation
- @Published var currencyInfos: [CurrencyInfo]
+ @Published var currencyTicker: Int = 0
@AppStorage("useHaptics") var useHaptics: Bool = true // extension mustn't define this, so it must be here
@AppStorage("playSounds") var playSounds: Int = 1 // extension mustn't define this, so it must be here
@AppStorage("talerFont") var talerFont: Int = 0 // extension mustn't define this, so it must be here
@@ -41,6 +41,7 @@ class Controller: ObservableObject {
let logger = Logger(subsystem: "net.taler.gnu", category: "Controller")
let player = AVQueuePlayer()
let semaphore = AsyncSemaphore(value: 1)
+ var currencyInfos: [CurrencyInfo]
var messageForSheet: String? = nil
@@ -52,35 +53,50 @@ class Controller: ObservableObject {
// }
// }
backendState = .instantiated
+ currencyTicker = 0
currencyInfos = []
}
// MARK: -
+ func hasInfo(for currency: String) -> Bool {
+ for info in currencyInfos {
+ if info.scope.currency == currency {
+ return true
+ }
+ }
+ return false
+ }
- func info(for currency: String) -> CurrencyInfo? {
- for currencyInfo in currencyInfos {
- if currencyInfo.scope.currency == currency {
- return currencyInfo
+ func info(for currency: String, _ ticker: Int) -> CurrencyInfo {
+ if ticker != currencyTicker {
+ print("Yikes")
+ }
+ for info in currencyInfos {
+ if info.scope.currency == currency {
+ return info
}
}
- return nil
+ return CurrencyInfo.zero(currency)
}
@MainActor
- func setInfo(_ info: CurrencyInfo) async {
+ func setInfo(_ newInfo: CurrencyInfo) async {
await semaphore.wait()
defer { semaphore.signal() }
- var existing: ScopeInfo? = nil
- for currencyInfo in currencyInfos {
- let scope = currencyInfo.scope
- if scope.currency == info.scope.currency {
- existing = scope
- break
+
+ var replaced = false
+ var newInfos = currencyInfos.map { currencyInfo in
+ if currencyInfo.scope.currency == newInfo.scope.currency {
+ replaced = true
+ return newInfo
+ } else {
+ return currencyInfo
}
}
- if existing != nil {
- currencyInfos.removeAll(where: { $0.scope == existing })
+ if !replaced {
+ newInfos.append(newInfo)
}
- currencyInfos.append(info)
+ currencyInfos = newInfos
+ currencyTicker += 1 // triggers published view update
}
// MARK: -
@MainActor
diff --git a/TalerWallet1/Views/Balances/BalanceRowView.swift b/TalerWallet1/Views/Balances/BalanceRowView.swift
@@ -53,6 +53,7 @@ struct BalanceRowView: View {
let recvAction: () -> Void
let rowAction: () -> Void
@Environment(\.sizeCategory) var sizeCategory
+ @EnvironmentObject private var controller: Controller
@AppStorage("iconOnly") var iconOnly: Bool = false
@AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic
@@ -72,6 +73,7 @@ struct BalanceRowView: View {
var body: some View {
SingleAxisGeometryReader { width in
VStack (alignment: .trailing) {
+ let currencyInfo = controller.info(for: amount.currencyStr, controller.currencyTicker)
let amountStr = amount.string(currencyInfo)
BalanceButton(amountStr: amountStr,
sizeCategory: sizeCategory,
diff --git a/TalerWallet1/Views/Balances/BalancesSectionView.swift b/TalerWallet1/Views/Balances/BalancesSectionView.swift
@@ -70,7 +70,7 @@ extension BalancesSectionView: View {
let _ = symLog.vlog() // just to get the # to compare it with .onAppear & onDisappear
#endif
let currency = balance.scopeInfo.currency
- let currencyInfo = controller.info(for: currency)
+ let currencyInfo = controller.info(for: currency, controller.currencyTicker)
Section {
if "KUDOS" == currency && !balance.available.isZero {
diff --git a/TalerWallet1/Views/Balances/PendingRowView.swift b/TalerWallet1/Views/Balances/PendingRowView.swift
@@ -57,6 +57,7 @@ struct PendingRowView: View {
let incoming: Bool
@Environment(\.sizeCategory) var sizeCategory
+ @EnvironmentObject private var controller: Controller
@AppStorage("iconOnly") var iconOnly: Bool = false
let inTitle0 = String(localized: "TitleIncoming_Short", defaultValue: "Incoming",
@@ -85,6 +86,7 @@ struct PendingRowView: View {
let pendingColor = WalletColors().pendingColor(incoming)
SingleAxisGeometryReader { width in
Group {
+ let currencyInfo = controller.info(for: amount.currencyStr, controller.currencyTicker)
let amountStr = amount.string(currencyInfo)
let amountWidth = amountStr.width(largeAmountFont: false, sizeCategory)
let inTitle = iconOnly ? (inTitle0, nil)
diff --git a/TalerWallet1/Views/Main/MainView.swift b/TalerWallet1/Views/Main/MainView.swift
@@ -268,7 +268,7 @@ extension MainView {
.onChange(of: balances) { newArray in
for balance in newArray {
let scope = balance.scopeInfo
- if controller.info(for: scope.currency) == nil {
+ if !controller.hasInfo(for: scope.currency) {
Task { // runs on MainActor
symLog?.log("get info for: \(scope.currency)")
do {
diff --git a/TalerWallet1/Views/Transactions/TransactionRowView.swift b/TalerWallet1/Views/Transactions/TransactionRowView.swift
@@ -62,6 +62,7 @@ struct TransactionRowView: View {
let currencyInfo: CurrencyInfo?
@Environment(\.sizeCategory) var sizeCategory
+ @EnvironmentObject private var controller: Controller
func needVStack(available: CGFloat, contentWidth: CGFloat, valueWidth: CGFloat) -> Bool {
available < (contentWidth + valueWidth + 40)
@@ -80,7 +81,7 @@ struct TransactionRowView: View {
let foreColor = pending ? WalletColors().pendingColor(incoming)
: done ? WalletColors().transactionColor(incoming)
: WalletColors().incompleteColor
-
+// let currencyInfo = controller.info(for: currency, controller.currencyTicker)
SingleAxisGeometryReader { width in
Group {
let amountStr = amount.string(currencyInfo)