Jelajahi Sumber

Nullify HBT when preset; some cleanup in home view

Deniz Cengiz 1 tahun lalu
induk
melakukan
e4219a70bd

+ 10 - 5
FreeAPS/Sources/APS/Storage/TempTargetsStorage.swift

@@ -18,7 +18,7 @@ protocol TempTargetsStorage {
     func loadLatestTempTargetConfigurations(fetchLimit: Int) async -> [NSManagedObjectID]
     func syncDate() -> Date
     func recent() -> [TempTarget]
-    func nightscoutTreatmentsNotUploaded() -> [NightscoutTreatment]
+    func getTempTargetsNotYetUploadedToNightscout() -> [NightscoutTreatment]
     func presets() -> [TempTarget]
     func current() -> TempTarget?
 }
@@ -126,8 +126,13 @@ final class BaseTempTargetsStorage: TempTargetsStorage, Injectable {
             newTempTarget.halfBasalTarget = NSDecimalNumber(decimal: tempTarget.halfBasalTarget ?? 160)
 
             // Set order position if we have a valid count and the temp target is a preset
-            if tempTarget.isPreset == true, presetCount > -1 {
-                newTempTarget.orderPosition = Int16(presetCount + 1)
+            // Nullify half basal target to ensure the latest HBT is used via OpenAPS Manager when sending TT data to oref
+            if tempTarget.isPreset == true {
+                newTempTarget.halfBasalTarget = nil
+
+                if presetCount > -1 {
+                    newTempTarget.orderPosition = Int16(presetCount + 1)
+                }
             }
 
             do {
@@ -174,7 +179,7 @@ final class BaseTempTargetsStorage: TempTargetsStorage, Injectable {
         newTempTarget.name = tempTarget.name
         newTempTarget.target = tempTarget.target
         newTempTarget.isPreset = false // no Preset
-        newTempTarget.halfBasalTarget = tempTarget.halfBasalTarget
+        newTempTarget.halfBasalTarget = tempTarget.halfBasalTarget != 160 ? tempTarget.halfBasalTarget : nil
 
         await viewContext.perform {
             do {
@@ -216,7 +221,7 @@ final class BaseTempTargetsStorage: TempTargetsStorage, Injectable {
         return last
     }
 
-    func nightscoutTreatmentsNotUploaded() -> [NightscoutTreatment] {
+    func getTempTargetsNotYetUploadedToNightscout() -> [NightscoutTreatment] {
         let uploaded = storage.retrieve(OpenAPS.Nightscout.uploadedTempTargets, as: [NightscoutTreatment].self) ?? []
 
         let eventsManual = recent().filter { $0.enteredBy == TempTarget.manual }

+ 1 - 1
FreeAPS/Sources/Modules/Adjustments/AdjustmentsStateModel.swift

@@ -703,7 +703,7 @@ extension OverrideConfig.StateModel {
     func saveScheduledTempTarget() async {
         // Save date to a constant to allow multiple executions of this function at the same time, i.e. allowing for scheduling multiple TTs
         let date = self.date
-        
+
         guard date > Date() else { return }
 
         let tempTarget = TempTarget(

+ 34 - 54
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -286,42 +286,6 @@ extension Home {
             return components.isEmpty ? nil : components.joined(separator: ", ")
         }
 
-        var infoPanel: some View {
-            HStack(alignment: .center) {
-                if state.pumpSuspended {
-                    Text("Pump suspended")
-                        .font(.system(size: 15, weight: .bold)).foregroundColor(.loopGray)
-                        .padding(.leading, 8)
-                } else if let tempBasalString = tempBasalString {
-                    Text(tempBasalString)
-                        .font(.system(size: 15, weight: .bold))
-                        .foregroundColor(.insulin)
-                        .padding(.leading, 8)
-                }
-                if state.totalInsulinDisplayType == .totalInsulinInScope {
-                    Text(
-                        "TINS: \(state.calculateTINS())" +
-                            NSLocalizedString(" U", comment: "Unit in number of units delivered (keep the space character!)")
-                    )
-                    .font(.system(size: 15, weight: .bold))
-                    .foregroundColor(.insulin)
-                }
-
-                if let tempTargetString = tempTargetString {
-                    Text(tempTargetString)
-                        .font(.caption)
-                        .foregroundColor(.secondary)
-                }
-
-                Spacer()
-
-                if state.closedLoop, state.settingsManager.preferences.maxIOB == 0 {
-                    Text("Max IOB: 0").font(.callout).foregroundColor(.orange).padding(.trailing, 20)
-                }
-            }
-            .frame(maxWidth: .infinity, maxHeight: 30)
-        }
-
         var timeInterval: some View {
             HStack(alignment: .center) {
                 ForEach(timeButtons) { button in
@@ -536,6 +500,9 @@ extension Home {
                         .font(.caption)
                 }
             }
+            .onTapGesture {
+                selectedTab = 2
+            }
         }
 
         @ViewBuilder func adjustmentsTempTargetView(_ tempTargetString: String) -> some View {
@@ -550,6 +517,9 @@ extension Home {
                         .font(.caption)
                 }
             }
+            .onTapGesture {
+                selectedTab = 2
+            }
         }
 
         @ViewBuilder func adjustmentsCancelView(_ cancelAction: @escaping () -> Void) -> some View {
@@ -660,24 +630,25 @@ extension Home {
                             Spacer()
                             adjustmentsCancelTempTargetView()
                         }
-                    } else {
-                        VStack {
-                            Text("No Active Adjustment")
-                                .font(.subheadline)
-                                .frame(maxWidth: .infinity, alignment: .leading)
-                            Text("Profile at 100 %")
-                                .font(.caption)
-                                .frame(maxWidth: .infinity, alignment: .leading)
-                        }.padding(.leading, 10)
-
-                        Spacer()
-
-                        /// to ensure the same position....
-                        Image(systemName: "xmark.app")
-                            .font(.system(size: 25))
-                            // clear color for the icon
-                            .foregroundStyle(Color.clear)
                     }
+//                    else {
+//                        VStack {
+//                            Text("No Active Adjustment")
+//                                .font(.subheadline)
+//                                .frame(maxWidth: .infinity, alignment: .leading)
+//                            Text("Profile at 100 %")
+//                                .font(.caption)
+//                                .frame(maxWidth: .infinity, alignment: .leading)
+//                        }.padding(.leading, 10)
+//
+//                        Spacer()
+//
+//                        /// to ensure the same position....
+//                        Image(systemName: "xmark.app")
+//                            .font(.system(size: 25))
+//                            // clear color for the icon
+//                            .foregroundStyle(Color.clear)
+//                    }
                 }.padding(.horizontal, 10)
                     .confirmationDialog("Adjustment to Stop", isPresented: $showCancelConfirmDialog) {
                         Button("Stop Override", role: .destructive) {
@@ -692,6 +663,15 @@ extension Home {
                                 await state.cancelTempTarget(withID: objectID)
                             }
                         }
+                        Button("Stop All Adjustments", role: .destructive) {
+                            Task {
+                                guard let overrideObjectID = latestOverride.first?.objectID else { return }
+                                await state.cancelOverride(withID: overrideObjectID)
+
+                                guard let tempTargetObjectID = latestTempTarget.first?.objectID else { return }
+                                await state.cancelTempTarget(withID: tempTargetObjectID)
+                            }
+                        }
                     } message: {
                         Text("Select Adjustment")
                     }
@@ -812,7 +792,7 @@ extension Home {
 
                     if let progress = state.bolusProgress {
                         bolusView(geo: geo, progress).padding(.bottom, UIDevice.adjustPadding(min: nil, max: 40))
-                    } else {
+                    } else if (tempBasalString != nil) || (overrideString != nil) {
                         adjustmentView(geo: geo).padding(.bottom, UIDevice.adjustPadding(min: nil, max: 40))
                     }
                 }

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

@@ -715,7 +715,7 @@ final class BaseNightscoutManager: NightscoutManager, Injectable {
 
     func uploadTempTargets() async {
         await uploadTreatments(
-            tempTargetsStorage.nightscoutTreatmentsNotUploaded(),
+            tempTargetsStorage.getTempTargetsNotYetUploadedToNightscout(),
             fileToSave: OpenAPS.Nightscout.uploadedTempTargets
         )
     }