polscm32 1 tahun lalu
induk
melakukan
59be0ae698

+ 1 - 0
Trio/Sources/Modules/DataTable/View/DataTableRootView.swift

@@ -433,6 +433,7 @@ extension DataTable {
                                 TextFieldWithToolBar(
                                     text: $state.manualGlucose,
                                     placeholder: " ... ",
+                                    maxValue: state.units == .mmolL ? 400 / 18 : 400,
                                     numberFormatter: manualGlucoseFormatter,
                                     initialFocus: true
                                 )

+ 4 - 0
Trio/Sources/Modules/Treatments/View/TreatmentsRootView.swift

@@ -84,6 +84,7 @@ extension Treatments {
                         text: $state.protein,
                         placeholder: "0",
                         keyboardType: .numberPad,
+                        maxValue: state.maxProtein,
                         numberFormatter: mealFormatter,
                         showArrows: true,
                         previousTextField: { focusedField = previousField(from: .protein) },
@@ -101,6 +102,7 @@ extension Treatments {
                         text: $state.fat,
                         placeholder: "0",
                         keyboardType: .numberPad,
+                        maxValue: state.maxFat,
                         numberFormatter: mealFormatter,
                         showArrows: true,
                         previousTextField: { focusedField = previousField(from: .fat) },
@@ -120,6 +122,7 @@ extension Treatments {
                     text: $state.carbs,
                     placeholder: "0",
                     keyboardType: .numberPad,
+                    maxValue: state.maxCarbs,
                     numberFormatter: mealFormatter,
                     showArrows: true,
                     previousTextField: { focusedField = previousField(from: .carbs) },
@@ -314,6 +317,7 @@ extension Treatments {
                                     placeholder: "0",
                                     textColor: colorScheme == .dark ? .white : .blue,
                                     maxLength: 5,
+                                    maxValue: state.maxExternal,
                                     numberFormatter: formatter,
                                     showArrows: true,
                                     previousTextField: { focusedField = previousField(from: .bolus) },

+ 12 - 2
Trio/Sources/Views/TextFieldWithToolBar.swift

@@ -8,6 +8,7 @@ public struct TextFieldWithToolBar: View {
     var textAlignment: TextAlignment
     var keyboardType: UIKeyboardType
     var maxLength: Int?
+    var maxValue: Decimal?
     var isDismissible: Bool
     var textFieldDidBeginEditing: (() -> Void)?
     var textDidChange: ((Decimal) -> Void)?
@@ -28,6 +29,7 @@ public struct TextFieldWithToolBar: View {
         textAlignment: TextAlignment = .trailing,
         keyboardType: UIKeyboardType = .decimalPad,
         maxLength: Int? = nil,
+        maxValue: Decimal? = nil,
         isDismissible: Bool = true,
         textFieldDidBeginEditing: (() -> Void)? = nil,
         textDidChange: ((Decimal) -> Void)? = nil,
@@ -44,6 +46,7 @@ public struct TextFieldWithToolBar: View {
         self.textAlignment = textAlignment
         self.keyboardType = keyboardType
         self.maxLength = maxLength
+        self.maxValue = maxValue
         self.isDismissible = isDismissible
         self.textFieldDidBeginEditing = textFieldDidBeginEditing
         self.textDidChange = textDidChange
@@ -149,8 +152,15 @@ public struct TextFieldWithToolBar: View {
 
         // Update if valid decimal
         if let decimal = Decimal(string: processedText, locale: numberFormatter.locale) {
-            text = decimal
-            textDidChange?(decimal)
+            if let maxValue = maxValue, decimal > maxValue {
+                text = maxValue
+                localText = numberFormatter.string(from: maxValue as NSNumber) ?? ""
+            } else {
+                text = decimal
+                textDidChange?(decimal)
+            }
+//            text = decimal
+//            textDidChange?(decimal)
 
             // If the processed text is different from the input, update the field
             if processedText != newValue {