Przeglądaj źródła

add insulin total in basal profile and autotune

(cherry picked from commit 660d08df243ae19994fb2b32e510966683fe4d04)
avouspierre 3 lat temu
rodzic
commit
ceb9c40ab8

+ 10 - 0
FreeAPS/Sources/Modules/AutotuneConfig/View/AutotuneConfigRootView.swift

@@ -72,6 +72,16 @@ extension AutotuneConfig {
                                 Text("U/hr").foregroundColor(.secondary)
                             }
                         }
+                        HStack {
+                            Text("Total")
+                                .bold()
+                                .foregroundColor(.primary)
+                            Spacer()
+                            Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
+                                .foregroundColor(.primary) +
+                                Text(" U/day")
+                                .foregroundColor(.secondary)
+                        }
                     }
 
                     Section {

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

@@ -4,6 +4,7 @@ extension BasalProfileEditor {
     final class StateModel: BaseStateModel<Provider> {
         @Published var syncInProgress = false
         @Published var items: [Item] = []
+        @Published var total: Decimal = 0.0
 
         let timeValues = stride(from: 0.0, to: 1.days.timeInterval, by: 30.minutes.timeInterval).map { $0 }
 
@@ -22,6 +23,24 @@ extension BasalProfileEditor {
                 let rateIndex = rateValues.firstIndex(of: value.rate) ?? 0
                 return Item(rateIndex: rateIndex, timeIndex: timeIndex)
             }
+            calcTotal()
+        }
+
+        func calcTotal() {
+            let profile = items.map { item -> BasalProfileEntry in
+                let fotmatter = DateFormatter()
+                fotmatter.timeZone = TimeZone(secondsFromGMT: 0)
+                fotmatter.dateFormat = "HH:mm:ss"
+                let date = Date(timeIntervalSince1970: self.timeValues[item.timeIndex])
+                let minutes = Int(date.timeIntervalSince1970 / 60)
+                let rate = self.rateValues[item.rateIndex]
+                return BasalProfileEntry(start: fotmatter.string(from: date), minutes: minutes, rate: rate)
+            }
+
+            var profileWith24hours = profile.map(\.minutes)
+            profileWith24hours.append(24 * 60)
+            let pr2 = zip(profile, profileWith24hours.dropFirst())
+            total = pr2.reduce(0) { $0 + (Decimal($1.1 - $1.0.minutes) / 60) * $1.0.rate }
         }
 
         func add() {
@@ -35,6 +54,7 @@ extension BasalProfileEditor {
             let newItem = Item(rateIndex: rate, timeIndex: time)
 
             items.append(newItem)
+            calcTotal()
         }
 
         func save() {

+ 15 - 0
FreeAPS/Sources/Modules/BasalProfileEditor/View/BasalProfileEditorRootView.swift

@@ -28,6 +28,18 @@ extension BasalProfileEditor {
                 }
                 Section {
                     HStack {
+                        Text("Total")
+                            .bold()
+                            .foregroundColor(.primary)
+                        Spacer()
+                        Text(rateFormatter.string(from: state.total as NSNumber) ?? "0")
+                            .foregroundColor(.primary) +
+                            Text(" U/day")
+                            .foregroundColor(.secondary)
+                    }
+                }
+                Section {
+                    HStack {
                         if state.syncInProgress {
                             ProgressView().padding(.trailing, 10)
                         }
@@ -69,6 +81,7 @@ extension BasalProfileEditor {
                                 ).tag(i)
                             }
                         }
+                        .onChange(of: state.items[index].rateIndex, perform: { _ in state.calcTotal() })
                         .frame(maxWidth: geometry.size.width / 2)
                         .clipped()
 
@@ -83,6 +96,7 @@ extension BasalProfileEditor {
                                 ).tag(i)
                             }
                         }
+                        .onChange(of: state.items[index].timeIndex, perform: { _ in state.calcTotal() })
                         .frame(maxWidth: geometry.size.width / 2)
                         .clipped()
                     }
@@ -132,6 +146,7 @@ extension BasalProfileEditor {
         private func onDelete(offsets: IndexSet) {
             state.items.remove(atOffsets: offsets)
             state.validate()
+            state.calcTotal()
         }
     }
 }