Skip to content

Commit

Permalink
correct number processing for ios16
Browse files Browse the repository at this point in the history
Signed-off-by: Tassilo Karge <[email protected]>
  • Loading branch information
TAKeanice committed Nov 11, 2024
1 parent 5da2080 commit 9b552d8
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,8 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
textField.delegate = self
textField.keyboardType = .numbersAndPunctuation
}
textExtractor = { alert.textFields?[0].text }
//replace expected decimal separator
textExtractor = { alert.textFields?[0].text?.replacingOccurrences(of: NSLocale.current.decimalSeparator ?? "", with: ".") }
case .text:
alert.addTextField { textField in
textField.clearButtonMode = .always
Expand All @@ -852,7 +853,7 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
textExtractor = { alert.textFields?[0].text }
}
let sendAction = UIAlertAction(title: "Set value", style: .destructive, handler: { [weak self] _ in
self?.sendCommand(widget.item, commandToSend: textExtractor()) // TODO: sanitize / convert text?
self?.sendCommand(widget.item, commandToSend: textExtractor())
})
alert.addAction(sendAction)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
Expand Down Expand Up @@ -888,8 +889,7 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let decimalSeparator = NSLocale.current.decimalSeparator ?? ""
let oldString = (textField.text ?? "")
let replacement: NSString = string as NSString
let wholeNumberRegex = "^-?[0-9]*$"
let wholeNumberRegex = /^-?[0-9]*$/

// check for deletion
return string.isEmpty
Expand All @@ -904,15 +904,19 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
) // new string replaces negative sign in old string
// check for old negative sign
&& (
!oldString.starts(with: "-") // old string does not start with negative sign
oldString.isEmpty
|| !oldString.starts(with: "-") // old string does not start with negative sign
|| range.location > 0 // new string starts after negative sign in old string
|| range.length > 0
) // new string replaces negative sign in old string
// check for decimal points
// check for decimal signs
&& (
string.range(of: wholeNumberRegex) != nil // new string is whole number
|| replacement.replacingCharacters(in: replacement.range(of: decimalSeparator), with: "").range(of: wholeNumberRegex) != nil // new string is valid decimal number
&& !(oldString as NSString).replacingCharacters(in: range, with: "").contains(decimalSeparator)
string.firstRange(of: wholeNumberRegex) != nil // new string is whole number
|| (
string.replacing(decimalSeparator, with: "", maxReplacements: 1)
.firstRange(of: wholeNumberRegex) != nil // new string is valid decimal number
&& !(oldString as NSString).replacingCharacters(in: range, with: "").contains(decimalSeparator)
)
) // old string without replaced range not yet contains decimal separator
}
}
Expand Down

0 comments on commit 9b552d8

Please sign in to comment.