Fixed crash when opening subscriptions screen

This commit is contained in:
ByteHamster 2021-01-02 17:16:57 +01:00
parent 3ed3379432
commit b107e6da95
3 changed files with 70 additions and 23 deletions

View File

@ -1,5 +1,6 @@
package de.danoeh.antennapod.adapter; package de.danoeh.antennapod.adapter;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import androidx.core.text.TextUtilsCompat; import androidx.core.text.TextUtilsCompat;
@ -22,6 +23,7 @@ import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.LocalFeedUpdater; import de.danoeh.antennapod.core.feed.LocalFeedUpdater;
import de.danoeh.antennapod.core.storage.NavDrawerData; import de.danoeh.antennapod.core.storage.NavDrawerData;
import de.danoeh.antennapod.core.util.ThemeUtils;
import de.danoeh.antennapod.fragment.FeedItemlistFragment; import de.danoeh.antennapod.fragment.FeedItemlistFragment;
import jp.shts.android.library.TriangleLabelView; import jp.shts.android.library.TriangleLabelView;
@ -61,7 +63,7 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
@Override @Override
public long getItemId(int position) { public long getItemId(int position) {
return position; // TODO return ((NavDrawerData.DrawerItem) getItem(position)).id;
} }
@Override @Override
@ -84,11 +86,13 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
holder = (Holder) convertView.getTag(); holder = (Holder) convertView.getTag();
} }
final Feed feed = (Feed) getItem(position); final NavDrawerData.DrawerItem drawerItem = (NavDrawerData.DrawerItem) getItem(position);
if (feed == null) return null; if (drawerItem == null) {
return null;
}
holder.feedTitle.setText(feed.getTitle()); holder.feedTitle.setText(drawerItem.getTitle());
holder.imageView.setContentDescription(feed.getTitle()); holder.imageView.setContentDescription(drawerItem.getTitle());
holder.feedTitle.setVisibility(View.VISIBLE); holder.feedTitle.setVisibility(View.VISIBLE);
// Fix TriangleLabelView corner for RTL // Fix TriangleLabelView corner for RTL
@ -97,30 +101,49 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
holder.count.setCorner(TriangleLabelView.Corner.TOP_LEFT); holder.count.setCorner(TriangleLabelView.Corner.TOP_LEFT);
} }
int count = itemAccess.getFeedCounter(feed.getId()); if (drawerItem.getCounter() > 0) {
if(count > 0) { holder.count.setPrimaryText(NumberFormat.getInstance().format(drawerItem.getCounter()));
holder.count.setPrimaryText(
NumberFormat.getInstance().format(itemAccess.getFeedCounter(feed.getId())));
holder.count.setVisibility(View.VISIBLE); holder.count.setVisibility(View.VISIBLE);
} else { } else {
holder.count.setVisibility(View.GONE); holder.count.setVisibility(View.GONE);
} }
boolean textAndImageCombined = feed.isLocalFeed() if (drawerItem.type == NavDrawerData.DrawerItem.Type.FEED) {
&& LocalFeedUpdater.getDefaultIconUrl(convertView.getContext()).equals(feed.getImageUrl()); Feed feed = ((NavDrawerData.FeedDrawerItem) drawerItem).feed;
new CoverLoader(mainActivityRef.get()) boolean textAndImageCombined = feed.isLocalFeed()
.withUri(feed.getImageLocation()) && LocalFeedUpdater.getDefaultIconUrl(convertView.getContext()).equals(feed.getImageUrl());
.withPlaceholderView(holder.feedTitle, textAndImageCombined) new CoverLoader(mainActivityRef.get())
.withCoverView(holder.imageView) .withUri(feed.getImageLocation())
.load(); .withPlaceholderView(holder.feedTitle, textAndImageCombined)
.withCoverView(holder.imageView)
.load();
} else {
String resourceEntryName = mainActivityRef.get().getResources()
.getResourceEntryName(ThemeUtils.getDrawableFromAttr(mainActivityRef.get(), R.attr.ic_folder));
String iconUri = ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
+ mainActivityRef.get().getPackageName() + "/raw/"
+ resourceEntryName;
new CoverLoader(mainActivityRef.get())
.withUri(iconUri)
.withPlaceholderView(holder.feedTitle, true)
.withCoverView(holder.imageView)
.load();
}
return convertView; return convertView;
} }
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = FeedItemlistFragment.newInstance(getItemId(position)); final NavDrawerData.DrawerItem drawerItem = (NavDrawerData.DrawerItem) getItem(position);
mainActivityRef.get().loadChildFragment(fragment); if (drawerItem == null) {
return;
}
if (drawerItem.type == NavDrawerData.DrawerItem.Type.FEED) {
Feed feed = ((NavDrawerData.FeedDrawerItem) drawerItem).feed;
Fragment fragment = FeedItemlistFragment.newInstance(feed.getId());
mainActivityRef.get().loadChildFragment(fragment);
}
} }
static class Holder { static class Holder {

View File

@ -868,18 +868,20 @@ public final class DBReader {
List<NavDrawerData.DrawerItem> items = new ArrayList<>(); List<NavDrawerData.DrawerItem> items = new ArrayList<>();
for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) {
items.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 0, i)); items.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 0, i, feedCounters.get(feeds.get(i).getId())));
} }
List<NavDrawerData.DrawerItem> folderItems = new ArrayList<>(); List<NavDrawerData.DrawerItem> folderItems = new ArrayList<>();
for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) {
folderItems.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 1, 10001 + i)); folderItems.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 1, 10001 + i,
feedCounters.get(feeds.get(i).getId())));
} }
items.add(new NavDrawerData.FolderDrawerItem("Folder 1", folderItems, 0, 10000)); items.add(new NavDrawerData.FolderDrawerItem("Folder 1", folderItems, 0, 10000));
List<NavDrawerData.DrawerItem> folderItems2 = new ArrayList<>(); List<NavDrawerData.DrawerItem> folderItems2 = new ArrayList<>();
for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) { for (int i = 0, feedsSize = feeds.size(); i < feedsSize; i++) {
folderItems2.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 1, 20001 + i)); folderItems2.add(new NavDrawerData.FeedDrawerItem(feeds.get(i), 1, 20001 + i,
feedCounters.get(feeds.get(i).getId())));
} }
items.add(new NavDrawerData.FolderDrawerItem("Folder 2", folderItems2, 0, 20000)); items.add(new NavDrawerData.FolderDrawerItem("Folder 2", folderItems2, 0, 20000));

View File

@ -27,7 +27,7 @@ public class NavDrawerData {
this.reclaimableSpace = reclaimableSpace; this.reclaimableSpace = reclaimableSpace;
} }
public static class DrawerItem { public abstract static class DrawerItem {
public enum Type { public enum Type {
FOLDER, FEED FOLDER, FEED
} }
@ -41,6 +41,10 @@ public class NavDrawerData {
this.layer = layer; this.layer = layer;
this.id = id; this.id = id;
} }
public abstract String getTitle();
public abstract int getCounter();
} }
public static class FolderDrawerItem extends DrawerItem { public static class FolderDrawerItem extends DrawerItem {
@ -52,14 +56,32 @@ public class NavDrawerData {
this.children = children; this.children = children;
this.name = name; this.name = name;
} }
public String getTitle() {
return name;
}
public int getCounter() {
return -1;
}
} }
public static class FeedDrawerItem extends DrawerItem { public static class FeedDrawerItem extends DrawerItem {
public final Feed feed; public final Feed feed;
public final int counter;
public FeedDrawerItem(Feed feed, int layer, long id) { public FeedDrawerItem(Feed feed, int layer, long id, int counter) {
super(DrawerItem.Type.FEED, layer, id); super(DrawerItem.Type.FEED, layer, id);
this.feed = feed; this.feed = feed;
this.counter = counter;
}
public String getTitle() {
return feed.getTitle();
}
public int getCounter() {
return counter;
} }
} }
} }