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

View File

@ -122,12 +122,13 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
@Override @Override
public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, ContextMenu.ContextMenuInfo menuInfo) { public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, 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(); MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.nav_feed_context, menu); inflater.inflate(R.menu.nav_feed_context, menu);
menu.setHeaderTitle(((NavDrawerData.FeedDrawerItem) contextPressedItem).feed.getTitle());
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! // 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) @Subscribe(threadMode = ThreadMode.MAIN)
public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) { public void onUnreadItemsChanged(UnreadItemsUpdateEvent event) {
loadData(); loadData();
@ -266,7 +261,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
public boolean isSelected(int position) { public boolean isSelected(int position) {
String lastNavFragment = getLastNavFragment(getContext()); String lastNavFragment = getLastNavFragment(getContext());
if (position < navAdapter.getSubscriptionOffset()) { 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 } else if (StringUtils.isNumeric(lastNavFragment)) { // last fragment was not a list, but a feed
long feedId = Long.parseLong(lastNavFragment); long feedId = Long.parseLong(lastNavFragment);
if (navDrawerData != null) { if (navDrawerData != null) {
@ -318,28 +313,18 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
int viewType = navAdapter.getItemViewType(position); int viewType = navAdapter.getItemViewType(position);
if (viewType != NavListAdapter.VIEW_TYPE_SECTION_DIVIDER) { if (viewType != NavListAdapter.VIEW_TYPE_SECTION_DIVIDER) {
if (position < navAdapter.getSubscriptionOffset()) { if (position < navAdapter.getSubscriptionOffset()) {
String tag = navAdapter.getTags().get(position); String tag = navAdapter.getFragmentTags().get(position);
if (getActivity() instanceof MainActivity) { ((MainActivity) getActivity()).loadFragment(tag, null);
((MainActivity) getActivity()).loadFragment(tag, null); ((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED);
((MainActivity) getActivity()).getBottomSheet().setState(BottomSheetBehavior.STATE_COLLAPSED);
} else {
showMainActivity(tag);
}
} else { } else {
int pos = position - navAdapter.getSubscriptionOffset(); int pos = position - navAdapter.getSubscriptionOffset();
NavDrawerData.DrawerItem clickedItem = flatItemList.get(pos); NavDrawerData.DrawerItem clickedItem = flatItemList.get(pos);
if (clickedItem.type == NavDrawerData.DrawerItem.Type.FEED) { if (clickedItem.type == NavDrawerData.DrawerItem.Type.FEED) {
long feedId = ((NavDrawerData.FeedDrawerItem) clickedItem).feed.getId(); long feedId = ((NavDrawerData.FeedDrawerItem) clickedItem).feed.getId();
if (getActivity() instanceof MainActivity) { ((MainActivity) getActivity()).loadFeedFragmentById(feedId, null);
((MainActivity) getActivity()).loadFeedFragmentById(feedId, null); ((MainActivity) getActivity()).getBottomSheet()
((MainActivity) getActivity()).getBottomSheet() .setState(BottomSheetBehavior.STATE_COLLAPSED);
.setState(BottomSheetBehavior.STATE_COLLAPSED);
} else {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra(MainActivity.EXTRA_FEED_ID, feedId);
startActivity(intent);
}
} else { } else {
NavDrawerData.FolderDrawerItem folder = ((NavDrawerData.FolderDrawerItem) clickedItem); NavDrawerData.FolderDrawerItem folder = ((NavDrawerData.FolderDrawerItem) clickedItem);
if (openFolders.contains(folder.name)) { if (openFolders.contains(folder.name)) {
@ -371,7 +356,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS
@Override @Override
public boolean onItemLongClick(int position) { public boolean onItemLongClick(int position) {
if (position < navAdapter.getTags().size()) { if (position < navAdapter.getFragmentTags().size()) {
showDrawerPreferencesDialog(); showDrawerPreferencesDialog();
return true; return true;
} else { } else {

View File

@ -61,7 +61,7 @@ public class NavDrawerData {
public boolean isOpen; public boolean isOpen;
public FolderDrawerItem(String name) { 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; this.name = name;
} }