Explorar o código

Remove all calibrations on newSensorDetected

Ivan Valkou %!s(int64=4) %!d(string=hai) anos
pai
achega
431a9f5e4d

+ 5 - 0
Dependencies/LibreTransmitter/Sources/LibreTransmitter/Common/Extensions/Notification.swift

@@ -0,0 +1,5 @@
+import Foundation
+
+public extension Notification.Name {
+    static let newSensorDetected = Notification.Name("LibreTransmitter.newSensorDetected")
+}

+ 1 - 0
Dependencies/LibreTransmitter/Sources/LibreTransmitter/LibreTransmitterManager.swift

@@ -755,6 +755,7 @@ extension LibreTransmitterManager {
         case .newSensor:
             logger.debug("dabear:: new libresensor detected")
             NotificationHelper.sendSensorChangeNotificationIfNeeded()
+            NotificationCenter.default.post(name: .newSensorDetected, object: nil)
         case .noSensor:
             logger.debug("dabear:: no libresensor detected")
             NotificationHelper.sendSensorNotDetectedNotificationIfNeeded(noSensor: true)

+ 12 - 0
FreeAPS/Sources/APS/CGM/Calibrations/CalibrationService.swift

@@ -1,4 +1,5 @@
 import Foundation
+import LibreTransmitter
 import Swinject
 
 struct Calibration: JSON, Hashable, Identifiable {
@@ -33,6 +34,8 @@ final class BaseCalibrationService: CalibrationService, Injectable {
     }
 
     @Injected() var storage: FileStorage!
+    @Injected() var notificationCenter: NotificationCenter!
+    private var lifetime = Lifetime()
 
     private(set) var calibrations: [Calibration] = [] {
         didSet {
@@ -43,6 +46,15 @@ final class BaseCalibrationService: CalibrationService, Injectable {
     init(resolver: Resolver) {
         injectServices(resolver)
         calibrations = storage.retrieve(OpenAPS.FreeAPS.calibrations, as: [Calibration].self) ?? []
+        subscribe()
+    }
+
+    private func subscribe() {
+        notificationCenter.publisher(for: .newSensorDetected)
+            .sink { [weak self] _ in
+                self?.removeAllCalibrations()
+            }
+            .store(in: &lifetime)
     }
 
     var slope: Double {

+ 9 - 1
FreeAPS/Sources/Services/Notifications/NotificationCenter.swift

@@ -1,3 +1,4 @@
+import Combine
 import Foundation
 
 protocol NotificationCenter {
@@ -14,6 +15,13 @@ protocol NotificationCenter {
         queue: OperationQueue?,
         using block: @escaping (Notification) -> Void
     ) -> NSObjectProtocol
+
+    func publisher(for name: Notification.Name, object: AnyObject?) -> Foundation.NotificationCenter.Publisher
+    func publisher(for name: Notification.Name) -> Foundation.NotificationCenter.Publisher
 }
 
-extension Foundation.NotificationCenter: NotificationCenter {}
+extension Foundation.NotificationCenter: NotificationCenter {
+    func publisher(for name: Notification.Name) -> Foundation.NotificationCenter.Publisher {
+        publisher(for: name, object: nil)
+    }
+}