Skip to content

Commit

Permalink
revert part of last commit that removes trailing zeros; skip bind for…
Browse files Browse the repository at this point in the history
…0 or more maxFractionDigits
  • Loading branch information
kskandis committed Jul 26, 2024
1 parent bd9ff5e commit 8f28b80
Showing 1 changed file with 1 addition and 29 deletions.
30 changes: 1 addition & 29 deletions FreeAPS/Sources/Views/TextFieldWithToolBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
let hasDecimalSeparator = proposedText.contains(decimalFormatter.decimalSeparator)
let hasTrailingZeros = (
parent.numberFormatter
.maximumFractionDigits > 1 && hasDecimalSeparator && proposedText[lastCharIndex] == "0"
.maximumFractionDigits > 0 && hasDecimalSeparator && proposedText[lastCharIndex] == "0"
) ||
(isDecimalSeparator && parent.numberFormatter.allowsFloats)
if !parent.numberFormatter.allowsFloats || !hasTrailingZeros
Expand All @@ -182,19 +182,6 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
)
let maxDigits = decimalIndexInt + parent.numberFormatter.maximumFractionDigits + 1
allowChange = proposedText.count > maxDigits ? false : true

// Remove trailing zeros when FractionDigits are all zero
if proposedText.count == maxDigits {
var pattern = "[" + decimalFormatter.decimalSeparator + "]{1}[0]"
pattern += "{" + String(parent.numberFormatter.maximumFractionDigits) + "}"
let regex = NSRegularExpression(pattern)
let matches = regex.matches(proposedText)
if matches {
let resultText = String(proposedText[...rangeOfDecimal!.lowerBound])
let trailingZerosNumber = parent.numberFormatter.number(from: resultText)
parent.text = trailingZerosNumber?.decimalValue ?? 0
}
}
}
} else {
parent.text = 0
Expand All @@ -211,21 +198,6 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
}
}

extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illegal regular expression: \(pattern).")
}
}

func matches(_ string: String) -> Bool {
let range = NSRange(location: 0, length: string.utf16.count)
return firstMatch(in: string, options: [], range: range) != nil
}
}

extension UITextField {
func moveCursorToEnd() {
dispatchPrecondition(condition: .onQueue(.main))
Expand Down

0 comments on commit 8f28b80

Please sign in to comment.