From 56a408355de2b14840b7423371a9bedc16c10c9c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 7 Feb 2025 00:43:44 -0500 Subject: [PATCH] Remove old EmojiTextField code --- .../Views/ContentWarningTextField.swift | 12 +++--------- .../ComposeUI/Views/EmojiTextField.swift | 19 +++++-------------- .../Sources/ComposeUI/Views/PollEditor.swift | 10 +++++++++- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Views/ContentWarningTextField.swift b/Packages/ComposeUI/Sources/ComposeUI/Views/ContentWarningTextField.swift index acceab8b..50b9e450 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Views/ContentWarningTextField.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Views/ContentWarningTextField.swift @@ -16,15 +16,9 @@ struct ContentWarningTextField: View { text: $draft.contentWarning, placeholder: "Write your warning here", maxLength: nil, - // TODO: completely replace this with FocusState - becomeFirstResponder: .constant(false), - focusNextView: Binding(get: { - false - }, set: { - if $0 { - focusedField = .body - } - }) + focusNextView: { + focusedField = .body + } ) .focused($focusedField, equals: .contentWarning) .modifier(FocusedInputModifier()) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift b/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift index 0a919726..7f7f1f34 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift @@ -17,14 +17,12 @@ struct EmojiTextField: UIViewRepresentable { @Binding var text: String let placeholder: String let maxLength: Int? - let becomeFirstResponder: Binding? - let focusNextView: Binding? + let focusNextView: (() -> Void)? - init(text: Binding, placeholder: String, maxLength: Int?, becomeFirstResponder: Binding? = nil, focusNextView: Binding? = nil) { + init(text: Binding, placeholder: String, maxLength: Int?, focusNextView: (() -> Void)? = nil) { self._text = text self.placeholder = placeholder self.maxLength = maxLength - self.becomeFirstResponder = becomeFirstResponder self.focusNextView = focusNextView } @@ -66,13 +64,6 @@ struct EmojiTextField: UIViewRepresentable { #if !os(visionOS) uiView.backgroundColor = colorScheme == .dark ? UIColor(fillColor) : .secondarySystemBackground #endif - - if becomeFirstResponder?.wrappedValue == true { - DispatchQueue.main.async { - uiView.becomeFirstResponder() - becomeFirstResponder!.wrappedValue = false - } - } } func makeCoordinator() -> Coordinator { @@ -85,7 +76,7 @@ struct EmojiTextField: UIViewRepresentable { class Coordinator: NSObject, UITextFieldDelegate, ComposeInput { var text: Binding - var focusNextView: Binding? + var focusNextView: (() -> Void)? var maxLength: Int? @Published var autocompleteState: AutocompleteState? @@ -93,7 +84,7 @@ struct EmojiTextField: UIViewRepresentable { weak var textField: UITextField? - init(text: Binding, focusNextView: Binding?, maxLength: Int? = nil) { + init(text: Binding, focusNextView: (() -> Void)?, maxLength: Int? = nil) { self.text = text self.focusNextView = focusNextView self.maxLength = maxLength @@ -104,7 +95,7 @@ struct EmojiTextField: UIViewRepresentable { } @objc func returnKeyPressed() { - focusNextView?.wrappedValue = true + focusNextView?() } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { diff --git a/Packages/ComposeUI/Sources/ComposeUI/Views/PollEditor.swift b/Packages/ComposeUI/Sources/ComposeUI/Views/PollEditor.swift index cc7489e0..334e1e55 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Views/PollEditor.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Views/PollEditor.swift @@ -125,7 +125,7 @@ private struct PollOptionEditor: View { .accessibilityLabel("Remove option") .disabled(poll.options.count == 1) - EmojiTextField(text: $option.text, placeholder: placeholder, maxLength: instanceFeatures.maxPollOptionChars) + EmojiTextField(text: $option.text, placeholder: placeholder, maxLength: instanceFeatures.maxPollOptionChars, focusNextView: self.focusNextOption) .focused($focusedField, equals: .pollOption(option.id)) } } @@ -145,6 +145,14 @@ private struct PollOptionEditor: View { } } } + + private func focusNextOption() { + let index = poll.options.index(of: option) + if index != NSNotFound && index + 1 < poll.options.count { + let nextOption = poll.options.object(at: index + 1) as! PollOption + focusedField = .pollOption(nextOption.objectID) + } + } } private struct PollOptionButtonStyle: ButtonStyle {