Jonas Björkert 2 лет назад
Родитель
Сommit
37ee5ea360
1 измененных файлов с 33 добавлено и 24 удалено
  1. 33 24
      LoopFollow/helpers/TextFieldWithToolBar.swift

+ 33 - 24
LoopFollow/helpers/TextFieldWithToolBar.swift

@@ -212,51 +212,60 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
             UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
         }
 
-        func format(quantity: HKQuantity, for unit: HKUnit) -> String {
-            let value = quantity.doubleValue(for: unit)
-            let formatter = NumberFormatter()
-            formatter.minimumFractionDigits = unit.preferredFractionDigits
-            formatter.maximumFractionDigits = unit.preferredFractionDigits
-            formatter.numberStyle = .decimal
-            return formatter.string(from: NSNumber(value: value)) ?? ""
-        }
-
-        private func isWithinLimits(_ quantity: HKQuantity) -> Bool {
-            if let minValue = minValue, quantity.doubleValue(for: unit) < minValue.doubleValue(for: unit) {
-                return false
-            }
-            if let maxValue = maxValue, quantity.doubleValue(for: unit) > maxValue.doubleValue(for: unit) {
-                return false
-            }
-            return true
-        }
-
         public func textField(
             _ textField: UITextField,
             shouldChangeCharactersIn range: NSRange,
             replacementString string: String
         ) -> Bool {
-            // Check if the input is a number or the decimal separator
             let decimalSeparator = Locale.current.decimalSeparator ?? "."
             let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
             let isDecimalSeparator = (string == decimalSeparator && textField.text?.contains(decimalSeparator) == false)
 
-            // Get the proposed new text
             let currentText = textField.text ?? ""
             let proposedText = (currentText as NSString).replacingCharacters(in: range, with: string)
 
-            // Enforce maxLength if set
             if let maxLength = maxLength, proposedText.count > maxLength {
                 return false
             }
 
-            // Only proceed if the input is a valid number or decimal separator
-            return isNumber || (isDecimalSeparator && parent.allowDecimalSeparator && unit.preferredFractionDigits > 0)
+            let isValidInput = isNumber || (isDecimalSeparator && parent.allowDecimalSeparator && unit.preferredFractionDigits > 0)
+
+            if isValidInput, let number = Double(proposedText.replacingOccurrences(of: decimalSeparator, with: ".")) {
+                let quantity = HKQuantity(unit: unit, doubleValue: number)
+                if isWithinLimits(quantity) {
+                    parent.quantity = quantity
+                } else {
+                    parent.quantity = HKQuantity(unit: unit, doubleValue: 0)
+                }
+            } else {
+                parent.quantity = HKQuantity(unit: unit, doubleValue: 0)
+            }
+
+            return isValidInput
         }
 
         public func textFieldDidBeginEditing(_: UITextField) {
             parent.textFieldDidBeginEditing?()
         }
+
+        func format(quantity: HKQuantity, for unit: HKUnit) -> String {
+            let value = quantity.doubleValue(for: unit)
+            let formatter = NumberFormatter()
+            formatter.minimumFractionDigits = unit.preferredFractionDigits
+            formatter.maximumFractionDigits = unit.preferredFractionDigits
+            formatter.numberStyle = .decimal
+            return formatter.string(from: NSNumber(value: value)) ?? ""
+        }
+
+        private func isWithinLimits(_ quantity: HKQuantity) -> Bool {
+            if let minValue = minValue, quantity.doubleValue(for: unit) < minValue.doubleValue(for: unit) {
+                return false
+            }
+            if let maxValue = maxValue, quantity.doubleValue(for: unit) > maxValue.doubleValue(for: unit) {
+                return false
+            }
+            return true
+        }
     }
 }