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

Merge branch 'dev' of github.com:nightscout/Trio into core-data-sync-trio

Deniz Cengiz пре 1 година
родитељ
комит
a341656671
2 измењених фајлова са 32 додато и 1 уклоњено
  1. 1 1
      Config.xcconfig
  2. 31 0
      FreeAPS/Sources/Views/TextFieldWithToolBar.swift

+ 1 - 1
Config.xcconfig

@@ -1,5 +1,5 @@
 APP_DISPLAY_NAME = Trio
-APP_VERSION = 0.1.0
+APP_VERSION = 0.2.0
 APP_BUILD_NUMBER = 1
 COPYRIGHT_NOTICE =
 DEVELOPER_TEAM = ##TEAM_ID##

+ 31 - 0
FreeAPS/Sources/Views/TextFieldWithToolBar.swift

@@ -135,6 +135,26 @@ public struct TextFieldWithToolBar: UIViewRepresentable {
                 textField.moveCursorToEnd()
             }
         }
+
+        // Helper method to calculate the number of decimal places in a string
+        fileprivate func calculateDecimalPlaces(in string: String) -> Int {
+            guard let decimalSeparator = decimalFormatter.decimalSeparator else { return 0 }
+            if let range = string.range(of: decimalSeparator) {
+                let decimalPart = string[range.upperBound...]
+                return decimalPart.count
+            }
+            return 0
+        }
+
+        // Helper method to check if the cursor is after the decimal separator
+        fileprivate func isCursorAfterDecimal(in textField: UITextField, range: NSRange) -> Bool {
+            guard let text = textField.text, let decimalSeparator = decimalFormatter.decimalSeparator else { return false }
+            if let decimalSeparatorRange = text.range(of: decimalSeparator) {
+                let decimalSeparatorPosition = text.distance(from: text.startIndex, to: decimalSeparatorRange.lowerBound)
+                return range.location > decimalSeparatorPosition
+            }
+            return false
+        }
     }
 }
 
@@ -161,6 +181,17 @@ extension TextFieldWithToolBar.Coordinator: UITextFieldDelegate {
             // Try to convert proposed text to number
             let number = parent.numberFormatter.number(from: proposedText) ?? decimalFormatter.number(from: proposedText)
 
+            let decimalPlacesCurrent = calculateDecimalPlaces(in: currentText as String)
+            let maxDecimalPlaces = parent.numberFormatter.maximumFractionDigits
+            let isCursorAfterDecimal = isCursorAfterDecimal(in: textField, range: range)
+
+            if decimalPlacesCurrent >= maxDecimalPlaces,
+               range.length == 0,
+               isCursorAfterDecimal
+            {
+                return false
+            }
+
             // Update the binding value if conversion is successful
             if let number = number {
                 let lastCharIndex = proposedText.index(before: proposedText.endIndex)