1
0
Fork 0

fixed crashing when scrolling the suggestions in ABC mode (caused by modifying the RecyclerView from different threads)

This commit is contained in:
sspanak 2025-04-22 17:24:53 +03:00 committed by Dimo Karaivanov
parent b71c989e07
commit fed5ee10da
3 changed files with 14 additions and 16 deletions

View file

@ -34,8 +34,6 @@ public class SettingsStore extends SettingsHotkeys {
public final static float SOFT_KEY_V_SHAPE_RATIO_OUTER = (float) Math.pow(SOFT_KEY_V_SHAPE_RATIO_INNER, 2);
public final static int SUGGESTIONS_MAX = 20;
public final static int SUGGESTIONS_MIN = 8;
public final static int SUGGESTIONS_RENDER_DEBOUNCE_TIME = 25; // ms
public final static int SUGGESTIONS_RENDER_CLEAR_DEBOUNCE_TIME = 60; // ms
public final static int SUGGESTIONS_SELECT_ANIMATION_DURATION = 66;
public final static int SUGGESTIONS_TRANSLATE_ANIMATION_DURATION = 0;
public final static int TEXT_INPUT_DEBOUNCE_TIME = 500; // ms

View file

@ -47,9 +47,10 @@ public class SuggestionsAdapter extends RecyclerView.Adapter<SuggestionsAdapter.
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.suggestionItem.setText(mSuggestions.get(position));
holder.suggestionItem.setTag(position);
holder.suggestionItem.setTextColor(selectedIndex == position ? colorHighlight : colorDefault);
holder.suggestionItem.setBackgroundColor(selectedIndex == position ? backgroundHighlight : Color.TRANSPARENT);
holder.suggestionItem.setOnClickListener(v -> onItemClick.accept(holder.getAdapterPosition()));
holder.suggestionItem.setOnClickListener(v -> onItemClick.accept((int) v.getTag()));
}

View file

@ -185,6 +185,10 @@ public class SuggestionsBar {
public void setMany(@Nullable List<String> newSuggestions, int initialSel, boolean containsGenerated) {
if ((suggestions == null || suggestions.isEmpty()) && (newSuggestions == null || newSuggestions.isEmpty())) {
return;
}
suggestions = newSuggestions;
selectedIndex = newSuggestions == null || newSuggestions.isEmpty() ? 0 : Math.max(initialSel, 0);
@ -194,9 +198,9 @@ public class SuggestionsBar {
boolean onlySpecialChars = newSuggestions != null && !newSuggestions.isEmpty() && !(new Text(newSuggestions.get(0)).isAlphabetic());
addMany(newSuggestions, mView == null || onlySpecialChars ? Integer.MAX_VALUE : SettingsStore.SUGGESTIONS_MAX);
selectedIndex = Math.min(selectedIndex, visibleSuggestions.size() - 1);
selectedIndex = Math.max(Math.min(selectedIndex, visibleSuggestions.size() - 1), 0);
renderDebounced();
render();
}
@ -266,17 +270,6 @@ public class SuggestionsBar {
}
/**
* Reduces flashing of the suggestions bar when the suggestions are empty and saves some resources
* by reducing the calls to render().
*/
private void renderDebounced() {
final int delay = visibleSuggestions.isEmpty() ? SettingsStore.SUGGESTIONS_RENDER_CLEAR_DEBOUNCE_TIME : SettingsStore.SUGGESTIONS_RENDER_DEBOUNCE_TIME;
delayedDisplayHandler.removeCallbacksAndMessages(null);
delayedDisplayHandler.postDelayed(this::render, delay);
}
private void render() {
if (mView == null) {
return;
@ -308,6 +301,7 @@ public class SuggestionsBar {
visibleSuggestions.clear();
addMany(suggestions, Integer.MAX_VALUE);
selectedIndex = scrollBack || selectedIndex >= visibleSuggestions.size() ? visibleSuggestions.size() - 1 : selectedIndex;
selectedIndex = Math.max(selectedIndex, 0);
return true;
}
@ -341,6 +335,11 @@ public class SuggestionsBar {
private void calculateScrollIndex(int increment) {
if (visibleSuggestions.isEmpty()) {
selectedIndex = 0;
return;
}
selectedIndex = selectedIndex + increment;
if (selectedIndex == visibleSuggestions.size()) {
selectedIndex = containsStem() ? 1 : 0;