Use a longer debounce delay for short search strings

This gives the user more time to type in their whole query before a search blocks the main thread.

Searches for short strings often have many results that aren't really useful to the user. They also take much longer to process.

This commit increases the debounce delay to 200ms for short strings three characters long or less. Longer strings still have the 50ms delay. Hopefully, this is a good balance between avoiding slowdowns for short strings and providing a snappy interactive experience for long strings.
This commit is contained in:
Robbie Cooper 2024-08-27 20:04:30 -04:00
parent 869b2cc5c7
commit a7859e1faa

View file

@ -48,6 +48,8 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
private SearchView searchView; private SearchView searchView;
private String searchQuery = null; private String searchQuery = null;
private static final int delay = 50; // If the search string does not change after $delay ms, then the search task starts. private static final int delay = 50; // If the search string does not change after $delay ms, then the search task starts.
private static final int shortStringDelay = 200; // A longer delay for short search strings.
private static final int shortStringSize = 3; // The maximum length of a short search string.
private boolean directEditRemotelyAvailable = false; // avoid using this directly, instead use: isDirectEditEnabled() private boolean directEditRemotelyAvailable = false; // avoid using this directly, instead use: isDirectEditEnabled()
@ColorInt @ColorInt
@ -228,7 +230,8 @@ public abstract class SearchableBaseNoteFragment extends BaseNoteFragment {
handler.removeCallbacksAndMessages(null); handler.removeCallbacksAndMessages(null);
} }
delayQueryTask = new DelayQueryRunnable(newText); delayQueryTask = new DelayQueryRunnable(newText);
handler.postDelayed(delayQueryTask, delay); // If there are few chars in the search pattern, we should start the search later.
handler.postDelayed(delayQueryTask, newText.length() > shortStringSize ? delay : shortStringDelay);
} }
class DelayQueryRunnable implements Runnable { class DelayQueryRunnable implements Runnable {