From 6ce9fbeb5a78bbb2aec319d4afadf9f0ff3c7756 Mon Sep 17 00:00:00 2001 From: Auggie Fisher Date: Sun, 7 Jul 2024 04:32:42 -0400 Subject: [PATCH] Fix shortcut bolus safety guards - Block (with an alert) boluses that exceed the user-configured max (whether this is max pump max bolus or recommended amount) - Addresses this request https://github.com/nightscout/Trio/pull/177#issuecomment-2192687780 --- .../ShortcutsDetail/ShortcutsDetail.xcstrings | 6 ++++ .../Shortcuts/Bolus/BolusIntentRequest.swift | 33 ++++++++++++++----- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/FreeAPS/Sources/Localizations/ShortcutsDetail/ShortcutsDetail.xcstrings b/FreeAPS/Sources/Localizations/ShortcutsDetail/ShortcutsDetail.xcstrings index dc72ce17e..097dc9eb7 100644 --- a/FreeAPS/Sources/Localizations/ShortcutsDetail/ShortcutsDetail.xcstrings +++ b/FreeAPS/Sources/Localizations/ShortcutsDetail/ShortcutsDetail.xcstrings @@ -4436,6 +4436,12 @@ } } } + }, + "The bolus cannot be larger than the pump setting max bolus" : { + + }, + "The bolus cannot be larger than the suggested insulin" : { + }, "the bolus is not allowed with shortcuts" : { "localizations" : { diff --git a/FreeAPS/Sources/Shortcuts/Bolus/BolusIntentRequest.swift b/FreeAPS/Sources/Shortcuts/Bolus/BolusIntentRequest.swift index 9f1bd763d..a79e16162 100644 --- a/FreeAPS/Sources/Shortcuts/Bolus/BolusIntentRequest.swift +++ b/FreeAPS/Sources/Shortcuts/Bolus/BolusIntentRequest.swift @@ -10,22 +10,37 @@ import Foundation func bolus(_ bolusAmount: Double) throws -> LocalizedStringResource { var bolusQuantity: Decimal = 0 switch settingsManager.settings.bolusShortcut { + + //Block boluses if they are disabled case .notAllowed: return LocalizedStringResource( - "the bolus is not allowed with shortcuts", + "Bolusing is not allowed with shortcuts.", table: "ShortcutsDetail" ) + + //Block any bolus attempted if it is larger than the max bolus in settings case .limitBolusMax: - bolusQuantity = apsManager - .roundBolus(amount: min(settingsManager.pumpSettings.maxBolus, Decimal(bolusAmount))) + if Decimal(bolusAmount) > settingsManager.pumpSettings.maxBolus { + return LocalizedStringResource( + "The bolus cannot be larger than the pump setting max bolus.", + table: "ShortcutsDetail" + ) + } else { + bolusQuantity = apsManager.roundBolus(amount: Decimal(bolusAmount)) + } + + //Block any bolus attempted if it is larger than the max bolus in settings case .limitInsulinSuggestion: let insulinSuggestion = suggestion?.insulinForManualBolus ?? 0 - - bolusQuantity = apsManager - .roundBolus(amount: min( - insulinSuggestion * (settingsManager.settings.insulinReqPercentage / 100), - Decimal(bolusAmount) - )) + if Decimal(bolusAmount) > insulinSuggestion { + return LocalizedStringResource( + "The bolus cannot be larger than the suggested insulin.", + table: "ShortcutsDetail" + ) + } else { + bolusQuantity = apsManager + .roundBolus(amount: Decimal(bolusAmount)) + } } apsManager.enactBolus(amount: Double(bolusQuantity), isSMB: false)