Bläddra i källkod

Correction for Garmin implementation and add delete function

Pierre L 3 år sedan
förälder
incheckning
2ef76bd5bb

+ 1 - 0
FreeAPS/Sources/Application/FreeAPSApp.swift

@@ -59,6 +59,7 @@ import Swinject
             Main.RootView(resolver: resolver)
                 .environment(\.managedObjectContext, dataController.persistentContainer.viewContext)
                 .environmentObject(Icons())
+                .onOpenURL(perform: handleURL)
         }
         .onChange(of: scenePhase) { newScenePhase in
             debug(.default, "APPLICATION PHASE: \(newScenePhase)")

+ 4 - 0
FreeAPS/Sources/Modules/GarminConfig/GarminConfigStateModel.swift

@@ -16,5 +16,9 @@ extension GarminConfig {
                 .weakAssign(to: \.devices, on: self)
                 .store(in: &lifetime)
         }
+
+        func deleteDevice() {
+            garmin.updateListDevices(devices: devices)
+        }
     }
 }

+ 10 - 2
FreeAPS/Sources/Modules/GarminConfig/View/GarminConfigRootView.swift

@@ -16,8 +16,11 @@ extension GarminConfig {
 
                 if state.devices.isNotEmpty {
                     Section(header: Text("Connected devices")) {
-                        ForEach(state.devices, id: \.uuid) { device in
-                            Text(device.friendlyName)
+                        List {
+                            ForEach(state.devices, id: \.uuid) { device in
+                                Text(device.friendlyName)
+                            }
+                            .onDelete(perform: onDelete)
                         }
                     }
                 }
@@ -26,5 +29,10 @@ extension GarminConfig {
             .navigationTitle("Garmin Watch")
             .navigationBarTitleDisplayMode(.automatic)
         }
+
+        private func onDelete(offsets: IndexSet) {
+            state.devices.remove(atOffsets: offsets)
+            state.deleteDevice()
+        }
     }
 }

+ 17 - 3
FreeAPS/Sources/Services/WatchManager/GarminManager.swift

@@ -5,6 +5,7 @@ import Swinject
 
 protocol GarminManager {
     func selectDevices() -> AnyPublisher<[IQDevice], Never>
+    func updateListDevices(devices: [IQDevice])
     var devices: [IQDevice] { get }
     func sendState(_ data: Data)
     var stateRequet: (() -> (Data))? { get set }
@@ -22,6 +23,8 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
 
     private let connectIQ = ConnectIQ.sharedInstance()
 
+    private let router = FreeAPSApp.resolver.resolve(Router.self)!
+
     @Injected() private var notificationCenter: NotificationCenter!
 
     @Persisted(key: "BaseGarminManager.persistedDevices") private var persistedDevices: [CodableDevice] = []
@@ -125,6 +128,10 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
         .eraseToAnyPublisher()
     }
 
+    func updateListDevices(devices: [IQDevice]) {
+        self.devices = devices
+    }
+
     func sendState(_ data: Data) {
         guard let object = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary else {
             return
@@ -133,8 +140,8 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
     }
 
     private func sendMessage(_ msg: NSDictionary, to app: IQApp) {
-        connectIQ?.sendMessage(msg, to: app, progress: { sent, all in
-            debug(.service, "Garmin: sending progress: \(Int(Double(sent) / Double(all) * 100)) %")
+        connectIQ?.sendMessage(msg, to: app, progress: { _, _ in
+            // debug(.service, "Garmin: sending progress: \(Int(Double(sent) / Double(all) * 100)) %")
         }, completion: { result in
             if result == .success {
                 debug(.service, "Garmin: message sent")
@@ -146,7 +153,14 @@ final class BaseGarminManager: NSObject, GarminManager, Injectable {
 }
 
 extension BaseGarminManager: IQUIOverrideDelegate {
-    func needsToInstallConnectMobile() {}
+    func needsToInstallConnectMobile() {
+        debug(.apsManager, "Garmin is not available")
+        let messageCont = MessageContent(
+            content: "The app Garmin Connect must be installed to use for iAPS.\n Go to App Store to download it",
+            type: .warning
+        )
+        router.alertMessage.send(messageCont)
+    }
 }
 
 extension BaseGarminManager: IQDeviceEventDelegate {

+ 1 - 1
FreeAPS/Sources/Services/WatchManager/WatchManager.swift

@@ -64,7 +64,7 @@ final class BaseWatchManager: NSObject, WatchManager, Injectable {
             self.state.lastLoopDate = self.enactedSuggestion?.recieved == true ? self.enactedSuggestion?.deliverAt : self
                 .apsManager.lastLoopDate
             self.state.lastLoopDateInterval = self.state.lastLoopDate.map {
-                guard $0.timeIntervalSince1970 > 0 else { return 0}
+                guard $0.timeIntervalSince1970 > 0 else { return 0 }
                 return UInt64($0.timeIntervalSince1970)
             }
             self.state.bolusIncrement = self.settingsManager.preferences.bolusIncrement