Skip to content

Commit

Permalink
Revert DoubleTextField,IntTextField
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Aug 3, 2024
1 parent f9bd46b commit f0eb59b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 84 deletions.
66 changes: 24 additions & 42 deletions src/apps/share/swift/Views/DoubleTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double>
Expand Down Expand Up @@ -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
}
}
}
66 changes: 24 additions & 42 deletions src/apps/share/swift/Views/IntTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit f0eb59b

Please sign in to comment.