Ver código fonte

Use Int for carb/fat/protein and add max checks

Convert carb, fat, and protein quantities from Double to Int in AddCarbPresetIntent and CarbPresetIntentRequest. Add validation guards against settings.maxCarbs/maxFat/maxProtein and return localized IntentDialog errors when limits are exceeded. Reuse a CarbPresetIntentRequest instance in the intent, preserve storage by converting Ints to Decimal when creating CarbsEntry
Mike Plante 3 meses atrás
pai
commit
8171c32b50

+ 41 - 9
Trio/Sources/Shortcuts/Carbs/AddCarbPresetIntent.swift

@@ -15,24 +15,24 @@ struct AddCarbPresetIntent: AppIntent {
         description: "Quantity of carbs in g",
         controlStyle: .field,
         inclusiveRange: (lowerBound: 0, upperBound: 300),
-    ) var carbQuantity: Double?
         requestValueDialog: IntentDialog(stringLiteral: String(localized: "How many grams of carbs?"))
+    ) var carbQuantity: Int?
 
     @Parameter(
         title: "Quantity Fat",
         description: "Quantity of fat in g",
-        default: 0.0,
+        default: 0,
         inclusiveRange: (0, 300),
-    ) var fatQuantity: Double
         requestValueDialog: IntentDialog(stringLiteral: String(localized: "How many grams of fat?"))
+    ) var fatQuantity: Int
 
     @Parameter(
         title: "Quantity Protein",
         description: "Quantity of Protein in g",
-        default: 0.0,
+        default: 0,
         inclusiveRange: (0, 300),
-    ) var proteinQuantity: Double
         requestValueDialog: IntentDialog(stringLiteral: String(localized: "How many grams of protein?"))
+    ) var proteinQuantity: Int
 
     @Parameter(
         title: "Date",
@@ -71,13 +71,46 @@ struct AddCarbPresetIntent: AppIntent {
 
     @MainActor func perform() async throws -> some ProvidesDialog {
         do {
-            let quantityCarbs: Double
+            let quantityCarbs: Int
             if let cq = carbQuantity {
                 quantityCarbs = cq
             } else {
                 quantityCarbs = try await $carbQuantity.requestValue("How many grams of carbs?")
             }
 
+            let request = CarbPresetIntentRequest()
+            let maxCarbs = Int(truncating: request.settingsManager.settings.maxCarbs as NSDecimalNumber)
+            let maxFat = Int(truncating: request.settingsManager.settings.maxFat as NSDecimalNumber)
+            let maxProtein = Int(truncating: request.settingsManager.settings.maxProtein as NSDecimalNumber)
+
+            guard quantityCarbs <= maxCarbs else {
+                return .result(
+                    dialog: IntentDialog(
+                        stringLiteral: String(
+                            localized: "Logging Failed: Max Carbs = \(maxCarbs) g"
+                        )
+                    )
+                )
+            }
+            guard proteinQuantity <= maxProtein else {
+                return .result(
+                    dialog: IntentDialog(
+                        stringLiteral: String(
+                            localized: "Logging Failed: Max Protein = \(maxProtein) g"
+                        )
+                    )
+                )
+            }
+            guard fatQuantity <= maxFat else {
+                return .result(
+                    dialog: IntentDialog(
+                        stringLiteral: String(
+                            localized: "Logging Failed: Max Fat = \(maxFat) g"
+                        )
+                    )
+                )
+            }
+
             let dateCarbsAdded: Date
             let dateDefinedByUser: Bool
             if let da = dateAdded {
@@ -88,16 +121,15 @@ struct AddCarbPresetIntent: AppIntent {
                 dateDefinedByUser = false
             }
 
-            let quantityCarbsName = quantityCarbs.toString()
             if confirmBeforeApplying {
                 try await requestConfirmation(
                     result: .result(
-                        dialog: IntentDialog(stringLiteral: String(localized: "Add \(quantityCarbsName) grams of carbs?"))
+                        dialog: IntentDialog(stringLiteral: String(localized: "Add \(quantityCarbs) grams of carbs?"))
                     )
                 )
             }
 
-            let finalQuantityCarbsDisplay = try await CarbPresetIntentRequest().addCarbs(
+            let finalQuantityCarbsDisplay = try await request.addCarbs(
                 quantityCarbs,
                 fatQuantity,
                 proteinQuantity,

+ 10 - 12
Trio/Sources/Shortcuts/Carbs/CarbPresetIntentRequest.swift

@@ -3,25 +3,23 @@ import Foundation
 
 final class CarbPresetIntentRequest: BaseIntentsRequest {
     func addCarbs(
-        _ quantityCarbs: Double,
-        _ quantityFat: Double,
-        _ quantityProtein: Double,
+        _ quantityCarbs: Int,
+        _ quantityFat: Int,
+        _ quantityProtein: Int,
         _ dateAdded: Date,
         _ note: String?,
         _ dateDefinedByUser: Bool
     ) async throws -> String {
-        guard quantityCarbs >= 0.0 || quantityFat >= 0.0 || quantityProtein >= 0.0 else {
+        guard quantityCarbs >= 0 || quantityFat >= 0 || quantityProtein >= 0 else {
             return "Amount must be positive."
         }
 
-        let carbs = min(Decimal(quantityCarbs), settingsManager.settings.maxCarbs)
-
         try await carbsStorage.storeCarbs(
             [CarbsEntry(
                 id: UUID().uuidString,
                 createdAt: dateAdded,
                 actualDate: dateAdded,
-                carbs: carbs,
+                carbs: Decimal(quantityCarbs),
                 fat: Decimal(quantityFat),
                 protein: Decimal(quantityProtein),
                 note: (note?.isEmpty ?? true) ? "Via Shortcut" : note!,
@@ -31,12 +29,12 @@ final class CarbPresetIntentRequest: BaseIntentsRequest {
             areFetchedFromRemote: false
         )
         var resultDisplay: String
-        resultDisplay = String(localized: "Added \(String(format: "%.0f", Double(carbs))) g carbs")
-        if quantityFat > 0.0 {
-            resultDisplay = String(localized: "\(resultDisplay) and \(String(format: "%.0f", Double(quantityFat))) g fat")
+        resultDisplay = String(localized: "Added \(quantityCarbs) g carbs")
+        if quantityFat > 0 {
+            resultDisplay = String(localized: "\(resultDisplay) and \(quantityFat) g fat")
         }
-        if quantityProtein > 0.0 {
-            resultDisplay = String(localized: "\(resultDisplay) and \(String(format: "%.0f", Double(quantityProtein))) g protein")
+        if quantityProtein > 0 {
+            resultDisplay = String(localized: "\(resultDisplay) and \(quantityProtein) g protein")
         }
         if dateDefinedByUser {
             let dateFormatter = DateFormatter()