Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
16 changes: 11 additions & 5 deletions LoopKitUI/CarbKit/CarbQuantityRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public struct CarbQuantityRow: View {
private let formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumIntegerDigits = 3
formatter.maximumFractionDigits = 1
return formatter
}()
Expand Down Expand Up @@ -77,19 +76,26 @@ public struct CarbQuantityRow: View {

// Update quantity based on text field input
private func updateQuantity(with input: String) {
if let number = formatter.number(from: input) {
quantity = number.doubleValue
let decimalSeparator = formatter.decimalSeparator ?? "."
let allowedCharacters = "0123456789" + decimalSeparator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has anyone tested that this works for arabic/persian locales?

let filtered = input.filter { allowedCharacters.contains($0) }
if filtered != input {
self.carbInput = filtered
}

let minAllowedCarbEntry = 1.0
if let doubleValue = Double(filtered), doubleValue >= minAllowedCarbEntry {
quantity = doubleValue
} else {
quantity = nil
}
}

// Update text field input based on quantity
private func updateCarbInput(with newQuantity: Double?) {
// Do not enable Continue button if newQuantity is invalid (< minAllowedCarbEntry)
if let value = newQuantity {
carbInput = formatter.string(from: NSNumber(value: value)) ?? ""
} else {
carbInput = ""
}
}

Expand Down