gnunet-svn
[Top][All Lists]
Advanced

[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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]