diff --git a/FreeAPS/Sources/Modules/NightscoutConfig/View/NightscoutConnectView.swift b/FreeAPS/Sources/Modules/NightscoutConfig/View/NightscoutConnectView.swift index cff0365c1..9f69b8821 100644 --- a/FreeAPS/Sources/Modules/NightscoutConfig/View/NightscoutConnectView.swift +++ b/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") } } diff --git a/FreeAPS/Sources/Views/TextFieldWithToolBar.swift b/FreeAPS/Sources/Views/TextFieldWithToolBar.swift index ca80c1588..2095d8537 100644 --- a/FreeAPS/Sources/Views/TextFieldWithToolBar.swift +++ b/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, @@ -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) {