Просмотр исходного кода

external insulin to shortcut

Add toggle for external insulin to shortcut action #528.
Not dependant of the settings configuration but maxBolus is checked.
Pierre L 1 год назад
Родитель
Сommit
b9a8769de4

+ 30 - 0
Trio/Sources/Localizations/Main/Localizable.xcstrings

@@ -20088,6 +20088,16 @@
         }
       }
     },
+    "A external bolus of %@ U of insulin was recorded." : {
+      "localizations" : {
+        "fr" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Un bolus externe de %@ U d'insuline a été enregistré."
+          }
+        }
+      }
+    },
     "A few important notes…" : {
       "localizations" : {
         "bg" : {
@@ -95686,6 +95696,16 @@
         }
       }
     },
+    "External Insulin?" : {
+      "localizations" : {
+        "fr" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Insuline externe ?"
+          }
+        }
+      }
+    },
     "External:" : {
       "localizations" : {
         "bg" : {
@@ -114711,6 +114731,16 @@
         }
       }
     },
+    "If toggled, Insulin will be added to IOB but it will not be delivered" : {
+      "localizations" : {
+        "fr" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Si cette option est activée, l'insuline sera ajoutée à l'IOB mais ne sera pas administrée."
+          }
+        }
+      }
+    },
     "If toggled, you will need to confirm before applying" : {
       "localizations" : {
         "bg" : {

+ 1 - 0
Trio/Sources/Shortcuts/BaseIntentsRequest.swift

@@ -15,6 +15,7 @@ import Swinject
     @Injected() var apsManager: APSManager!
     @Injected() var overrideStorage: OverrideStorage!
     @Injected() var liveActivityManager: LiveActivityManager!
+    @Injected() var pumpHistoryStorage: PumpHistoryStorage!
 
     let resolver: Resolver
 

+ 20 - 6
Trio/Sources/Shortcuts/Bolus/BolusIntent.swift

@@ -26,6 +26,13 @@ import Swinject
     ) var bolusQuantity: Double
 
     @Parameter(
+        title: LocalizedStringResource("External Insulin"),
+        description: LocalizedStringResource("If toggled, Insulin will be added to IOB but it will not be delivered"),
+        default: false,
+        requestValueDialog: IntentDialog(stringLiteral: String(localized: "External Insulin?"))
+    ) var externalInsulin: Bool
+
+    @Parameter(
         title: LocalizedStringResource("Confirm Before applying"),
         description: LocalizedStringResource("If toggled, you will need to confirm before applying."),
         default: true
@@ -34,10 +41,12 @@ import Swinject
     static var parameterSummary: some ParameterSummary {
         When(\.$confirmBeforeApplying, .equalTo, true, {
             Summary("Applying \(\.$bolusQuantity) U") {
+                \.$externalInsulin
                 \.$confirmBeforeApplying
             }
         }, otherwise: {
             Summary("Immediately applying \(\.$bolusQuantity) U") {
+                \.$externalInsulin
                 \.$confirmBeforeApplying
             }
         })
@@ -59,12 +68,17 @@ import Swinject
                     )
                 )
             }
-
-            let finalBolusDisplay = try await BolusIntentRequest().bolus(amount)
-            return .result(
-                dialog: IntentDialog(finalBolusDisplay)
-            )
-
+            if externalInsulin {
+                let finalExternalBolusDisplay = try await BolusIntentRequest().bolusExternal(amount)
+                return .result(
+                    dialog: IntentDialog(stringLiteral: finalExternalBolusDisplay)
+                )
+            } else {
+                let finalBolusDisplay = try await BolusIntentRequest().bolus(amount)
+                return .result(
+                    dialog: IntentDialog(finalBolusDisplay)
+                )
+            }
         } catch {
             throw error
         }

+ 21 - 0
Trio/Sources/Shortcuts/Bolus/BolusIntentRequest.swift

@@ -27,4 +27,25 @@ import Foundation
             )
         }
     }
+
+    func bolusExternal(_ bolusAmount: Double) async throws -> String {
+        var bolusQuantity: Decimal = 0
+        if Decimal(bolusAmount) > settingsManager.pumpSettings.maxBolus {
+            return String(
+                localized:
+                "The bolus cannot be larger than the pump setting max bolus (\(settingsManager.pumpSettings.maxBolus.description))."
+            )
+        } else {
+            bolusQuantity = apsManager.roundBolus(amount: Decimal(bolusAmount))
+        }
+
+        await pumpHistoryStorage.storeExternalInsulinEvent(amount: bolusQuantity, timestamp: Date())
+        // perform determine basal sync
+        try await apsManager.determineBasalSync()
+
+        return String(
+            localized:
+            "A external bolus of \(bolusQuantity.formatted()) U of insulin was recorded."
+        )
+    }
 }