Skip to content

Commit

Permalink
Merge pull request nightscout#362 from kskandis/incorrect-bolus-amoun…
Browse files Browse the repository at this point in the history
…t-entered-textfield

resolve incorrect bolus amount due to uitextfield issue
  • Loading branch information
dnzxy authored Jul 27, 2024
2 parents 03ef229 + 723163f commit 57fd895
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions FreeAPS/Sources/Views/TextFieldWithToolBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,23 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
let currentText = textField.text as NSString?
{
// Get the proposed new text
let proposedText = currentText.replacingCharacters(in: range, with: string)
let proposedTextOriginal = currentText.replacingCharacters(in: range, with: string)

// Remove thousand separator
let proposedText = proposedTextOriginal.replacingOccurrences(of: decimalFormatter.groupingSeparator, with: "")

// Try to convert proposed text to number
let number = parent.numberFormatter.number(from: proposedText) ?? decimalFormatter.number(from: proposedText)

// Update the binding value if conversion is successful
if let number = number {
parent.text = number.decimalValue
let lastCharIndex = proposedText.index(before: proposedText.endIndex)
let hasDecimalSeparator = proposedText.contains(decimalFormatter.decimalSeparator)
let hasTrailingZeros = (hasDecimalSeparator && proposedText[lastCharIndex] == "0") || isDecimalSeparator
if !hasTrailingZeros
{
parent.text = number.decimalValue
}
} else {
parent.text = 0
}
Expand Down

0 comments on commit 57fd895

Please sign in to comment.