Code cleanup

This commit is contained in:
ByteHamster 2021-03-05 16:55:55 +01:00
parent 3a2a2abbd9
commit d54ce2f8b1
3 changed files with 27 additions and 46 deletions

View File

@ -135,13 +135,13 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
default:
return null;
}
TypedArray ta = context.obtainStyledAttributes(new int[] { icon } );
TypedArray ta = context.obtainStyledAttributes(new int[] { icon });
Drawable result = ta.getDrawable(0);
ta.recycle();
return result;
}
public List<String> getTags() {
public List<String> getFragmentTags() {
return Collections.unmodifiableList(fragmentTags);
}
@ -160,7 +160,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
if (viewType == VIEW_TYPE_SUBSCRIPTION) {
return itemAccess.getItem(position - getSubscriptionOffset()).id;
} else {
return -position - 1; //TODO
return -position - 1; // IDs are >0
}
}
@ -215,13 +215,9 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
if (viewType != VIEW_TYPE_SECTION_DIVIDER) {
TypedValue typedValue = new TypedValue();
if (itemAccess.isSelected(position)) {
activity.get().getTheme().resolveAttribute(R.attr.drawer_activated_color, typedValue, true);
activity.get().getTheme().resolveAttribute(itemAccess.isSelected(position)
? R.attr.drawer_activated_color : android.R.attr.windowBackground, typedValue, true);
holder.itemView.setBackgroundResource(typedValue.resourceId);
} else {
activity.get().getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true);
holder.itemView.setBackgroundResource(typedValue.resourceId);
}
holder.itemView.setOnClickListener(v -> itemAccess.onItemClick(position));
holder.itemView.setOnLongClickListener(v -> itemAccess.onItemLongClick(position));
@ -230,7 +226,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
private void bindNavView(String title, int position, NavHolder holder) {
Activity context = activity.get();
if(context == null) {
if (context == null) {
return;
}
holder.title.setText(title);
@ -258,11 +254,11 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
holder.count.setText(NumberFormat.getInstance().format(sum));
holder.count.setVisibility(View.VISIBLE);
}
} else if(tag.equals(DownloadsFragment.TAG) && UserPreferences.isEnableAutodownload()) {
} else if (tag.equals(DownloadsFragment.TAG) && UserPreferences.isEnableAutodownload()) {
int epCacheSize = UserPreferences.getEpisodeCacheSize();
// don't count episodes that can be reclaimed
int spaceUsed = itemAccess.getNumberOfDownloadedItems() -
itemAccess.getReclaimableItems();
int spaceUsed = itemAccess.getNumberOfDownloadedItems()
- itemAccess.getReclaimableItems();
if (epCacheSize > 0 && spaceUsed >= epCacheSize) {
holder.count.setText("{md-disc-full 150%}");
@ -272,7 +268,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
new AlertDialog.Builder(context)
.setTitle(R.string.episode_cache_full_title)
.setMessage(R.string.episode_cache_full_message)
.setPositiveButton(android.R.string.ok, (dialog, which) -> {})
.setPositiveButton(android.R.string.ok, (dialog, which) -> { })
.show()
);
}
@ -283,7 +279,7 @@ public class NavListAdapter extends RecyclerView.Adapter<NavListAdapter.Holder>
private void bindSectionDivider(DividerHolder holder) {
Activity context = activity.get();
if(context == null) {
if (context == null) {
return;
}

View File

@ -122,12 +122,13 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
@Override
public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (contextPressedItem.type != NavDrawerData.DrawerItem.Type.FEED) {
return; // Should actually never happen because the context menu is not set up for other items
}
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.nav_feed_context, menu);
if (contextPressedItem.type == NavDrawerData.DrawerItem.Type.FEED) {
menu.setHeaderTitle(((NavDrawerData.FeedDrawerItem) contextPressedItem).feed.getTitle());
}
// episodes are not loaded, so we cannot check if the podcast has new or unplayed ones!
}
@ -179,12 +180,6 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
}
}
private void showMainActivity(String tag) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, tag);
startActivity(intent);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) {
loadData();
@ -266,7 +261,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
public boolean isSelected(int position) {
String lastNavFragment = getLastNavFragment(getContext());
if (position < navAdapter.getSubscriptionOffset()) {
return navAdapter.getTags().get(position).equals(lastNavFragment);
return navAdapter.getFragmentTags().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) {
@ -318,28 +313,18 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
int viewType = navAdapter.getItemViewType(position);
if (viewType != NavListAdapter.VIEW_TYPE_SECTION_DIVIDER) {
if (position < navAdapter.getSubscriptionOffset()) {
String tag = navAdapter.getTags().get(position);
if (getActivity() instanceof MainActivity) {
String tag = navAdapter.getFragmentTags().get(position);
((MainActivity) getActivity()).loadFragment(tag, null);
((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED);
} else {
showMainActivity(tag);
}
} else {
int pos = position - navAdapter.getSubscriptionOffset();
NavDrawerData.DrawerItem clickedItem = flatItemList.get(pos);
if (clickedItem.type == NavDrawerData.DrawerItem.Type.FEED) {
long feedId = ((NavDrawerData.FeedDrawerItem) clickedItem).feed.getId();
if (getActivity() instanceof MainActivity) {
((MainActivity) getActivity()).loadFeedFragmentById(feedId, null);
((MainActivity) getActivity()).getBottomSheet()
.setState(BottomSheetBehavior.STATE_COLLAPSED);
} else {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra(MainActivity.EXTRA_FEED_ID, feedId);
startActivity(intent);
}
} else {
NavDrawerData.FolderDrawerItem folder = ((NavDrawerData.FolderDrawerItem) clickedItem);
if (openFolders.contains(folder.name)) {
@ -371,7 +356,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
@Override
public boolean onItemLongClick(int position) {
if (position < navAdapter.getTags().size()) {
if (position < navAdapter.getFragmentTags().size()) {
showDrawerPreferencesDialog();
return true;
} else {

View File

@ -61,7 +61,7 @@ public class NavDrawerData {
public boolean isOpen;
public FolderDrawerItem(String name) {
super(DrawerItem.Type.FOLDER, (long) name.hashCode() << 32);
super(DrawerItem.Type.FOLDER, (long) name.hashCode() << 20); // Keep IDs >0 but make room for many feeds
this.name = name;
}