Procházet zdrojové kódy

Merge branch 'dev' into nonpump-insulin-dialog

Jon B Mårtensson před 2 roky
rodič
revize
1b7e064844

+ 1 - 1
FreeAPS/Sources/APS/CGM/DexcomSourceG5.swift

@@ -148,7 +148,7 @@ extension DexcomSourceG5: CGMManagerDelegate {
                 let quantity = newGlucoseSample.quantity
                 let value = Int(quantity.doubleValue(for: .milligramsPerDeciliter))
                 return BloodGlucose(
-                    _id: newGlucoseSample.syncIdentifier,
+                    _id: UUID().uuidString,
                     sgv: value,
                     direction: .init(trendType: newGlucoseSample.trend),
                     date: Decimal(Int(newGlucoseSample.date.timeIntervalSince1970 * 1000)),

+ 1 - 1
FreeAPS/Sources/APS/CGM/DexcomSourceG6.swift

@@ -153,7 +153,7 @@ extension DexcomSourceG6: CGMManagerDelegate {
                     let quantity = newGlucoseSample.quantity
                     let value = Int(quantity.doubleValue(for: .milligramsPerDeciliter))
                     return BloodGlucose(
-                        _id: newGlucoseSample.syncIdentifier,
+                        _id: UUID().uuidString,
                         sgv: value,
                         direction: .init(trendType: newGlucoseSample.trend),
                         date: Decimal(Int(newGlucoseSample.date.timeIntervalSince1970 * 1000)),

+ 1 - 1
FreeAPS/Sources/APS/CGM/dexcomSourceG7.swift

@@ -148,7 +148,7 @@ extension DexcomSourceG7: CGMManagerDelegate {
                 let quantity = newGlucoseSample.quantity
                 let value = Int(quantity.doubleValue(for: .milligramsPerDeciliter))
                 return BloodGlucose(
-                    _id: newGlucoseSample.syncIdentifier,
+                    _id: UUID().uuidString,
                     sgv: value,
                     direction: .init(trendType: newGlucoseSample.trend),
                     date: Decimal(Int(newGlucoseSample.date.timeIntervalSince1970 * 1000)),

+ 2 - 2
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -46,8 +46,8 @@ struct MainChartView: View {
     }
 
     private enum Command {
-        static let open = "🟢"
-        static let closed = "🔴"
+        static let open = "🔴"
+        static let closed = "🟢"
         static let suspend = "❌"
         static let resume = "✅"
         static let tempbasal = "basal"

+ 3 - 1
FreeAPS/Sources/Modules/NightscoutConfig/NightscoutConfigStateModel.swift

@@ -266,7 +266,9 @@ extension NightscoutConfig {
                                 debug(.service, "Settings have been imported and the Basals saved to pump!")
                                 // DIA. Save if changed.
                                 let dia = fetchedProfile.dia
-                                if dia != self.dia, dia <= 0 {
+                                print("dia: " + dia.description)
+                                print("pump dia: " + self.dia.description)
+                                if dia != self.dia, dia >= 0 {
                                     let file = PumpSettings(
                                         insulinActionCurve: dia,
                                         maxBolus: self.maxBolus,

+ 0 - 6
FreeAPS/Sources/Modules/PreferencesEditor/PreferencesEditorStateModel.swift

@@ -23,12 +23,6 @@ extension PreferencesEditor {
 
             let mainFields = [
                 Field(
-                    displayName: NSLocalizedString("Insulin curve", comment: "Insulin curve"),
-                    type: .insulinCurve(keypath: \.curve),
-                    infoText: "Insulin curve info",
-                    settable: self
-                ),
-                Field(
                     displayName: NSLocalizedString("Max IOB", comment: "Max IOB"),
                     type: .decimal(keypath: \.maxIOB),
                     infoText: NSLocalizedString(

+ 17 - 25
FreeAPS/Sources/Services/HealthKit/HealthKitManager.swift

@@ -227,23 +227,22 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
               events.isNotEmpty
         else { return }
 
-        func delete(syncIds: [String]?) {
-            syncIds?.forEach { syncID in
+        func save(bolusToModify: [InsulinBolus], bolus: [InsulinBolus], basal: [InsulinBasal]) {
+            // first step : delete the HK value
+            // second step : recreate with the new value !
+            bolusToModify.forEach { syncID in
                 let predicate = HKQuery.predicateForObjects(
                     withMetadataKey: HKMetadataKeySyncIdentifier,
                     operatorType: .equalTo,
                     value: syncID
                 )
-
                 self.healthKitStore.deleteObjects(of: sampleType, predicate: predicate) { _, _, error in
                     guard let error = error else { return }
                     warning(.service, "Cannot delete sample with syncID: \(syncID)", error: error)
                 }
             }
-        }
-
-        func save(bolus: [InsulinBolus], basal: [InsulinBasal]) {
-            let bolusSamples = bolus
+            let bolusTotal = bolus + bolusToModify
+            let bolusSamples = bolusTotal
                 .map {
                     HKQuantitySample(
                         type: sampleType,
@@ -280,30 +279,21 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
             healthKitStore.save(bolusSamples + basalSamples) { _, _ in }
         }
 
-        // delete existing event in HK where the amount is not the last value in the pumphistory
         loadSamplesFromHealth(sampleType: sampleType, withIDs: events.map(\.id))
             .receive(on: processQueue)
-            .compactMap { samples -> [String] in
+            .compactMap { samples -> ([InsulinBolus], [InsulinBolus], [InsulinBasal]) in
                 let sampleIDs = samples.compactMap(\.syncIdentifier)
-                let bolusToDelete = events
+                let bolusToModify = events
                     .filter { $0.type == .bolus && sampleIDs.contains($0.id) }
-                    .compactMap { event -> String? in
+                    .compactMap { event -> InsulinBolus? in
                         guard let amount = event.amount else { return nil }
                         guard let sampleAmount = samples.first(where: { $0.syncIdentifier == event.id }) as? HKQuantitySample
                         else { return nil }
                         if Double(amount) != sampleAmount.quantity.doubleValue(for: .internationalUnit()) {
-                            return sampleAmount.syncIdentifier
+                            return InsulinBolus(id: sampleAmount.syncIdentifier!, amount: amount, date: event.timestamp)
                         } else { return nil }
                     }
-                return bolusToDelete
-            }
-            .sink(receiveValue: delete)
-            .store(in: &lifetime)
 
-        loadSamplesFromHealth(sampleType: sampleType, withIDs: events.map(\.id))
-            .receive(on: processQueue)
-            .compactMap { samples -> ([InsulinBolus], [InsulinBasal]) in
-                let sampleIDs = samples.compactMap(\.syncIdentifier)
                 let bolus = events
                     .filter { $0.type == .bolus && !sampleIDs.contains($0.id) }
                     .compactMap { event -> InsulinBolus? in
@@ -348,7 +338,7 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
                             endDelivery: nextBasalEvent.timestamp
                         )
                     }
-                return (bolus, basal)
+                return (bolusToModify, bolus, basal)
             }
             .sink(receiveValue: save)
             .store(in: &lifetime)
@@ -407,13 +397,14 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
 
     /// Try to load samples from Health store
     private func loadSamplesFromHealth(
-        sampleType: HKQuantityType
+        sampleType: HKQuantityType,
+        limit: Int = 100
     ) -> Future<[HKSample], Never> {
         Future { promise in
             let query = HKSampleQuery(
                 sampleType: sampleType,
                 predicate: nil,
-                limit: 1000,
+                limit: limit,
                 sortDescriptors: nil
             ) { _, results, _ in
                 promise(.success((results as? [HKQuantitySample]) ?? []))
@@ -425,7 +416,8 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
     /// Try to load samples from Health store with id and do some work
     private func loadSamplesFromHealth(
         sampleType: HKQuantityType,
-        withIDs ids: [String]
+        withIDs ids: [String],
+        limit: Int = 100
     ) -> Future<[HKSample], Never> {
         Future { promise in
             let predicate = HKQuery.predicateForObjects(
@@ -436,7 +428,7 @@ final class BaseHealthKitManager: HealthKitManager, Injectable, CarbsObserver, P
             let query = HKSampleQuery(
                 sampleType: sampleType,
                 predicate: predicate,
-                limit: 1000,
+                limit: limit,
                 sortDescriptors: nil
             ) { _, results, _ in
                 promise(.success((results as? [HKQuantitySample]) ?? []))

+ 4 - 1
FreeAPS/Sources/Services/Network/NightscoutManager.swift

@@ -610,9 +610,12 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
         guard !glucose.isEmpty, let nightscout = nightscoutAPI, isUploadEnabled, isUploadGlucoseEnabled else {
             return
         }
+        // check if unique code
+        // var uuid = UUID(uuidString: yourString) This will return nil if yourString is not a valid UUID
+        let glucoseWithoutCorrectID = glucose.filter { UUID(uuidString: $0._id) != nil }
 
         processQueue.async {
-            glucose.chunks(ofCount: 100)
+            glucoseWithoutCorrectID.chunks(ofCount: 100)
                 .map { chunk -> AnyPublisher<Void, Error> in
                     nightscout.uploadGlucose(Array(chunk))
                 }