Przeglądaj źródła

Utilize max bolus limit in Bolus module

In the bolus module, if bolus amount is set over the `Max Bolus` set in `Pump Settings', disable the `Enact bolus` button, change it's text to `Max Bolus exceeded!`, and change alert dialog for external insulin.

If bolus amount is set over 3x Max Bolus, disable the external insulin button as well.

Cherry-picked from https://github.com/Artificial-Pancreas/iAPS/commit/581e3e2838f7ee461df8f86c17db06450d49850e and https://github.com/Artificial-Pancreas/iAPS/commit/7c7cafecf2c10b377d329559b851e417c02ebe96 but had to refactor the alert to get it to build

Co-Authored-By: Jon B Mårtensson <53905247+jon-b-m@users.noreply.github.com>
Mike Plante 2 lat temu
rodzic
commit
5e085b26d1

+ 5 - 2
FreeAPS/Sources/Localizations/Main/sv.lproj/Localizable.strings

@@ -16,8 +16,8 @@
 /* Continue after added carbs without bolus */
 "Continue without bolus" = "Fortsätt utan bolus";
 
-/* Alert when adding large amount without bolusing */
-"\nAmount is more than your Max Bolus setting! \nAre you sure you want to add " = "\nDetta är mer insulin än din maxdos-inställning!\nÄr du säker på att du vill lägga till ";
+/* Alert when adding large amount without bolusing. Don't remove the " " */
+"\nAmount is more than your Max Bolus setting! \nAre you sure you want to add " = "\nDetta är mer insulin än din maxdos-inställning! \nÄr du säker på att du vill lägga till ";
 
 /* Header */
 "Enact Bolus" = "Ge bolus";
@@ -1058,6 +1058,9 @@ Enact a temp Basal or a temp target */
 /* */
 "Bolus failed or inaccurate. Check pump history before repeating." = "Misslyckad eller felaktig bolus. Kontrollera pumpens historik innan du försöker igen.";
 
+/* "Max Bolus Exceeded label" */
+"Max Bolus exceeded!" = "Du kan inte ge mer än din maxinställning!";
+
 /* */
 "Carbs" = "Kolhydrater";
 

+ 4 - 1
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -25,6 +25,7 @@ extension Bolus {
         @Published var expectedDelta: Decimal = 0
         @Published var minPredBG: Decimal = 0
         @Published var units: GlucoseUnits = .mmolL
+        @Published var maxBolus: Decimal = 0
 
         var waitForSuggestionInitial: Bool = false
 
@@ -34,6 +35,7 @@ extension Bolus {
             units = settingsManager.settings.units
             percentage = settingsManager.settings.insulinReqPercentage
             threshold = provider.suggestion?.threshold ?? 0
+            maxBolus = provider.pumpSettings().maxBolus
 
             if waitForSuggestionInitial {
                 apsManager.determineBasal()
@@ -55,7 +57,7 @@ extension Bolus {
                 return
             }
 
-            let maxAmount = Double(min(amount, provider.pumpSettings().maxBolus))
+            let maxAmount = Double(min(amount, maxBolus))
 
             unlockmanager.unlock()
                 .sink { _ in } receiveValue: { [weak self] _ in
@@ -71,6 +73,7 @@ extension Bolus {
                 showModal(for: nil)
                 return
             }
+            amount = min(amount, maxBolus * 3) // Allow for 3 * Max Bolus for non-pump insulin
 
             pumpHistoryStorage.storeEvents(
                 [

+ 29 - 9
FreeAPS/Sources/Modules/Bolus/View/BolusRootView.swift

@@ -78,8 +78,10 @@ extension Bolus {
                     header: { Text("Bolus") }
                     Section {
                         Button { state.add() }
-                        label: { Text("Enact bolus") }
-                            .disabled(state.amount <= 0)
+                        label: { Text(!(state.amount > state.maxBolus) ? "Enact bolus" : "Max Bolus exceeded!") }
+                            .disabled(
+                                state.amount <= 0 || state.amount > state.maxBolus
+                            )
                     }
                     Section {
                         if waitForSuggestion {
@@ -88,16 +90,34 @@ extension Bolus {
                         } else {
                             Button { isAddInsulinAlertPresented = true }
                             label: { Text("Add insulin without actually bolusing") }
-                                .disabled(state.amount <= 0)
+                                .disabled(state.amount <= 0 || state.amount > state.maxBolus * 3)
                         }
                     }
                     .alert(isPresented: $isAddInsulinAlertPresented) {
-                        Alert(
-                            title: Text("Are you sure?"),
-                            message: Text(
-                                NSLocalizedString("Add", comment: "Add insulin without bolusing alert") + " " + formatter
-                                    .string(from: state.amount as NSNumber)! + NSLocalizedString(" U", comment: "Insulin unit") +
-                                    NSLocalizedString(" without bolusing", comment: "Add insulin without bolusing alert")
+                        let isOverMax = state.amount > state.maxBolus ? true : false
+                        let addOverMax = NSLocalizedString(
+                            "\nAmount is more than your Max Bolus setting! \nAre you sure you want to add ",
+                            comment: "Alert"
+                        )
+                        let addUnderMax = NSLocalizedString("Add", comment: "Add insulin without bolusing alert")
+                        let insulinUnit = NSLocalizedString(" U", comment: "Insulin unit")
+                        let withoutBolusing = NSLocalizedString(
+                            " without bolusing",
+                            comment: "Add insulin without bolusing alert"
+                        )
+                        let insulinAmount = formatter.string(from: state.amount as NSNumber)!
+
+                        let overMaxBolusString = addOverMax + insulinAmount + insulinUnit + withoutBolusing + "?"
+                        let underMaxBolusString = addUnderMax + " " + insulinAmount + insulinUnit + withoutBolusing
+
+                        // Actual alert
+                        return Alert(
+                            title: Text(
+                                isOverMax ? "Warning" : "Are you sure?"
+                            ),
+                            message:
+                            Text(
+                                isOverMax ? overMaxBolusString : underMaxBolusString
                             ),
                             primaryButton: .destructive(
                                 Text("Add"),