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