[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-taler-ios] 07/11: cache currency infos
From: |
gnunet |
Subject: |
[taler-taler-ios] 07/11: cache currency infos |
Date: |
Sat, 21 Oct 2023 21:10:05 +0200 |
This is an automated email from the git hooks/post-receive script.
marc-stibane pushed a commit to branch master
in repository taler-ios.
commit cefa3ed84304f474f45929d0b8793a322586a0bf
Author: Marc Stibane <marc@taler.net>
AuthorDate: Fri Oct 20 08:53:47 2023 +0200
cache currency infos
---
TalerWallet1/Controllers/Controller.swift | 30 ++++++++++++++++++++++++++++++
TalerWallet1/Views/Main/MainView.swift | 19 +++++++++++++++++++
2 files changed, 49 insertions(+)
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
}
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [taler-taler-ios] 10/11: Use CurrencyFormatter for Balance, (continued)
- [taler-taler-ios] 10/11: Use CurrencyFormatter for Balance, gnunet, 2023/10/21
- [taler-taler-ios] 11/11: Minimalistic, gnunet, 2023/10/21
- [taler-taler-ios] 06/11: listExchangesForScopedCurrency, gnunet, 2023/10/21
- [taler-taler-ios] 04/11: cleanup, gnunet, 2023/10/21
- [taler-taler-ios] 01/11: CurrencySpecification, gnunet, 2023/10/21
- [taler-taler-ios] 03/11: ExchangeListItem, gnunet, 2023/10/21
- [taler-taler-ios] 05/11: getCurrencyInfo(scope), gnunet, 2023/10/21
- [taler-taler-ios] 02/11: HTTPError, gnunet, 2023/10/21
- [taler-taler-ios] 09/11: Call AboutView, gnunet, 2023/10/21
- [taler-taler-ios] 08/11: AboutView, gnunet, 2023/10/21
- [taler-taler-ios] 07/11: cache currency infos,
gnunet <=