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

Revert "Remove insulinReq guardrail, fix threading"

This reverts commit 1afd9db494bd9c6a92f9233b01a7cef42f9c42a0.
Auggie Fisher 1 год назад
Родитель
Сommit
d565c14be6

+ 10 - 10
FreeAPS.xcodeproj/project.pbxproj

@@ -496,7 +496,7 @@
 /* Begin PBXCopyFilesBuildPhase section */
 		3821ECD025DC703C00BC42AD /* Embed Frameworks */ = {
 			isa = PBXCopyFilesBuildPhase;
-			buildActionMask = 8;
+			buildActionMask = 2147483647;
 			dstPath = "";
 			dstSubfolderSpec = 10;
 			files = (
@@ -509,18 +509,18 @@
 				CE95BF5E2BA770C300DC3DE3 /* LoopKitUI.framework in Embed Frameworks */,
 			);
 			name = "Embed Frameworks";
-			runOnlyForDeploymentPostprocessing = 1;
+			runOnlyForDeploymentPostprocessing = 0;
 		};
 		38E8753D27554D5900975559 /* Embed Watch Content */ = {
 			isa = PBXCopyFilesBuildPhase;
-			buildActionMask = 8;
+			buildActionMask = 2147483647;
 			dstPath = "$(CONTENTS_FOLDER_PATH)/Watch";
 			dstSubfolderSpec = 16;
 			files = (
 				38E8753C27554D5900975559 /* FreeAPSWatch.app in Embed Watch Content */,
 			);
 			name = "Embed Watch Content";
-			runOnlyForDeploymentPostprocessing = 1;
+			runOnlyForDeploymentPostprocessing = 0;
 		};
 		38E8754027554D5900975559 /* Embed App Extensions */ = {
 			isa = PBXCopyFilesBuildPhase;
@@ -535,14 +535,14 @@
 		};
 		6B1A8D122B14D88E00E76752 /* Embed Foundation Extensions */ = {
 			isa = PBXCopyFilesBuildPhase;
-			buildActionMask = 8;
+			buildActionMask = 2147483647;
 			dstPath = "";
 			dstSubfolderSpec = 13;
 			files = (
 				6B1A8D282B14D91700E76752 /* LiveActivityExtension.appex in Embed Foundation Extensions */,
 			);
 			name = "Embed Foundation Extensions";
-			runOnlyForDeploymentPostprocessing = 1;
+			runOnlyForDeploymentPostprocessing = 0;
 		};
 /* End PBXCopyFilesBuildPhase section */
 
@@ -2819,7 +2819,7 @@
 		CE95BF582BA5F8F300DC3DE3 /* Install plugins */ = {
 			isa = PBXShellScriptBuildPhase;
 			alwaysOutOfDate = 1;
-			buildActionMask = 8;
+			buildActionMask = 2147483647;
 			files = (
 			);
 			inputFileListPaths = (
@@ -2831,13 +2831,13 @@
 			);
 			outputPaths = (
 			);
-			runOnlyForDeploymentPostprocessing = 1;
+			runOnlyForDeploymentPostprocessing = 0;
 			shellPath = /bin/sh;
 			shellScript = "\"${SRCROOT}/Scripts/copy-plugins.sh\"\n";
 		};
 		DD88C8DF2C4D583900F2D558 /* ShellScript */ = {
 			isa = PBXShellScriptBuildPhase;
-			buildActionMask = 8;
+			buildActionMask = 2147483647;
 			files = (
 			);
 			inputFileListPaths = (
@@ -2848,7 +2848,7 @@
 			);
 			outputPaths = (
 			);
-			runOnlyForDeploymentPostprocessing = 1;
+			runOnlyForDeploymentPostprocessing = 0;
 			shellPath = /bin/sh;
 			shellScript = "\"${SRCROOT}/scripts/capture-build-details.sh\"\n";
 		};

+ 3 - 0
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -4,6 +4,7 @@ enum BolusShortcutLimit: String, JSON, CaseIterable, Identifiable {
     var id: String { rawValue }
     case notAllowed
     case limitBolusMax
+    case limitInsulinSuggestion
 
     var displayName: String {
         switch self {
@@ -11,6 +12,8 @@ enum BolusShortcutLimit: String, JSON, CaseIterable, Identifiable {
             return String(localized: "Not allowed", table: "ShortcutsDetail")
         case .limitBolusMax:
             return String(localized: "Max bolus", table: "ShortcutsDetail")
+        case .limitInsulinSuggestion:
+            return String(localized: "Insulin recommended", table: "ShortcutsDetail")
         }
     }
 }

+ 11 - 0
FreeAPS/Sources/Modules/ShortcutsConfig/View/ShortcutsConfigView.swift

@@ -25,6 +25,17 @@ extension ShortcutsConfig {
                         String(localized: "Allow bolusing with shortcuts", table: "ShortcutsDetail"),
                         isOn: $state.allowBolusByShortcuts
                     )
+
+                    Picker(
+                        selection: $state.maxBolusByShortcuts,
+                        label: Text("Limit bolus from shortcuts to", tableName: "ShortcutsDetail")
+                    ) {
+                        ForEach(BolusShortcutLimit.allCases) { v in
+                            v != .notAllowed ? Text(v.displayName).tag(v) : nil
+                            // Text(v.displayName).tag(v)
+                        }
+                    }
+                    .disabled(!state.allowBolusByShortcuts)
                 }
             }
             .onAppear(perform: configureView)

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

@@ -17,7 +17,6 @@ import Swinject
     let resolver: Resolver
 
     let coredataContext = CoreDataStack.shared.newTaskContext()
-    let viewContext = CoreDataStack.shared.persistentContainer.viewContext
 
     override init() {
         resolver = FreeAPSApp.resolver

+ 60 - 5
FreeAPS/Sources/Shortcuts/Bolus/BolusIntentRequest.swift

@@ -3,6 +3,12 @@ import CoreData
 import Foundation
 
 @available(iOS 16.0,*) final class BolusIntentRequest: BaseIntentsRequest {
+    private var suggestion: Determination? {
+        // TODO: CRITICAL
+        /// This MUST update to use the latest determination's insulinRequired from Core Data
+        fileStorage.retrieve(OpenAPS.Enact.suggested, as: Determination.self)
+    }
+
     func bolus(_ bolusAmount: Double) async throws -> LocalizedStringResource {
         var bolusQuantity: Decimal = 0
         switch settingsManager.settings.bolusShortcut {
@@ -24,11 +30,60 @@ import Foundation
                 bolusQuantity = apsManager.roundBolus(amount: Decimal(bolusAmount))
             }
 
-            await apsManager.enactBolus(amount: Double(bolusQuantity), isSMB: false)
-            return LocalizedStringResource(
-                "A bolus command of \(bolusQuantity.formatted()) U of insulin was sent",
-                table: "ShortcutsDetail"
-            )
+        // Block any bolus attempted if it is larger than the insulin recommended
+        case .limitInsulinSuggestion:
+            /*
+             case .limitInsulinSuggestion:
+                 let lastDetermination = await CoreDataStack.shared.fetchEntitiesAsync(
+                     ofType: OrefDetermination.self,
+                     onContext: coredataContext,
+                     predicate: NSPredicate.predicateFor30MinAgoForDetermination,
+                     key: "deliveryAt", ascending: false,
+                     fetchLimit: 1
+                 )
+                 guard let latest = lastDetermination.first else {
+                     return LocalizedStringResource(
+                         "Error retrieving suggested insulin amount guardrail.",
+                         table: "ShortcutsDetail"
+                     )
+                 }
+                 let insulinSuggestion = latest.insulinForManualBolus ?? 0
+             */
+            let insulinSuggestion = suggestion?.insulinForManualBolus ?? 0
+            if Decimal(bolusAmount) > insulinSuggestion {
+                return LocalizedStringResource(
+                    "The bolus cannot be larger than the suggested insulin (\(insulinSuggestion.description)).",
+                    table: "ShortcutsDetail"
+                )
+            }
+            // Also make sure that no matter what, the bolus doesn't exceed the max setting in Trio
+            else if Decimal(bolusAmount) > settingsManager.pumpSettings.maxBolus {
+                return LocalizedStringResource(
+                    "The bolus cannot be larger than the pump setting max bolus (\(settingsManager.pumpSettings.maxBolus.description)).",
+                    table: "ShortcutsDetail"
+                )
+            } else {
+                bolusQuantity = apsManager
+                    .roundBolus(amount: Decimal(bolusAmount))
+            }
+        }
+
+        await apsManager.enactBolus(amount: Double(bolusQuantity), isSMB: false)
+        return LocalizedStringResource(
+            "A bolus command of \(bolusQuantity.formatted()) U of insulin was sent",
+            table: "ShortcutsDetail"
+        )
+    }
+
+    private var glucoseFormatter: NumberFormatter {
+        let formatter = NumberFormatter()
+        formatter.numberStyle = .decimal
+        formatter.maximumFractionDigits = 0
+        if settingsManager.settings.units == .mmolL {
+            formatter.minimumFractionDigits = 1
+            formatter.maximumFractionDigits = 1
         }
+        formatter.roundingMode = .halfUp
+        return formatter
     }
 }

+ 1 - 1
Trio.xcworkspace/xcshareddata/swiftpm/Package.resolved

@@ -49,7 +49,7 @@
     {
       "identity" : "swiftcharts",
       "kind" : "remoteSourceControl",
-      "location" : "https://github.com/ivanschuetz/SwiftCharts.git",
+      "location" : "https://github.com/ivanschuetz/SwiftCharts",
       "state" : {
         "branch" : "master",
         "revision" : "c354c1945bb35a1f01b665b22474f6db28cba4a2"