Fix system autocomplete not working in Compose post body and CW

This commit is contained in:
Shadowfacts 2020-10-24 11:19:35 -04:00
parent e763d48bf3
commit 8c4ef3caa6
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 16 additions and 0 deletions

View File

@ -58,6 +58,10 @@ struct ComposeContentWarningTextField: UIViewRepresentable {
} }
func textFieldDidChangeSelection(_ textField: UITextField) { func textFieldDidChangeSelection(_ textField: UITextField) {
// Update text binding before potentially triggering SwiftUI view update.
// See comment in MainComposeTextView.Coordinator.textViewDidChangeSelection
text.wrappedValue = textField.text ?? ""
updateAutocompleteState(textField: textField) updateAutocompleteState(textField: textField)
} }

View File

@ -144,6 +144,7 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
func updateUIView(_ uiView: UITextView, context: Context) { func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text uiView.text = text
if let visibilityButton = visibilityButton { if let visibilityButton = visibilityButton {
visibilityButton.image = UIImage(systemName: visibility.imageName) visibilityButton.image = UIImage(systemName: visibility.imageName)
updateVisibilityMenu(visibilityButton) updateVisibilityMenu(visibilityButton)
@ -227,6 +228,17 @@ struct MainComposeWrappedTextView: UIViewRepresentable {
} }
func textViewDidChangeSelection(_ textView: UITextView) { func textViewDidChangeSelection(_ textView: UITextView) {
// Update the value of the text binding.
// Sometimes, when the user accepts an autocomplete suggestion from the system keyboard, the system
// calls didChangeSelection before textDidChange, resulting in a loop where the updating the Tusker autocomplete
// state in didChangeSection (via updateAutocompleteState) triggers a new SwiftUI view update,
// but when that SwiftUI update is handled, the model still has the old text (from prior to accepting the autocomplete
// suggestion), meaning the UITextView's text gets set back to whatever it was prior to the system autocomplete.
// To work around that, we also update the text binding in didChangeSelection, to ensure that, if the autocomplete state
// does change and trigger a SwiftUI update, the binding will have the correct text that was produced by the system
// autocompletion.
text.wrappedValue = textView.text ?? ""
updateAutocompleteState() updateAutocompleteState()
} }