Fix selection highlighting

This commit is contained in:
ByteHamster 2021-03-05 11:18:01 +01:00
parent 627bd35500
commit d1d5974cea
2 changed files with 20 additions and 38 deletions

View File

@ -215,7 +215,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
if (viewType != VIEW_TYPE_SECTION_DIVIDER) {
TypedValue typedValue = new TypedValue();
if (position == itemAccess.getSelectedItemIndex()) {
if (itemAccess.isSelected(position)) {
activity.get().getTheme().resolveAttribute(R.attr.drawer_activated_color, typedValue, true);
holder.itemView.setBackgroundResource(typedValue.resourceId);
} else {
@ -399,7 +399,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
NavDrawerData.DrawerItem getItem(int position);
int getSelectedItemIndex();
boolean isSelected(int position);
int getQueueSize();
@ -409,8 +409,6 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
int getReclaimableItems();
int getFeedCounter(long feedId);
int getFeedCounterSum();
void onItemClick(int position);

View File

@ -72,7 +72,6 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
private NavDrawerData navDrawerData;
private List<NavDrawerData.DrawerItem> flatItemList;
private int selectedNavListIndex = -1;
private NavDrawerData.DrawerItem contextPressedItem = null;
private NavListAdapter navAdapter;
private Disposable disposable;
@ -91,7 +90,6 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
navAdapter.setHasStableIds(true);
navList.setAdapter(navAdapter);
navList.setLayoutManager(new LinearLayoutManager(getContext()));
updateSelection();
root.findViewById(R.id.nav_settings).setOnClickListener(v ->
startActivity(new Intent(getActivity(), PreferenceActivity.class)));
@ -100,27 +98,6 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
return root;
}
private void updateSelection() {
String lastNavFragment = getLastNavFragment(getContext());
int tagIndex = navAdapter.getTags().indexOf(lastNavFragment);
if (tagIndex >= 0) {
selectedNavListIndex = tagIndex;
} else if (StringUtils.isNumeric(lastNavFragment)) { // last fragment was not a list, but a feed
long feedId = Long.parseLong(lastNavFragment);
if (navDrawerData != null) {
List<NavDrawerData.DrawerItem> items = flatItemList;
for (int i = 0; i < items.size(); i++) {
if (items.get(i).type == NavDrawerData.DrawerItem.Type.FEED
&& ((NavDrawerData.FeedDrawerItem) items.get(i)).feed.getId() == feedId) {
selectedNavListIndex = navAdapter.getSubscriptionOffset() + i;
break;
}
}
}
}
navAdapter.notifyDataSetChanged();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
@ -256,7 +233,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
});
builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
UserPreferences.setHiddenDrawerItems(hiddenDrawerItems);
updateSelection();
navAdapter.notifyDataSetChanged(); // Update selection
});
builder.setNegativeButton(R.string.cancel_label, null);
builder.create().show();
@ -282,8 +259,22 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
}
@Override
public int getSelectedItemIndex() {
return selectedNavListIndex;
public boolean isSelected(int position) {
String lastNavFragment = getLastNavFragment(getContext());
if (position < navAdapter.getSubscriptionOffset()) {
return navAdapter.getTags().get(position).equals(lastNavFragment);
} else if (StringUtils.isNumeric(lastNavFragment)) { // last fragment was not a list, but a feed
long feedId = Long.parseLong(lastNavFragment);
if (navDrawerData != null) {
NavDrawerData.DrawerItem itemToCheck = flatItemList.get(
position - navAdapter.getSubscriptionOffset());
if (itemToCheck.type == NavDrawerData.DrawerItem.Type.FEED) {
// When the same feed is displayed multiple times, it should be highlighted multiple times.
return ((NavDrawerData.FeedDrawerItem) itemToCheck).feed.getId() == feedId;
}
}
}
return false;
}
@Override
@ -306,11 +297,6 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
return (navDrawerData != null) ? navDrawerData.reclaimableSpace : 0;
}
@Override
public int getFeedCounter(long feedId) {
return navDrawerData != null ? navDrawerData.feedCounters.get(feedId) : 0;
}
@Override
public int getFeedCounterSum() {
if (navDrawerData == null) {
@ -404,7 +390,6 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
result -> {
navDrawerData = result.first;
flatItemList = result.second;
updateSelection(); // Selected item might be a feed
navAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE); // Stays hidden once there is something in the list
}, error -> {
@ -451,8 +436,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (PREF_LAST_FRAGMENT_TAG.equals(key)) {
updateSelection();
navAdapter.notifyDataSetChanged();
navAdapter.notifyDataSetChanged(); // Update selection
}
}
}