Jelajahi Sumber

fix NS Port Textfield showing a thousands place separator

polscm32 aka Marvout 1 tahun lalu
induk
melakukan
c24bd7bb00

+ 11 - 4
FreeAPS/Sources/Modules/NightscoutConfig/View/NightscoutConnectView.swift

@@ -2,12 +2,13 @@ import SwiftUI
 
 struct NightscoutConnectView: View {
     @ObservedObject var state: NightscoutConfig.StateModel
-    @State private var portFormater: NumberFormatter
+    @State private var portFormatter: NumberFormatter
 
     init(state: NightscoutConfig.StateModel) {
         self.state = state
-        portFormater = NumberFormatter()
-        portFormater.allowsFloats = false
+        portFormatter = NumberFormatter()
+        portFormatter.allowsFloats = false
+        portFormatter.usesGroupingSeparator = false
     }
 
     var body: some View {
@@ -49,7 +50,13 @@ struct NightscoutConnectView: View {
                 Toggle("Use local glucose server", isOn: $state.useLocalSource)
                 HStack {
                     Text("Port")
-                    TextFieldWithToolBar(text: $state.localPort, placeholder: "", numberFormatter: portFormater)
+                    TextFieldWithToolBar(
+                        text: $state.localPort,
+                        placeholder: "",
+                        keyboardType: .numberPad,
+                        numberFormatter: portFormatter,
+                        allowDecimalSeparator: false
+                    )
                 }
             } header: { Text("Local glucose source") }
         }

+ 6 - 3
FreeAPS/Sources/Views/TextFieldWithToolBar.swift

@@ -14,6 +14,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
     var isDismissible: Bool
     var textFieldDidBeginEditing: (() -> Void)?
     var numberFormatter: NumberFormatter
+    var allowDecimalSeparator: Bool
 
     public init(
         text: Binding<Decimal>,
@@ -27,7 +28,8 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
         maxLength: Int? = nil,
         isDismissible: Bool = true,
         textFieldDidBeginEditing: (() -> Void)? = nil,
-        numberFormatter: NumberFormatter
+        numberFormatter: NumberFormatter,
+        allowDecimalSeparator: Bool = true
     ) {
         _text = text
         self.placeholder = placeholder
@@ -42,6 +44,7 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
         self.textFieldDidBeginEditing = textFieldDidBeginEditing
         self.numberFormatter = numberFormatter
         self.numberFormatter.numberStyle = .decimal
+        self.allowDecimalSeparator = allowDecimalSeparator
     }
 
     public func makeUIView(context: Context) -> UITextField {
@@ -146,7 +149,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
         let isDecimalSeparator = (string == decimalFormatter.decimalSeparator && textField.text?.contains(string) == false)
 
         // Only proceed if the input is a valid number or decimal separator
-        if isNumber || isDecimalSeparator,
+        if isNumber || isDecimalSeparator && parent.allowDecimalSeparator,
            let currentText = textField.text as NSString?
         {
             // Get the proposed new text
@@ -164,7 +167,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
         }
 
         // Allow the change if it's a valid number or decimal separator
-        return isNumber || isDecimalSeparator
+        return isNumber || isDecimalSeparator && parent.allowDecimalSeparator
     }
 
     public func textFieldDidBeginEditing(_: UITextField) {