Skip to content

Commit

Permalink
fix NS Port Textfield showing a thousands place separator
Browse files Browse the repository at this point in the history
  • Loading branch information
marv-out committed Jul 6, 2024
1 parent 49bc151 commit c24bd7b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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") }
}
Expand Down
9 changes: 6 additions & 3 deletions FreeAPS/Sources/Views/TextFieldWithToolBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit c24bd7b

Please sign in to comment.