Fix compose autocomplete suggestions not displaying

c737354ed3 was overzealous
This commit is contained in:
Shadowfacts 2021-05-01 19:18:00 -04:00
parent 698b045f86
commit 90f17693f1
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 28 additions and 2 deletions

View File

@ -32,7 +32,11 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
if context.coordinator.skipSettingTextOnNextUpdate {
context.coordinator.skipSettingTextOnNextUpdate = false
} else {
uiView.text = text
}
}
func makeCoordinator() -> Coordinator {
@ -44,6 +48,8 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
var text: Binding<String>!
var uiState: ComposeUIState!
var skipSettingTextOnNextUpdate = false
@objc func didChange(_ textField: UITextField) {
text.wrappedValue = textField.text ?? ""
}
@ -57,6 +63,12 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
updateAutocompleteState(textField: textField)
}
func textFieldDidChangeSelection(_ textField: UITextField) {
// see MainComposeTextView.Coordinator.textViewDidChangeSelection(_:)
skipSettingTextOnNextUpdate = true
self.updateAutocompleteState(textField: textField)
}
func autocomplete(with string: String) {
guard let textField = textField,
let text = textField.text,

View File

@ -135,7 +135,11 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
if context.coordinator.skipSettingTextOnNextUpdate {
context.coordinator.skipSettingTextOnNextUpdate = false
} else {
uiView.text = text
}
if let visibilityButton = visibilityButton {
visibilityButton.image = UIImage(systemName: visibility.imageName)
@ -199,6 +203,8 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
var uiState: ComposeUIState
var caretScrollPositionAnimator: UIViewPropertyAnimator?
var skipSettingTextOnNextUpdate = false
init(text: Binding<String>, uiState: ComposeUIState, didChange: @escaping (UITextView) -> Void) {
self.text = text
self.didChange = didChange
@ -258,6 +264,14 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
updateAutocompleteState()
}
func textViewDidChangeSelection(_ textView: UITextView) {
// Setting the text view's text causes it to move the cursor to the end (though only
// when the text contains an emoji :/), so skip setting the text on the next SwiftUI update
// that's triggered by setting the autocomplete state.
skipSettingTextOnNextUpdate = true
self.updateAutocompleteState()
}
func autocomplete(with string: String) {
guard let textView = textView,
let text = textView.text,