Skip to content

Instantly share code, notes, and snippets.

@thebnich
Created July 23, 2015 21:15
Show Gist options
  • Save thebnich/c0e5731f9e25b24b1168 to your computer and use it in GitHub Desktop.
Save thebnich/c0e5731f9e25b24b1168 to your computer and use it in GitHub Desktop.
Autocomplete flicker fix
diff --git a/Client/Frontend/Widgets/AutocompleteTextField.swift b/Client/Frontend/Widgets/AutocompleteTextField.swift
index 5ca98a8..0ad478a 100644
--- a/Client/Frontend/Widgets/AutocompleteTextField.swift
+++ b/Client/Frontend/Widgets/AutocompleteTextField.swift
@@ -133,10 +133,24 @@ class AutocompleteTextField: UITextField, UITextFieldDelegate {
}
override func insertText(text: String) {
+ // Before replacing the string, cache the existing autocompletion so we can reuse it
+ // immediately if the user continues the same string.
+ var reuseCompletion = false
+ let suggestion = self.text
+ if completionActive {
+ let endingString = self.text.substringFromIndex(advance(self.text.startIndex, enteredTextLength))
+ reuseCompletion = endingString.startsWith(text)
+ }
+
removeCompletion()
super.insertText(text)
enteredTextLength = count(self.text)
+ // The user entered a character that's still part of the autocomplete result, so apply it now.
+ if reuseCompletion {
+ setAutocompleteSuggestion(suggestion)
+ }
+
notifyTextChanged?()
}
@codestergit
Copy link

Hi @brian ,
I agree flicker fix is little bit complicated and I reach same patch with insertText before. But due to some bug insertText not called in iOS9. We do not have self.text in our control in iOS9. Before inserting text we removed completion removeCompletion() and then super.insertText(text) to enter text without completion and then we set the autoCompletion.This all happen same time so flicker issue will remove.

I have tried that with shouldChangeCharacters where I have simply removed completion and on textDidChange event I have set the AutoCompletion. But in between textField get updated and we get flicker issue again not because of database delay this time but textField get update.
We need to decide before textDidChange event to remove or not remove completion. We should not remove completion each time with our iOS9 fix.
Need your help if we can simplify further. Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment