polscm32 2 лет назад
Родитель
Сommit
93c8c43335

+ 48 - 0
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -469,6 +469,54 @@ extension Bolus {
                 print("meals 1: ID: " + (save.id ?? "").description + " FPU ID: " + (save.fpuID ?? "").description)
             }
         }
+
+        // MARK: EXTERNAL INSULIN
+
+        func addExternalInsulin() async {
+            guard amount > 0 else {
+                showModal(for: nil)
+                return
+            }
+
+            amount = min(amount, maxBolus * 3)
+
+            do {
+                let authenticated = try await unlockmanager.unlock()
+                if authenticated {
+                    storeExternalInsulinEvent()
+                } else {
+                    print("authentication failed")
+                }
+            } catch {
+                print("authentication error: \(error.localizedDescription)")
+            }
+        }
+
+        private func storeExternalInsulinEvent() {
+            pumpHistoryStorage.storeEvents(
+                [
+                    PumpHistoryEvent(
+                        id: UUID().uuidString,
+                        type: .bolus,
+                        timestamp: date,
+                        amount: amount,
+                        duration: nil,
+                        durationMin: nil,
+                        rate: nil,
+                        temp: nil,
+                        carbInput: nil,
+                        isExternal: true
+                    )
+                ]
+            )
+            debug(.default, "External insulin saved to pumphistory.json")
+
+            // Reset amount to 0 for next entry.
+            amount = 0
+
+            // perform determine basal sync
+            apsManager.determineBasalSync()
+        }
     }
 }
 

+ 57 - 10
FreeAPS/Sources/Modules/Bolus/View/AlternativeBolusCalcRootView.swift

@@ -443,18 +443,39 @@ extension Bolus {
 
                     Section {
                         Button {
-                            Task {
-                                await state.add()
-                                state.hideModal()
-                                state.addCarbs()
+                            if !state.externalInsulin {
+                                Task {
+                                    await state.add()
+                                    state.hideModal()
+                                    state.addCarbs()
+                                }
+                            } else {
+                                Task {
+                                    do {
+                                        await state.addExternalInsulin()
+                                        state.hideModal()
+                                        state.addCarbs()
+                                    }
+                                }
                             }
                         }
 
-                        label: { Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus") }
-                            .frame(maxWidth: .infinity, alignment: .center)
-                            .disabled(disabled)
-                            .listRowBackground(!disabled ? Color(.systemBlue) : Color(.systemGray4))
-                            .tint(.white)
+                        label: {
+                            if !state.externalInsulin {
+                                Text(exceededMaxBolus ? "Max Bolus exceeded!" : "Enact bolus")
+                            } else {
+                                Text("Log external insulin")
+                            }
+                        }
+                        .frame(maxWidth: .infinity, alignment: .center)
+                        .disabled(state.externalInsulin ? limitManualBolus : limitPumpBolus)
+                        .listRowBackground(logExternalInsulinBackground)
+                        .tint(logExternalInsulinForeground)
+                    } header: {
+                        if state.amount > state.maxBolus
+                        {
+                            Text("⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!")
+                        }
                     }
                 }
                 if state.amount <= 0 {
@@ -936,9 +957,35 @@ extension Bolus {
             return Decimal(floor(100 * toRound) / 100)
         }
 
-        private var disabled: Bool {
+        private var limitPumpBolus: Bool {
             state.amount <= 0 || state.amount > state.maxBolus
         }
+
+        // MARK: DEFINITIONS FOR ADDING EXTERNAL INSULIN
+
+        private var limitManualBolus: Bool {
+            state.amount <= 0 || state.amount > state.maxBolus * 3
+        }
+
+        private var logExternalInsulinBackground: Color {
+            if state.amount > state.maxBolus {
+                return Color.red
+            } else if state.amount <= 0 || state.amount > state.maxBolus * 3 {
+                return Color(.systemGray4)
+            } else {
+                return Color(.systemBlue)
+            }
+        }
+
+        private var logExternalInsulinForeground: Color {
+            if state.amount > state.maxBolus {
+                return Color.white
+            } else if state.amount <= 0 || state.amount > state.maxBolus * 3 {
+                return Color.secondary
+            } else {
+                return Color.white
+            }
+        }
     }
 
     struct DividerDouble: View {

+ 0 - 48
FreeAPS/Sources/Modules/DataTable/DataTableStateModel.swift

@@ -18,8 +18,6 @@ extension DataTable {
         @Published var meals: [Treatment] = []
         @Published var manualGlucose: Decimal = 0
         @Published var maxBolus: Decimal = 0
-        @Published var externalInsulinAmount: Decimal = 0
-        @Published var externalInsulinDate = Date()
         @Published var waitForSuggestion: Bool = false
         @Published var showExternalInsulin: Bool = false
         @Published var addButtonPressed: Bool = false
@@ -227,52 +225,6 @@ extension DataTable {
             var saveToHealth = [BloodGlucose]()
             saveToHealth.append(saveToJSON)
         }
-
-        func addExternalInsulin() async {
-            guard externalInsulinAmount > 0 else {
-                showModal(for: nil)
-                return
-            }
-
-            externalInsulinAmount = min(externalInsulinAmount, maxBolus * 3)
-
-            do {
-                let authenticated = try await unlockmanager.unlock()
-                if authenticated {
-                    storeExternalInsulinEvent()
-                } else {
-                    print("authentication failed")
-                }
-            } catch {
-                print("authentication error: \(error.localizedDescription)")
-            }
-        }
-
-        private func storeExternalInsulinEvent() {
-            pumpHistoryStorage.storeEvents(
-                [
-                    PumpHistoryEvent(
-                        id: UUID().uuidString,
-                        type: .bolus,
-                        timestamp: externalInsulinDate,
-                        amount: externalInsulinAmount,
-                        duration: nil,
-                        durationMin: nil,
-                        rate: nil,
-                        temp: nil,
-                        carbInput: nil,
-                        isExternal: true
-                    )
-                ]
-            )
-            debug(.default, "External insulin saved to pumphistory.json")
-
-            // Reset amount to 0 for next entry.
-            externalInsulinAmount = 0
-
-            // perform determine basal sync
-            apsManager.determineBasalSync()
-        }
     }
 }
 

+ 8 - 109
FreeAPS/Sources/Modules/DataTable/View/DataTableRootView.swift

@@ -107,10 +107,7 @@ extension DataTable {
                 .toolbar {
                     ToolbarItem(placement: .topBarTrailing) {
                         switch state.mode {
-                        case .treatments: addButton({
-                                state.showExternalInsulin = true
-                                state.externalInsulinDate = Date()
-                            })
+                        case .treatments: EmptyView()
                         case .meals: EmptyView()
                         case .glucose: addButton({
                                 showManualGlucose = true
@@ -122,13 +119,13 @@ extension DataTable {
                 .sheet(isPresented: $showManualGlucose) {
                     addGlucoseView()
                 }
-                .sheet(
-                    isPresented: $state.showExternalInsulin,
-                    onDismiss: { if isAmountUnconfirmed { state.externalInsulinAmount = 0
-                        state.externalInsulinDate = Date() } }
-                ) {
-                    addExternalInsulinView()
-                }
+//                .sheet(
+//                    isPresented: $state.showExternalInsulin,
+//                    onDismiss: { if isAmountUnconfirmed { state.externalInsulinAmount = 0
+//                        state.externalInsulinDate = Date() } }
+//                ) {
+//                    addExternalInsulinView()
+//                }
         }
 
         @ViewBuilder func addButton(_ action: @escaping () -> Void) -> some View {
@@ -392,104 +389,6 @@ extension DataTable {
             }
         }
 
-        @ViewBuilder func addExternalInsulinView() -> some View {
-            NavigationView {
-                ZStack(alignment: .center) {
-                    VStack {
-                        Form {
-                            Section {
-                                HStack {
-                                    Text("Amount")
-                                    Spacer()
-                                    DecimalTextField(
-                                        "0",
-                                        value: $state.externalInsulinAmount,
-                                        formatter: insulinFormatter,
-                                        autofocus: true,
-                                        cleanInput: true
-                                    )
-                                    Text("U").foregroundColor(.secondary)
-                                }
-                            }.listRowBackground(Color.chart)
-
-                            Section {
-                                DatePicker("Date", selection: $state.externalInsulinDate, in: ...Date())
-                            }.listRowBackground(Color.chart)
-
-                            let amountWarningCondition = (state.externalInsulinAmount > state.maxBolus)
-
-                            var listBackgroundColor: Color {
-                                if amountWarningCondition {
-                                    return Color.red
-                                } else if state.externalInsulinAmount <= 0 || state.externalInsulinAmount > state.maxBolus * 3 {
-                                    return Color(.systemGray4)
-                                } else {
-                                    return Color(.systemBlue)
-                                }
-                            }
-
-                            var foregroundColor: Color {
-                                if amountWarningCondition {
-                                    return Color.white
-                                } else if state.externalInsulinAmount <= 0 || state.externalInsulinAmount > state.maxBolus * 3 {
-                                    return Color.secondary
-                                } else {
-                                    return Color.white
-                                }
-                            }
-
-                            Section {
-                                HStack {
-                                    Button {
-                                        Task {
-                                            do {
-                                                await state.addExternalInsulin()
-                                                state.waitForSuggestion = true
-                                                state.addButtonPressed = true
-                                                isAmountUnconfirmed = false
-                                            }
-                                        }
-                                    } label: {
-                                        Text("Log external insulin")
-                                    }
-                                    .foregroundStyle(foregroundColor)
-                                    .frame(maxWidth: .infinity, alignment: .center)
-                                    .disabled(
-                                        state.externalInsulinAmount <= 0 || state.externalInsulinAmount > state.maxBolus * 3
-                                    )
-                                }
-                            }
-                            header: {
-                                if amountWarningCondition
-                                {
-                                    Text("⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!")
-                                }
-                            }
-                            .listRowBackground(listBackgroundColor).tint(.white)
-                        }.scrollContentBackground(.hidden).background(color)
-                    }.blur(radius: state.waitForSuggestion ? 5 : 0)
-
-                    if state.waitForSuggestion {
-                        CustomProgressView(text: "Updating IOB")
-                    }
-                }
-                .onAppear(perform: configureView)
-                .onDisappear {
-                    state.addButtonPressed = false
-                }
-                .navigationTitle("External Insulin")
-                .navigationBarTitleDisplayMode(.inline)
-                .toolbar {
-                    ToolbarItem(placement: .topBarLeading) {
-                        Button("Close") {
-                            state.showExternalInsulin = false
-                            state.externalInsulinAmount = 0
-                        }
-                    }
-                }
-            }
-        }
-
         @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> some View {
             HStack {
                 Text(item.glucose.glucose.map {