Просмотр исходного кода

Extend HealthKitManager with Logging (#475)

Deniz Cengiz 2 лет назад
Родитель
Сommit
7a673fa1bd

+ 4 - 2
FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift

@@ -180,7 +180,8 @@ extension Bolus {
                         Button {
                             keepForNextWiew = true
                             state.add()
-                            appState.currentTab = .home
+                            state.hideModal()
+//                            appState.currentTab = .home
                         }
                         label: { Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus") }
                             .frame(maxWidth: .infinity, alignment: .center)
@@ -193,7 +194,8 @@ extension Bolus {
                     Section {
                         Button {
                             keepForNextWiew = true
-                            appState.currentTab = .home
+                            state.hideModal()
+//                            appState.currentTab = .home
                         }
                         label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
                     }.listRowBackground(Color.chart)

+ 4 - 2
FreeAPS/Sources/Modules/Bolus/View/DefaultBolusCalcRootView.swift

@@ -120,7 +120,8 @@ extension Bolus {
                         Button {
                             keepForNextWiew = true
                             state.add()
-                            appState.currentTab = .home
+                            state.hideModal()
+//                            appState.currentTab = .home
                         }
                         label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
                             .frame(maxWidth: .infinity, alignment: .center)
@@ -134,7 +135,8 @@ extension Bolus {
                     Section {
                         Button {
                             keepForNextWiew = true
-                            appState.currentTab = .home
+                            state.hideModal()
+//                            appState.currentTab = .home
                         }
                         label: { Text("Continue without bolus") }.frame(maxWidth: .infinity, alignment: .center)
                     }.listRowBackground(Color.chart)

+ 42 - 6
FreeAPS/Sources/Services/HealthKit/HealthKitManager.swift

@@ -19,9 +19,9 @@ protocol HealthKitManager: GlucoseSource {
     func saveIfNeeded(carbs: [CarbsEntry])
     /// Save Insulin to Health store
     func saveIfNeeded(pumpEvents events: [PumpHistoryEvent])
-    /// Create observer for data passing beetwen Health Store and FreeAPS
+    /// Create observer for data passing beetwen Health Store and iAPS
     func createBGObserver()
-    /// Enable background delivering objects from Apple Health to FreeAPS
+    /// Enable background delivering objects from Apple Health to iAPS
     func enableBackgroundDelivery()
     /// Delete glucose with syncID
     func deleteGlucose(syncID: String)
@@ -169,7 +169,19 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
                     )
                 }
 
-            healthKitStore.save(samplesToSave) { _, _ in }
+            healthKitStore.save(samplesToSave) { (success: Bool, error: Error?) -> Void in
+                if success {
+                    for sample in samplesToSave {
+                        debug(
+                            .service,
+                            "Stored blood glucose \(sample.quantity) in HealthKit Store! Metadata: \(String(describing: sample.metadata?.values))"
+                        )
+                    }
+                } else {
+                    debug(.service, "Failed to store blood glucose in HealthKit Store!")
+                    debug(.service, error?.localizedDescription ?? "Unknown error")
+                }
+            }
         }
 
         loadSamplesFromHealth(sampleType: sampleType, withIDs: bloodGlucose.map(\.id))
@@ -195,7 +207,7 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
             let sampleDates = samples.map(\.startDate)
             let samplesToSave = carbsWithId
                 .filter { !sampleIDs.contains($0.id ?? "") } // id existing in AH
-                .filter { !sampleDates.contains($0.actualDate ?? $0.createdAt) } // not id but exaclty the same datetime
+                .filter { !sampleDates.contains($0.actualDate ?? $0.createdAt) } // not id but exactly the same datetime
                 .map {
                     HKQuantitySample(
                         type: sampleType,
@@ -210,7 +222,19 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
                     )
                 }
 
-            healthKitStore.save(samplesToSave) { _, _ in }
+            healthKitStore.save(samplesToSave) { (success: Bool, error: Error?) -> Void in
+                if success {
+                    for sample in samplesToSave {
+                        debug(
+                            .service,
+                            "Stored carb entry \(sample.quantity) in HealthKit Store! Metadata: \(String(describing: sample.metadata?.values))"
+                        )
+                    }
+                } else {
+                    debug(.service, "Failed to store carb entry in HealthKit Store!")
+                    debug(.service, error?.localizedDescription ?? "Unknown error")
+                }
+            }
         }
 
         loadSamplesFromHealth(sampleType: sampleType)
@@ -275,7 +299,19 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
                     )
                 }
 
-            healthKitStore.save(bolusSamples + basalSamples) { _, _ in }
+            healthKitStore.save(bolusSamples + basalSamples) { (success: Bool, error: Error?) -> Void in
+                if success {
+                    for sample in bolusSamples + basalSamples {
+                        debug(
+                            .service,
+                            "Stored insulin entry in HealthKit Store! Metadata: \(String(describing: sample.metadata?.values))"
+                        )
+                    }
+                } else {
+                    debug(.service, "Failed to store insulin entry in HealthKit Store!")
+                    debug(.service, error?.localizedDescription ?? "Unknown error")
+                }
+            }
         }
 
         loadSamplesFromHealth(sampleType: sampleType, withIDs: events.map(\.id))