1
0
Fork 0

regression: fixed the last item remains selected after wrap-scrolling to the first one, when a stem is displayed in the suggestion bar

This commit is contained in:
sspanak 2024-10-01 11:16:17 +03:00 committed by Dimo Karaivanov
parent 7fb509604e
commit 13673aa036
2 changed files with 33 additions and 29 deletions

View file

@ -1,5 +1,6 @@
package io.github.sspanak.tt9.ui.tray;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
@ -57,8 +58,16 @@ public class SuggestionsAdapter extends RecyclerView.Adapter<SuggestionsAdapter.
}
public void setSelection(int index) {
selectedIndex = index;
public void setSelection(int newIndex) {
notifyItemChanged(selectedIndex);
notifyItemChanged(selectedIndex = newIndex);
}
@SuppressLint("NotifyDataSetChanged")
public void resetItems(int newIndex) {
selectedIndex = newIndex;
notifyDataSetChanged();
}

View file

@ -1,6 +1,5 @@
package io.github.sspanak.tt9.ui.tray;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
@ -208,11 +207,10 @@ public class SuggestionsBar {
}
@SuppressLint("NotifyDataSetChanged")
private void setSuggestionsOnScreen() {
if (mView != null) {
mSuggestionsAdapter.setSelection(selectedIndex);
mSuggestionsAdapter.notifyDataSetChanged();
mSuggestionsAdapter.resetItems(selectedIndex);
mView.scrollToPosition(selectedIndex);
}
}
@ -223,48 +221,45 @@ public class SuggestionsBar {
return;
}
int oldIndex = selectedIndex;
calculateScrollIndex(increment);
scrollToIndex();
}
private void calculateScrollIndex(int increment) {
selectedIndex = selectedIndex + increment;
if (selectedIndex == suggestions.size()) {
selectedIndex = 0;
selectedIndex = containsStem() ? 1 : 0;
} else if (selectedIndex < 0) {
selectedIndex = suggestions.size() - 1;
} else if (selectedIndex == 0 && containsStem()) {
selectedIndex = suggestions.size() - 1;
}
if (containsStem() && selectedIndex == 0) {
scrollToSuggestion(increment);
return;
}
scrollToSuggestionOnScreen(oldIndex);
}
private void scrollToSuggestionOnScreen(int oldIndex) {
private void scrollToIndex() {
if (mView == null) {
return;
}
mSuggestionsAdapter.setSelection(selectedIndex);
mSuggestionsAdapter.notifyItemChanged(oldIndex);
mSuggestionsAdapter.notifyItemChanged(selectedIndex);
if (settings.getSuggestionScrollingDelay() > 0) {
alternativeScrollingHandler.removeCallbacksAndMessages(null);
alternativeScrollingHandler.postDelayed(this::scrollToSelectedIndex, settings.getSuggestionScrollingDelay());
alternativeScrollingHandler.postDelayed(this::scrollView, settings.getSuggestionScrollingDelay());
} else {
scrollToSelectedIndex();
scrollView();
}
}
/**
* Tells the adapter to scroll. Always call scrollToSuggestionOnScreen() first,
* Tells the adapter to scroll. Always call scrollToIndex() first,
* to set the selected index in the adapter.
*/
private void scrollToSelectedIndex() {
if (containsStem() && selectedIndex == 1 && getSuggestion(1).length() < getSuggestion(suggestions.size() - 1).length()) {
private void scrollView() {
if (containsStem() && selectedIndex == 1) {
mView.scrollToPosition(0);
} else {
mView.scrollToPosition(selectedIndex);