Explorar el Código

logic for progress view

polscm32 hace 2 años
padre
commit
6fd75469a1

+ 26 - 0
FreeAPS/Sources/Helpers/CustomProgressView.swift

@@ -0,0 +1,26 @@
+import SwiftUI
+
+struct CustomProgressView: View {
+    let text: String
+
+    @Environment(\.colorScheme) var colorScheme
+
+    var body: some View {
+        ZStack(alignment: .center) {
+            RoundedRectangle(cornerRadius: 15)
+                .fill(Color(.systemGray4))
+                .clipShape(RoundedRectangle(cornerRadius: 15))
+                .shadow(
+                    color: colorScheme == .dark ? Color(red: 0.02745098039, green: 0.1098039216, blue: 0.1411764706) :
+                        Color.black.opacity(0.33),
+                    radius: 3
+                )
+                .padding(.horizontal, 10)
+                .frame(maxHeight: UIScreen.main.bounds.height / 6)
+
+            ProgressView {
+                Text(text)
+            }
+        }
+    }
+}

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

@@ -581,7 +581,7 @@ extension Bolus {
                     + " / " +
                     + " / " +
                     state.isf.formatted()
                     state.isf.formatted()
                     + " ≈ " +
                     + " ≈ " +
-                self.insulinRounder(state.targetDifferenceInsulin).formatted()
+                    self.insulinRounder(state.targetDifferenceInsulin).formatted()
 
 
                 Text(secondRow).foregroundColor(.secondary).gridColumnAlignment(.leading)
                 Text(secondRow).foregroundColor(.secondary).gridColumnAlignment(.leading)
 
 
@@ -677,7 +677,7 @@ extension Bolus {
                         + " / " +
                         + " / " +
                         state.isf.formatted()
                         state.isf.formatted()
                         + " ≈ " +
                         + " ≈ " +
-                    self.insulinRounder(state.fifteenMinInsulin).formatted()
+                        self.insulinRounder(state.fifteenMinInsulin).formatted()
                 )
                 )
                 .foregroundColor(.secondary)
                 .foregroundColor(.secondary)
                 .gridColumnAlignment(.leading)
                 .gridColumnAlignment(.leading)

+ 17 - 2
FreeAPS/Sources/Modules/DataTable/DataTableStateModel.swift

@@ -20,6 +20,9 @@ extension DataTable {
         @Published var maxBolus: Decimal = 0
         @Published var maxBolus: Decimal = 0
         @Published var externalInsulinAmount: Decimal = 0
         @Published var externalInsulinAmount: Decimal = 0
         @Published var externalInsulinDate = Date()
         @Published var externalInsulinDate = Date()
+        @Published var waitForSuggestion: Bool = false
+        @Published var showExternalInsulin: Bool = false
+        @Published var addButtonPressed: Bool = false
 
 
         var units: GlucoseUnits = .mmolL
         var units: GlucoseUnits = .mmolL
         var historyLayout: HistoryLayout = .twoTabs
         var historyLayout: HistoryLayout = .twoTabs
@@ -35,6 +38,7 @@ extension DataTable {
             broadcaster.register(TempTargetsObserver.self, observer: self)
             broadcaster.register(TempTargetsObserver.self, observer: self)
             broadcaster.register(CarbsObserver.self, observer: self)
             broadcaster.register(CarbsObserver.self, observer: self)
             broadcaster.register(GlucoseObserver.self, observer: self)
             broadcaster.register(GlucoseObserver.self, observer: self)
+            broadcaster.register(SuggestionObserver.self, observer: self)
         }
         }
 
 
         private func setupTreatments() {
         private func setupTreatments() {
@@ -265,8 +269,8 @@ extension DataTable {
 
 
             // Reset amount to 0 for next entry.
             // Reset amount to 0 for next entry.
             externalInsulinAmount = 0
             externalInsulinAmount = 0
-            
-            //perform determine basal sync
+
+            // perform determine basal sync
             apsManager.determineBasalSync()
             apsManager.determineBasalSync()
         }
         }
     }
     }
@@ -300,3 +304,14 @@ extension DataTable.StateModel:
         setupGlucose()
         setupGlucose()
     }
     }
 }
 }
+
+extension DataTable.StateModel: SuggestionObserver {
+    func suggestionDidUpdate(_: Suggestion) {
+        DispatchQueue.main.async {
+            self.waitForSuggestion = false
+            if self.addButtonPressed {
+                self.showExternalInsulin = false
+            }
+        }
+    }
+}

+ 12 - 6
FreeAPS/Sources/Modules/DataTable/View/DataTableRootView.swift

@@ -12,7 +12,6 @@ extension DataTable {
         @State private var alertTreatmentToDelete: Treatment?
         @State private var alertTreatmentToDelete: Treatment?
         @State private var alertGlucoseToDelete: Glucose?
         @State private var alertGlucoseToDelete: Glucose?
 
 
-        @State private var showExternalInsulin: Bool = false
         @State private var showFutureEntries: Bool = false // default to hide future entries
         @State private var showFutureEntries: Bool = false // default to hide future entries
         @State private var showManualGlucose: Bool = false
         @State private var showManualGlucose: Bool = false
         @State private var isAmountUnconfirmed: Bool = true
         @State private var isAmountUnconfirmed: Bool = true
@@ -109,7 +108,7 @@ extension DataTable {
                     ToolbarItem(placement: .topBarTrailing) {
                     ToolbarItem(placement: .topBarTrailing) {
                         switch state.mode {
                         switch state.mode {
                         case .treatments: addButton({
                         case .treatments: addButton({
-                                showExternalInsulin = true
+                                state.showExternalInsulin = true
                                 state.externalInsulinDate = Date()
                                 state.externalInsulinDate = Date()
                             })
                             })
                         case .meals: EmptyView()
                         case .meals: EmptyView()
@@ -123,8 +122,11 @@ extension DataTable {
                 .sheet(isPresented: $showManualGlucose) {
                 .sheet(isPresented: $showManualGlucose) {
                     addGlucoseView()
                     addGlucoseView()
                 }
                 }
-                .sheet(isPresented: $showExternalInsulin, onDismiss: { if isAmountUnconfirmed { state.externalInsulinAmount = 0
-                    state.externalInsulinDate = Date() } }) {
+                .sheet(
+                    isPresented: $state.showExternalInsulin,
+                    onDismiss: { if isAmountUnconfirmed { state.externalInsulinAmount = 0
+                        state.externalInsulinDate = Date() } }
+                ) {
                     addExternalInsulinView()
                     addExternalInsulinView()
                 }
                 }
         }
         }
@@ -441,8 +443,9 @@ extension DataTable {
                                     Task {
                                     Task {
                                         do {
                                         do {
                                             await state.addExternalInsulin()
                                             await state.addExternalInsulin()
+                                            state.waitForSuggestion = true
+                                            state.addButtonPressed = true
                                             isAmountUnconfirmed = false
                                             isAmountUnconfirmed = false
-                                            showExternalInsulin = false
                                         }
                                         }
                                     }
                                     }
                                 } label: {
                                 } label: {
@@ -465,12 +468,15 @@ extension DataTable {
                     }.scrollContentBackground(.hidden).background(color)
                     }.scrollContentBackground(.hidden).background(color)
                 }
                 }
                 .onAppear(perform: configureView)
                 .onAppear(perform: configureView)
+                .onDisappear {
+                    state.addButtonPressed = false
+                }
                 .navigationTitle("External Insulin")
                 .navigationTitle("External Insulin")
                 .navigationBarTitleDisplayMode(.inline)
                 .navigationBarTitleDisplayMode(.inline)
                 .toolbar {
                 .toolbar {
                     ToolbarItem(placement: .topBarLeading) {
                     ToolbarItem(placement: .topBarLeading) {
                         Button("Close") {
                         Button("Close") {
-                            showExternalInsulin = false
+                            state.showExternalInsulin = false
                             state.externalInsulinAmount = 0
                             state.externalInsulinAmount = 0
                         }
                         }
                     }
                     }

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

@@ -436,37 +436,45 @@ extension MainChartView {
                 ).foregroundStyle(Color.clear)
                 ).foregroundStyle(Color.clear)
                 /// temp basal rects
                 /// temp basal rects
                 ForEach(TempBasals) { temp in
                 ForEach(TempBasals) { temp in
-                                 /// calculate end time of temp basal adding duration to start time
-                                 let end = temp.timestamp + (temp.durationMin ?? 0).minutes.timeInterval
-                                 let now = Date()
-
-                                 /// ensure that temp basals that are set cannot exceed current date -> i.e. scheduled temp basals are not shown
-                                 /// we could display scheduled temp basals with opacity etc... in the future
-                                 let maxEndTime = min(end, now)
-
-                                 /// find next basal entry and if available set end of current entry to start of next entry
-                                 if let nextTemp = TempBasals.first(where: { $0.timestamp > temp.timestamp }) {
-                                     let nextTempStart = nextTemp.timestamp
-
-                                     RectangleMark(
-                                         xStart: .value("start", temp.timestamp),
-                                         xEnd: .value("end", nextTempStart),
-                                         yStart: .value("rate-start", 0),
-                                         yEnd: .value("rate-end", temp.rate ?? 0)
-                                     ).foregroundStyle(Color.insulin.opacity(0.2))
-                                     LineMark(x: .value("Start Date", temp.timestamp), y: .value("Amount", temp.rate ?? 0)).lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
-                                     LineMark(x: .value("End Date", nextTempStart), y: .value("Amount", temp.rate ?? 0)).lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
-                                 } else {
-                                     RectangleMark(
-                                         xStart: .value("start", temp.timestamp),
-                                         xEnd: .value("end", maxEndTime),
-                                         yStart: .value("rate-start", 0),
-                                         yEnd: .value("rate-end", temp.rate ?? 0)
-                                     ).foregroundStyle(Color.insulin.opacity(0.2))
-                                     LineMark(x: .value("Start Date", temp.timestamp), y: .value("Amount", temp.rate ?? 0)).lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
-                                     LineMark(x: .value("End Date", maxEndTime), y: .value("Amount", temp.rate ?? 0)).lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
-                                 }
-                             }
+                    /// calculate end time of temp basal adding duration to start time
+                    let end = temp.timestamp + (temp.durationMin ?? 0).minutes.timeInterval
+                    let now = Date()
+
+                    /// ensure that temp basals that are set cannot exceed current date -> i.e. scheduled temp basals are not shown
+                    /// we could display scheduled temp basals with opacity etc... in the future
+                    let maxEndTime = min(end, now)
+
+                    /// find next basal entry and if available set end of current entry to start of next entry
+                    if let nextTemp = TempBasals.first(where: { $0.timestamp > temp.timestamp }) {
+                        let nextTempStart = nextTemp.timestamp
+
+                        RectangleMark(
+                            xStart: .value("start", temp.timestamp),
+                            xEnd: .value("end", nextTempStart),
+                            yStart: .value("rate-start", 0),
+                            yEnd: .value("rate-end", temp.rate ?? 0)
+                        ).foregroundStyle(Color.insulin.opacity(0.2))
+                        
+                        LineMark(x: .value("Start Date", temp.timestamp), y: .value("Amount", temp.rate ?? 0))
+                            .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
+                        
+                        LineMark(x: .value("End Date", nextTempStart), y: .value("Amount", temp.rate ?? 0))
+                            .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
+                    } else {
+                        RectangleMark(
+                            xStart: .value("start", temp.timestamp),
+                            xEnd: .value("end", maxEndTime),
+                            yStart: .value("rate-start", 0),
+                            yEnd: .value("rate-end", temp.rate ?? 0)
+                        ).foregroundStyle(Color.insulin.opacity(0.2))
+                        
+                        LineMark(x: .value("Start Date", temp.timestamp), y: .value("Amount", temp.rate ?? 0))
+                            .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
+                        
+                        LineMark(x: .value("End Date", maxEndTime), y: .value("Amount", temp.rate ?? 0))
+                            .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.insulin)
+                    }
+                }
                 /// dashed profile line
                 /// dashed profile line
                 ForEach(BasalProfiles, id: \.self) { profile in
                 ForEach(BasalProfiles, id: \.self) { profile in
                     LineMark(
                     LineMark(