Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash when holding backspace #352

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions FreeAPS/Sources/Views/TextFieldWithToolBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,24 @@ extension TextFieldWithToolBarString.Coordinator: UITextFieldDelegate {
shouldChangeCharactersIn range: NSRange,
replacementString string: String
) -> Bool {
if let maxLength = parent.maxLength {
// Get the current text, including the proposed change
let currentText = textField.text ?? ""
let newLength = currentText.count + string.count - range.length
if newLength > maxLength {
return false
}
guard let currentText = textField.text as NSString? else {
return false
}

// Calculate the new text length
let newLength = currentText.length + string.count - range.length

// If there's a maxLength, ensure the new length is within the limit
if let maxLength = parent.maxLength, newLength > maxLength {
return false
}

// Attempt to replace characters in range with the replacement string
let newText = currentText.replacingCharacters(in: range, with: string)

// Update the binding text state
DispatchQueue.main.async {
if let textFieldText = textField.text as NSString? {
let newText = textFieldText.replacingCharacters(in: range, with: string)
self.parent.text = newText
}
self.parent.text = newText
}

return true
Expand Down