Jonas Björkert 2 anni fa
parent
commit
c293580fef

+ 2 - 2
LoopFollow/Remote/RemoteView.swift

@@ -74,13 +74,13 @@ struct RemoteView: View {
                             HStack {
                                 Text("Target")
                                 Spacer()
-                                TextFieldWithToolBar(quantity: $newHKTarget, unit: UserDefaultsRepository.getPreferredUnit())
+                                TextFieldWithToolBar(quantity: $newHKTarget, maxLength: 4, unit: UserDefaultsRepository.getPreferredUnit())
                                 Text(UserDefaultsRepository.getPreferredUnit().localizedShortUnitString).foregroundColor(.secondary)
                             }
                             HStack {
                                 Text("Duration")
                                 Spacer()
-                                TextFieldWithToolBar(quantity: $duration, unit: HKUnit.minute())
+                                TextFieldWithToolBar(quantity: $duration, maxLength: 4, unit: HKUnit.minute())
                                 Text("minutes").foregroundColor(.secondary)
                             }
                             HStack {

+ 9 - 8
LoopFollow/helpers/TextFieldWithToolBar.swift

@@ -182,16 +182,17 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
             let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
             let isDecimalSeparator = (string == decimalSeparator && textField.text?.contains(decimalSeparator) == false)
 
-            // Only proceed if the input is a valid number or decimal separator
-            if isNumber || (isDecimalSeparator && parent.allowDecimalSeparator && unit.preferredFractionDigits > 0),
-               let currentText = textField.text as NSString?
-            {
-                // Get the proposed new text
-                let proposedTextOriginal = currentText.replacingCharacters(in: range, with: string)
+            // Get the proposed new text
+            let currentText = textField.text ?? ""
+            let proposedText = (currentText as NSString).replacingCharacters(in: range, with: string)
 
-                // Remove thousand separator
-                let proposedText = proposedTextOriginal.replacingOccurrences(of: ",", with: "")
+            // 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
+            if isNumber || (isDecimalSeparator && parent.allowDecimalSeparator && unit.preferredFractionDigits > 0) {
                 // Try to convert proposed text to number
                 if let number = Double(proposedText.replacingOccurrences(of: decimalSeparator, with: ".")) {
                     DispatchQueue.main.async {