diff options
author | Marc Stibane <marc@taler.net> | 2023-10-20 08:53:47 +0200 |
---|---|---|
committer | Marc Stibane <marc@taler.net> | 2023-10-20 09:33:16 +0200 |
commit | cefa3ed84304f474f45929d0b8793a322586a0bf (patch) | |
tree | 5bc7d5974b543c17ad025583400e5ed3627c0601 | |
parent | fa93599052d1eb19046a3a3952636b83eaaaa0e9 (diff) | |
download | taler-ios-cefa3ed84304f474f45929d0b8793a322586a0bf.tar.gz taler-ios-cefa3ed84304f474f45929d0b8793a322586a0bf.tar.bz2 taler-ios-cefa3ed84304f474f45929d0b8793a322586a0bf.zip |
cache currency infos
-rw-r--r-- | TalerWallet1/Controllers/Controller.swift | 30 | ||||
-rw-r--r-- | TalerWallet1/Views/Main/MainView.swift | 19 |
2 files changed, 49 insertions, 0 deletions
diff --git a/TalerWallet1/Controllers/Controller.swift b/TalerWallet1/Controllers/Controller.swift index f408084..33169ee 100644 --- a/TalerWallet1/Controllers/Controller.swift +++ b/TalerWallet1/Controllers/Controller.swift @@ -33,12 +33,14 @@ class Controller: ObservableObject { private let symLog = SymLogC() @Published var backendState: BackendState = .none // only used for launch animation + @Published var currencyInfos: [CurrencyInfo] @AppStorage("useHaptics") var useHaptics: Bool = false // extension mustn't define this, so it must be here @AppStorage("playSounds") var playSounds: Int = 0 // 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 let hapticCapability = CHHapticEngine.capabilitiesForHardware() let logger = Logger (subsystem: "net.taler.gnu", category: "Controller") let player = AVQueuePlayer() + let semaphore = AsyncSemaphore(value: 1) var messageForSheet: String? = nil @@ -50,6 +52,34 @@ class Controller: ObservableObject { // } // } backendState = .instantiated + currencyInfos = [] + } + + func info(for currency: String) -> CurrencyInfo? { + for currencyInfo in currencyInfos { + if currencyInfo.scope.currency == currency { + return currencyInfo + } + } + return nil + } + + @MainActor + func setInfo(_ info: 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 + } + } + if existing != nil { + currencyInfos.removeAll(where: { $0.scope == existing }) + } + currencyInfos.append(info) } @MainActor diff --git a/TalerWallet1/Views/Main/MainView.swift b/TalerWallet1/Views/Main/MainView.swift index 7d70698..a0abc93 100644 --- a/TalerWallet1/Views/Main/MainView.swift +++ b/TalerWallet1/Views/Main/MainView.swift @@ -79,6 +79,8 @@ extension MainView { @State private var balances: [Balance] = [] @Binding var talerFont: Int @AppStorage("iconOnly") var iconOnly: Bool = false + @EnvironmentObject private var controller: Controller + @EnvironmentObject private var model: WalletModel let balancesTitle = String(localized: "Balances") let exchangesTitle = String(localized: "Exchanges") let settingsTitle = String(localized: "Settings") @@ -213,6 +215,23 @@ extension MainView { symLog?.log(".onNotification(.BalanceChange) ==> reload") shouldReloadBalances += 1 } + .onChange(of: balances) { newArray in + for balance in newArray { + let scope = balance.scopeInfo + if controller.info(for: scope.currency) == nil { + Task { // runs on MainActor + symLog?.log("get info for: \(scope.currency)") + do { + let info = try await model.getCurrencyInfo(scope: scope) + symLog?.log("added: \(scope.currency)") + await controller.setInfo(info) + } catch { // TODO: error handling - couldn't get CurrencyInfo + symLog?.log("error: \(error)") + } + } + } + } + } } // body } // Content } |