浏览代码

Adopt nightscout/Trio#421 PR to fix basal total calc; adapt onChange to iOS 17

Deniz Cengiz 1 年之前
父节点
当前提交
64bfdd4a76

+ 6 - 0
FreeAPS/Sources/Modules/BasalProfileEditor/BasalProfileEditorStateModel.swift

@@ -114,7 +114,13 @@ extension BasalProfileEditor {
                 if self.items != sorted {
                     self.items = sorted
                 }
+                self.calcTotal()
             }
         }
+
+        func availableTimeIndices(_ itemIndex: Int) -> [Int] {
+            let usedIndicesByOtherItems = items.filter { $0 != items[itemIndex] }.map(\.timeIndex)
+            return (0 ..< timeValues.count).filter { !usedIndicesByOtherItems.contains($0) }
+        }
     }
 }

+ 4 - 6
FreeAPS/Sources/Modules/BasalProfileEditor/View/BasalProfileEditorRootView.swift

@@ -84,9 +84,7 @@ extension BasalProfileEditor {
                     dismissButton: .default(Text("Close"))
                 )
             }
-            .onChange(of: state.items) { _ in
-                state.calcTotal()
-            }
+            .onChange(of: state.items) { state.calcTotal() }
             .scrollContentBackground(.hidden).background(color)
             .onAppear(perform: configureView)
             .navigationTitle("Basal Profile")
@@ -118,12 +116,12 @@ extension BasalProfileEditor {
                             ).tag(i)
                         }
                     }
-                    .onChange(of: state.items[index].rateIndex, perform: { _ in state.calcTotal() })
+                    .onChange(of: state.items[index].rateIndex, { state.calcTotal() })
                 }.listRowBackground(Color.chart)
 
                 Section {
                     Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
-                        ForEach(0 ..< state.timeValues.count, id: \.self) { i in
+                        ForEach(state.availableTimeIndices(index), id: \.self) { i in
                             Text(
                                 self.dateFormatter
                                     .string(from: Date(
@@ -133,7 +131,7 @@ extension BasalProfileEditor {
                             ).tag(i)
                         }
                     }
-                    .onChange(of: state.items[index].timeIndex, perform: { _ in state.calcTotal() })
+                    .onChange(of: state.items[index].timeIndex, { state.calcTotal() })
                 }.listRowBackground(Color.chart)
             }
             .padding(.top)