Преглед изворни кода

only initialize decimal formatter once

polscm32 aka Marvout пре 1 година
родитељ
комит
bbc0c60627
1 измењених фајлова са 5 додато и 7 уклоњено
  1. 5 7
      FreeAPS/Sources/Views/DecimalTextField.swift

+ 5 - 7
FreeAPS/Sources/Views/DecimalTextField.swift

@@ -111,12 +111,15 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
         var parent: TextFieldWithToolBar
         var textField: UITextField?
         let maxLength: Int?
-
         var didBecomeFirstResponder = false
+        let decimalFormatter: NumberFormatter
 
         init(_ parent: TextFieldWithToolBar, maxLength: Int?) {
             self.parent = parent
             self.maxLength = maxLength
+            decimalFormatter = NumberFormatter()
+            decimalFormatter.locale = Locale.current
+            decimalFormatter.numberStyle = .decimal
         }
 
         @objc fileprivate func clearText() {
@@ -140,7 +143,7 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
     ) -> Bool {
         // Check if the input is a number or the decimal separator
         let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
-        let isDecimalSeparator = (string == NumberFormatter().decimalSeparator && textField.text?.contains(string) == false)
+        let isDecimalSeparator = (string == decimalFormatter.decimalSeparator && textField.text?.contains(string) == false)
 
         // Only proceed if the input is a valid number or decimal separator
         if isNumber || isDecimalSeparator,
@@ -149,11 +152,6 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
             // Get the proposed new text
             let proposedText = currentText.replacingCharacters(in: range, with: string)
 
-            // Initialize number formatter
-            let decimalFormatter = NumberFormatter()
-            decimalFormatter.locale = Locale.current
-            decimalFormatter.numberStyle = .decimal
-
             // Try to convert proposed text to number
             let number = parent.numberFormatter.number(from: proposedText) ?? decimalFormatter.number(from: proposedText)