From f0eb59bc4ea6fd7d7b91efedcdcd10cbb7655a59 Mon Sep 17 00:00:00 2001 From: Takayama Fumihiko Date: Sat, 3 Aug 2024 17:23:15 +0900 Subject: [PATCH] Revert DoubleTextField,IntTextField --- .../share/swift/Views/DoubleTextField.swift | 66 +++++++------------ src/apps/share/swift/Views/IntTextField.swift | 66 +++++++------------ 2 files changed, 48 insertions(+), 84 deletions(-) diff --git a/src/apps/share/swift/Views/DoubleTextField.swift b/src/apps/share/swift/Views/DoubleTextField.swift index e30b11d05..688bc875e 100644 --- a/src/apps/share/swift/Views/DoubleTextField.swift +++ b/src/apps/share/swift/Views/DoubleTextField.swift @@ -7,8 +7,6 @@ struct DoubleTextField: View { // To avoid this, do not specify the formatter directly in the TextField, but use onChange to format the value. @State private var text = "" @State private var error = false - @State private var ignoreNextUpdateByText: String? - @State private var ignoreNextUpdateByValue: Double? private let step: Double private let range: ClosedRange @@ -78,53 +76,37 @@ struct DoubleTextField: View { } private func update(byValue newValue: Double) { - let ignore = ignoreNextUpdateByValue == newValue - ignoreNextUpdateByValue = nil - if ignore { - return - } - - var newText = text - var newError = true + if let newText = formatter.string(for: newValue) { + error = false - if let t = formatter.string(for: newValue) { - newError = false - newText = t + Task { @MainActor in + if value != newValue { + value = newValue + } + if text != newText { + text = newText + } + } + } else { + error = true } - - updateProperties(newValue: newValue, newText: newText, newError: newError) } private func update(byText newText: String) { - let ignore = ignoreNextUpdateByText == newText - ignoreNextUpdateByText = nil - if ignore { - return - } - - var newValue = value - var newError = true - if let number = formatter.number(from: newText) { - newError = false - newValue = number.doubleValue - } - - updateProperties(newValue: newValue, newText: newText, newError: newError) - } + error = false - private func updateProperties(newValue: Double, newText: String, newError: Bool) { - if value != newValue { - ignoreNextUpdateByValue = newValue - value = newValue - } - if text != newText { - ignoreNextUpdateByText = newText - text = newText - } - - if error != newError { - error = newError + let newValue = number.doubleValue + Task { @MainActor in + if value != newValue { + value = newValue + } + if text != newText { + text = newText + } + } + } else { + error = true } } } diff --git a/src/apps/share/swift/Views/IntTextField.swift b/src/apps/share/swift/Views/IntTextField.swift index fa8a32fb9..839dda4a7 100644 --- a/src/apps/share/swift/Views/IntTextField.swift +++ b/src/apps/share/swift/Views/IntTextField.swift @@ -7,8 +7,6 @@ struct IntTextField: View { // To avoid this, do not apply the formatter directly to the TextField; instead, apply the formatting in the onChange event. @State private var text = "" @State private var error = false - @State private var ignoreNextUpdateByText: String? - @State private var ignoreNextUpdateByValue: Int? private let step: Int private let range: ClosedRange @@ -69,53 +67,37 @@ struct IntTextField: View { } private func update(byValue newValue: Int) { - let ignore = ignoreNextUpdateByValue == newValue - ignoreNextUpdateByValue = nil - if ignore { - return - } - - var newText = text - var newError = true + if let newText = formatter.string(for: newValue) { + error = false - if let t = formatter.string(for: newValue) { - newError = false - newText = t + Task { @MainActor in + if value != newValue { + value = newValue + } + if text != newText { + text = newText + } + } + } else { + error = true } - - updateProperties(newValue: newValue, newText: newText, newError: newError) } private func update(byText newText: String) { - let ignore = ignoreNextUpdateByText == newText - ignoreNextUpdateByText = nil - if ignore { - return - } - - var newValue = value - var newError = true - if let number = formatter.number(from: newText) { - newError = false - newValue = number.intValue - } - - updateProperties(newValue: newValue, newText: newText, newError: newError) - } + error = false - private func updateProperties(newValue: Int, newText: String, newError: Bool) { - if value != newValue { - ignoreNextUpdateByValue = newValue - value = newValue - } - if text != newText { - ignoreNextUpdateByText = newText - text = newText - } - - if error != newError { - error = newError + let newValue = number.intValue + Task { @MainActor in + if value != newValue { + value = newValue + } + if text != newText { + text = newText + } + } + } else { + error = true } } }