فهرست منبع

Change dispatch queue handling for plugin; improve re-init with additional guard

Deniz Cengiz 1 سال پیش
والد
کامیت
7f7e7b241f

+ 1 - 1
Trio/Sources/APS/CGM/PluginSource.swift

@@ -7,7 +7,7 @@ import LoopKit
 import LoopKitUI
 
 final class PluginSource: GlucoseSource {
-    private let processQueue = DispatchQueue(label: "DexcomSource.processQueue")
+    private let processQueue = DispatchQueue(label: "CGMPluginSource.processQueue")
     private let glucoseStorage: GlucoseStorage!
     var glucoseManager: FetchGlucoseManager?
 

+ 32 - 5
Trio/Sources/APS/FetchGlucoseManager.swift

@@ -126,11 +126,20 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
         // if plugin, if not the same pluginID, need to reset the cgmManager
         // if plugin and newManager provides, update cgmManager
         debug(.apsManager, "plugin : \(String(describing: cgmManager?.pluginIdentifier))")
-        if let manager = newManager
-        {
-            cgmManager = manager
-            glucoseSource = nil
-            removeCalibrations()
+//        if let manager = newManager
+//        {
+//            cgmManager = manager
+//            glucoseSource = nil
+//            removeCalibrations()
+
+        if let manager = newManager {
+            // If the pointer to manager is the *same* as our current `cgmManager`, skip re-init
+            if manager !== cgmManager {
+                // or do a more thorough check to see if it is the same class & state
+                removeCalibrations()
+                cgmManager = manager
+                glucoseSource = nil
+            }
         } else if self.cgmGlucoseSourceType == .plugin, cgmManager == nil, let rawCGMManager = rawCGMManager {
             cgmManager = cgmManagerFromRawValue(rawCGMManager)
             updateManagerUnits(cgmManager)
@@ -339,6 +348,24 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
     }
 }
 
+extension FetchGlucoseManager {
+    /// Dispatches given `functionToInvoke` to the CGM manager's queue (if any).
+    func performOnCGMManagerQueue(_ functionToInvoke: @escaping () -> Void) {
+        // If a CGM manager exists and it defines a delegate queue, use it
+        if let cgmManager = self.cgmManager,
+           let managerQueue = cgmManager.delegateQueue
+        {
+            managerQueue.async {
+                functionToInvoke()
+            }
+        } else {
+            // If there's no cgmManager or no queue, just run the block immediately
+            // This possibly executes `functionToInvoke` on main thread
+            functionToInvoke()
+        }
+    }
+}
+
 extension CGMManager {
     typealias RawValue = [String: Any]
 

+ 19 - 6
Trio/Sources/Modules/CGMSettings/CGMSettingsStateModel.swift

@@ -134,13 +134,26 @@ extension CGMSettings {
         }
 
         func deleteCGM() {
-            shouldDisplayCGMSetupSheet = false
-
-            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
-                self.fetchGlucoseManager.deleteGlucoseSource()
-                self.completionNotifyingDidComplete(OtherCGMSourceCompletionNotifying())
-            })
+            fetchGlucoseManager.performOnCGMManagerQueue {
+                // Call plugin functionality on the manager queue (or at least attempt to)
+                self.fetchGlucoseManager?.deleteGlucoseSource()
+
+                // UI updates go back to Main
+                DispatchQueue.main.async {
+                    self.shouldDisplayCGMSetupSheet = false
+                    self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
+                }
+            }
         }
+
+//        func deleteCGM() {
+//            shouldDisplayCGMSetupSheet = false
+//
+//            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
+//                self.fetchGlucoseManager.deleteGlucoseSource()
+//                self.completionNotifyingDidComplete(OtherCGMSourceCompletionNotifying())
+//            })
+//        }
     }
 }
 

+ 18 - 5
Trio/Sources/Modules/Home/HomeStateModel.swift

@@ -454,14 +454,27 @@ extension Home {
         }
 
         func deleteCGM() {
-            shouldDisplayCGMSetupSheet = false
-
-            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
+            fetchGlucoseManager.performOnCGMManagerQueue {
+                // Call plugin functionality on the manager queue (or at least attempt to)
                 self.fetchGlucoseManager?.deleteGlucoseSource()
-                self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
-            })
+
+                // UI updates go back to Main
+                DispatchQueue.main.async {
+                    self.shouldDisplayCGMSetupSheet = false
+                    self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
+                }
+            }
         }
 
+//        func deleteCGM() {
+//            shouldDisplayCGMSetupSheet = false
+//
+//            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
+//                self.fetchGlucoseManager?.deleteGlucoseSource()
+//                self.completionNotifyingDidComplete(CGMDeletionCompletionNotifying())
+//            })
+//        }
+
         /// Display the eventual status message provided by the manager of the pump
         /// Only display if state is warning or critical message else return nil
         private func displayPumpStatusHighlightMessage(_ didDeactivate: Bool = false) {