Ivan Valkou 4 лет назад
Родитель
Сommit
82ade92bd8
1 измененных файлов с 17 добавлено и 2 удалено
  1. 17 2
      FreeAPS/Sources/Views/DecimalTextField.swift

+ 17 - 2
FreeAPS/Sources/Views/DecimalTextField.swift

@@ -1,3 +1,4 @@
+import Combine
 import SwiftUI
 
 struct DecimalTextField: UIViewRepresentable {
@@ -62,8 +63,11 @@ struct DecimalTextField: UIViewRepresentable {
         return textfield
     }
 
-    func updateUIView(_ textField: UITextField, context _: Context) {
-        if value != 0 {
+    func updateUIView(_ textField: UITextField, context: Context) {
+        let coordinator = context.coordinator
+        if coordinator.isEditing {
+            coordinator.resetEditing()
+        } else if value != 0 {
             textField.text = formatter.string(for: value)
         }
     }
@@ -79,6 +83,15 @@ struct DecimalTextField: UIViewRepresentable {
             parent = textField
         }
 
+        private(set) var isEditing = false
+        private var editingCancellable: AnyCancellable?
+
+        func resetEditing() {
+            editingCancellable = Just(false)
+                .delay(for: 0.5, scheduler: DispatchQueue.main)
+                .weakAssign(to: \.isEditing, on: self)
+        }
+
         func textField(
             _ textField: UITextField,
             shouldChangeCharactersIn range: NSRange,
@@ -106,6 +119,7 @@ struct DecimalTextField: UIViewRepresentable {
 
                 // Set Value
                 let double = number.doubleValue
+                isEditing = true
                 parent.value = Decimal(double)
             }
 
@@ -118,6 +132,7 @@ struct DecimalTextField: UIViewRepresentable {
         ) {
             // Format value with formatter at End Editing
             textField.text = parent.formatter.string(for: parent.value)
+            isEditing = false
         }
     }
 }