|
@@ -8,7 +8,6 @@ import Swinject
|
|
|
import UIKit
|
|
import UIKit
|
|
|
|
|
|
|
|
protocol FetchGlucoseManager: SourceInfoProvider {
|
|
protocol FetchGlucoseManager: SourceInfoProvider {
|
|
|
- func updateGlucoseStore(newBloodGlucose: [BloodGlucose])
|
|
|
|
|
func updateGlucoseSource(cgmGlucoseSourceType: CGMType, cgmGlucosePluginId: String, newManager: CGMManagerUI?)
|
|
func updateGlucoseSource(cgmGlucoseSourceType: CGMType, cgmGlucosePluginId: String, newManager: CGMManagerUI?)
|
|
|
func deleteGlucoseSource()
|
|
func deleteGlucoseSource()
|
|
|
func removeCalibrations()
|
|
func removeCalibrations()
|
|
@@ -163,11 +162,15 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
|
|
|
// if plugin, if not the same pluginID, need to reset the cgmManager
|
|
// if plugin, if not the same pluginID, need to reset the cgmManager
|
|
|
// if plugin and newManager provides, update cgmManager
|
|
// if plugin and newManager provides, update cgmManager
|
|
|
debug(.apsManager, "plugin : \(String(describing: cgmManager?.pluginIdentifier))")
|
|
debug(.apsManager, "plugin : \(String(describing: cgmManager?.pluginIdentifier))")
|
|
|
- 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 {
|
|
} else if self.cgmGlucoseSourceType == .plugin, cgmManager == nil, let rawCGMManager = rawCGMManager {
|
|
|
cgmManager = cgmManagerFromRawValue(rawCGMManager)
|
|
cgmManager = cgmManagerFromRawValue(rawCGMManager)
|
|
|
updateManagerUnits(cgmManager)
|
|
updateManagerUnits(cgmManager)
|
|
@@ -311,17 +314,22 @@ final class BaseFetchGlucoseManager: FetchGlucoseManager, Injectable {
|
|
|
return entries
|
|
return entries
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- /// function called when a callback is fired by CGM BLE
|
|
|
|
|
- public func updateGlucoseStore(newBloodGlucose: [BloodGlucose]) {
|
|
|
|
|
- Task {
|
|
|
|
|
- let syncDate = glucoseStorage.syncDate()
|
|
|
|
|
- debug(.deviceManager, "CGM BLE FETCHGLUCOSE : SyncDate is \(syncDate)")
|
|
|
|
|
- do {
|
|
|
|
|
- try await glucoseStoreAndHeartDecision(syncDate: syncDate, glucose: newBloodGlucose)
|
|
|
|
|
- } catch {
|
|
|
|
|
- debug(.deviceManager, "Failed to store glucose: \(error.localizedDescription)")
|
|
|
|
|
|
|
+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()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|