Преглед изворни кода

Merge pull request #607 from avouspierre/external-insulin-to-shortcut

external insulin to the bolus shortcuts.
marv-out пре 10 месеци
родитељ
комит
435c9d9de1

+ 12 - 0
Trio/Resources/InfoPlist.xcstrings

@@ -457,6 +457,18 @@
         }
       }
     },
+    "NSCalendarsFullAccessUsageDescription" : {
+      "comment" : "Privacy - Calendars Full Access Usage Description",
+      "extractionState" : "extracted_with_value",
+      "localizations" : {
+        "en" : {
+          "stringUnit" : {
+            "state" : "new",
+            "value" : "To create events with BG reading values, so that they can be viewed on Apple Watch and CarPlay"
+          }
+        }
+      }
+    },
     "NSCalendarsUsageDescription" : {
       "comment" : "Privacy - Calendars Usage Description",
       "extractionState" : "extracted_with_value",

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

@@ -8832,6 +8832,9 @@
         }
       }
     },
+    "%lld h" : {
+
+    },
     "%lld hr" : {
       "localizations" : {
         "bg" : {
@@ -20554,6 +20557,17 @@
         }
       }
     },
+    "A external bolus of %@ U of insulin was recorded." : {
+      "extractionState" : "stale",
+      "localizations" : {
+        "fr" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Un bolus externe de %@ U d'insuline a été enregistré."
+          }
+        }
+      }
+    },
     "A few important notes…" : {
       "localizations" : {
         "bg" : {
@@ -37406,6 +37420,9 @@
         }
       }
     },
+    "An external bolus of %@ U of insulin was recorded." : {
+
+    },
     "An unknown authentication error occurred. Please try again." : {
       "localizations" : {
         "bg" : {
@@ -39642,6 +39659,7 @@
       }
     },
     "Are you sure to bolus %@ U of insulin?" : {
+      "extractionState" : "stale",
       "localizations" : {
         "bg" : {
           "stringUnit" : {
@@ -96107,6 +96125,16 @@
         }
       }
     },
+    "External Insulin?" : {
+      "localizations" : {
+        "fr" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Insuline externe ?"
+          }
+        }
+      }
+    },
     "External:" : {
       "localizations" : {
         "bg" : {
@@ -114809,6 +114837,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" : {
@@ -128637,6 +128675,16 @@
         }
       }
     },
+    "Log external insulin bolus ${bolusQuantity} U" : {
+      "localizations" : {
+        "fr" : {
+          "stringUnit" : {
+            "state" : "translated",
+            "value" : "Enregistrer un bolus d’insuline externe ${bolusQuantity} U"
+          }
+        }
+      }
+    },
     "Log FPU" : {
       "localizations" : {
         "bg" : {
@@ -202201,6 +202249,9 @@
         }
       }
     },
+    "The external bolus cannot be larger than 3 x the pump setting max bolus (%@)." : {
+
+    },
     "The Fat and Protein Delay setting defines the time between when you log fat and protein and when the system starts delivering insulin for the Fat-Protein Unit Carb Equivalents (FPUs)." : {
       "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
 

+ 34 - 12
Trio/Sources/Shortcuts/Bolus/BolusIntent.swift

@@ -27,20 +27,36 @@ 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
     ) var confirmBeforeApplying: Bool
 
     static var parameterSummary: some ParameterSummary {
-        When(\.$confirmBeforeApplying, .equalTo, true, {
-            Summary("Applying \(\.$bolusQuantity) U") {
+        When(\.$externalInsulin, .equalTo, true, {
+            Summary("Log external insulin bolus \(\.$bolusQuantity) U") {
+                \.$externalInsulin
                 \.$confirmBeforeApplying
             }
         }, otherwise: {
-            Summary("Immediately applying \(\.$bolusQuantity) U") {
-                \.$confirmBeforeApplying
-            }
+            When(\.$confirmBeforeApplying, .equalTo, true, {
+                Summary("Applying \(\.$bolusQuantity) U") {
+                    \.$externalInsulin
+                    \.$confirmBeforeApplying
+                }
+            }, otherwise: {
+                Summary("Immediately applying \(\.$bolusQuantity) U") {
+                    \.$externalInsulin
+                    \.$confirmBeforeApplying
+                }
+            })
         })
     }
 
@@ -55,18 +71,24 @@ import Swinject
                         dialog: IntentDialog(
                             stringLiteral: String(
                                 localized:
-                                "Are you sure to bolus \(bolusFormatted) U of insulin?"
+                                externalInsulin ? "Are you sure to log \(bolusFormatted) U of external insulin?" :
+                                    "Are you sure to bolus \(bolusFormatted) U of insulin?"
                             )
                         )
                     )
                 )
             }
-
-            let finalBolusDisplay = try await BolusIntentRequest().bolus(amount)
-            return .result(
-                dialog: IntentDialog(stringLiteral: 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(stringLiteral: finalBolusDisplay)
+                )
+            }
         } catch {
             throw error
         }

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

@@ -30,4 +30,25 @@ import Foundation
             )
         }
     }
+
+    func bolusExternal(_ bolusAmount: Double) async throws -> String {
+        var bolusQuantity: Decimal = 0
+        var maxExternal: Decimal { settingsManager.pumpSettings.maxBolus * 3 }
+        if Decimal(bolusAmount) > maxExternal {
+            return String(
+                localized:
+                "The external bolus cannot be larger than 3 x 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:
+                "An external bolus of \(bolusQuantity.formatted()) U of insulin was recorded."
+            )
+        }
+    }
 }