|
@@ -212,51 +212,60 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
|
|
|
UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
|
|
UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: nil)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- func format(quantity: HKQuantity, for unit: HKUnit) -> String {
|
|
|
|
|
- let value = quantity.doubleValue(for: unit)
|
|
|
|
|
- let formatter = NumberFormatter()
|
|
|
|
|
- formatter.minimumFractionDigits = unit.preferredFractionDigits
|
|
|
|
|
- formatter.maximumFractionDigits = unit.preferredFractionDigits
|
|
|
|
|
- formatter.numberStyle = .decimal
|
|
|
|
|
- return formatter.string(from: NSNumber(value: value)) ?? ""
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private func isWithinLimits(_ quantity: HKQuantity) -> Bool {
|
|
|
|
|
- if let minValue = minValue, quantity.doubleValue(for: unit) < minValue.doubleValue(for: unit) {
|
|
|
|
|
- return false
|
|
|
|
|
- }
|
|
|
|
|
- if let maxValue = maxValue, quantity.doubleValue(for: unit) > maxValue.doubleValue(for: unit) {
|
|
|
|
|
- return false
|
|
|
|
|
- }
|
|
|
|
|
- return true
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
public func textField(
|
|
public func textField(
|
|
|
_ textField: UITextField,
|
|
_ textField: UITextField,
|
|
|
shouldChangeCharactersIn range: NSRange,
|
|
shouldChangeCharactersIn range: NSRange,
|
|
|
replacementString string: String
|
|
replacementString string: String
|
|
|
) -> Bool {
|
|
) -> Bool {
|
|
|
- // Check if the input is a number or the decimal separator
|
|
|
|
|
let decimalSeparator = Locale.current.decimalSeparator ?? "."
|
|
let decimalSeparator = Locale.current.decimalSeparator ?? "."
|
|
|
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
|
|
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
|
|
|
let isDecimalSeparator = (string == decimalSeparator && textField.text?.contains(decimalSeparator) == false)
|
|
let isDecimalSeparator = (string == decimalSeparator && textField.text?.contains(decimalSeparator) == false)
|
|
|
|
|
|
|
|
- // Get the proposed new text
|
|
|
|
|
let currentText = textField.text ?? ""
|
|
let currentText = textField.text ?? ""
|
|
|
let proposedText = (currentText as NSString).replacingCharacters(in: range, with: string)
|
|
let proposedText = (currentText as NSString).replacingCharacters(in: range, with: string)
|
|
|
|
|
|
|
|
- // Enforce maxLength if set
|
|
|
|
|
if let maxLength = maxLength, proposedText.count > maxLength {
|
|
if let maxLength = maxLength, proposedText.count > maxLength {
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Only proceed if the input is a valid number or decimal separator
|
|
|
|
|
- return isNumber || (isDecimalSeparator && parent.allowDecimalSeparator && unit.preferredFractionDigits > 0)
|
|
|
|
|
|
|
+ let isValidInput = isNumber || (isDecimalSeparator && parent.allowDecimalSeparator && unit.preferredFractionDigits > 0)
|
|
|
|
|
+
|
|
|
|
|
+ if isValidInput, let number = Double(proposedText.replacingOccurrences(of: decimalSeparator, with: ".")) {
|
|
|
|
|
+ let quantity = HKQuantity(unit: unit, doubleValue: number)
|
|
|
|
|
+ if isWithinLimits(quantity) {
|
|
|
|
|
+ parent.quantity = quantity
|
|
|
|
|
+ } else {
|
|
|
|
|
+ parent.quantity = HKQuantity(unit: unit, doubleValue: 0)
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ parent.quantity = HKQuantity(unit: unit, doubleValue: 0)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return isValidInput
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public func textFieldDidBeginEditing(_: UITextField) {
|
|
public func textFieldDidBeginEditing(_: UITextField) {
|
|
|
parent.textFieldDidBeginEditing?()
|
|
parent.textFieldDidBeginEditing?()
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ func format(quantity: HKQuantity, for unit: HKUnit) -> String {
|
|
|
|
|
+ let value = quantity.doubleValue(for: unit)
|
|
|
|
|
+ let formatter = NumberFormatter()
|
|
|
|
|
+ formatter.minimumFractionDigits = unit.preferredFractionDigits
|
|
|
|
|
+ formatter.maximumFractionDigits = unit.preferredFractionDigits
|
|
|
|
|
+ formatter.numberStyle = .decimal
|
|
|
|
|
+ return formatter.string(from: NSNumber(value: value)) ?? ""
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func isWithinLimits(_ quantity: HKQuantity) -> Bool {
|
|
|
|
|
+ if let minValue = minValue, quantity.doubleValue(for: unit) < minValue.doubleValue(for: unit) {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ if let maxValue = maxValue, quantity.doubleValue(for: unit) > maxValue.doubleValue(for: unit) {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|