From e334b9cad6c627d5793898c110e7e0ad57613c20 Mon Sep 17 00:00:00 2001 From: asdoi <36813904+asdoi@users.noreply.github.com> Date: Fri, 16 Oct 2020 11:56:43 +0000 Subject: Extended subscriptions filter (#4502) --- .../antennapod/core/feed/SubscriptionsFilter.java | 106 +++++++++++++++++++++ .../core/feed/SubscriptionsFilterGroup.java | 30 ++++++ .../core/preferences/UserPreferences.java | 15 ++- .../danoeh/antennapod/core/storage/DBReader.java | 13 +-- 4 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilter.java create mode 100644 core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilterGroup.java (limited to 'core/src/main/java/de') diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilter.java b/core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilter.java new file mode 100644 index 000000000..93f098ecf --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilter.java @@ -0,0 +1,106 @@ +package de.danoeh.antennapod.core.feed; + +import android.text.TextUtils; + +import java.util.ArrayList; +import java.util.List; + +import de.danoeh.antennapod.core.util.LongIntMap; + +public class SubscriptionsFilter { + private static final String divider = ","; + + private final String[] properties; + + private boolean showIfCounterGreaterZero = false; + + private boolean showAutoDownloadEnabled = false; + private boolean showAutoDownloadDisabled = false; + + private boolean showUpdatedEnabled = false; + private boolean showUpdatedDisabled = false; + + public SubscriptionsFilter(String properties) { + this(TextUtils.split(properties, divider)); + } + + + public SubscriptionsFilter(String[] properties) { + this.properties = properties; + for (String property : properties) { + // see R.arrays.feed_filter_values + switch (property) { + case "counter_greater_zero": + showIfCounterGreaterZero = true; + break; + case "enabled_auto_download": + showAutoDownloadEnabled = true; + break; + case "disabled_auto_download": + showAutoDownloadDisabled = true; + break; + case "enabled_updates": + showUpdatedEnabled = true; + break; + case "disabled_updates": + showUpdatedDisabled = true; + break; + default: + break; + } + } + } + + public boolean isEnabled() { + return properties.length > 0; + } + + /** + * Run a list of feed items through the filter. + */ + public List filter(List items, LongIntMap feedCounters) { + if (properties.length == 0) { + return items; + } + + List result = new ArrayList<>(); + + for (Feed item : items) { + FeedPreferences itemPreferences = item.getPreferences(); + + // If the item does not meet a requirement, skip it. + if (showAutoDownloadEnabled && !itemPreferences.getAutoDownload()) { + continue; + } else if (showAutoDownloadDisabled && itemPreferences.getAutoDownload()) { + continue; + } + + if (showUpdatedEnabled && !itemPreferences.getKeepUpdated()) { + continue; + } else if (showUpdatedDisabled && itemPreferences.getKeepUpdated()) { + continue; + } + + // If the item reaches here, it meets all criteria (except counter > 0) + result.add(item); + } + + if (showIfCounterGreaterZero) { + for (int i = result.size() - 1; i >= 0; i--) { + if (feedCounters.get(result.get(i).getId()) <= 0) { + result.remove(i); + } + } + } + + return result; + } + + public String[] getValues() { + return properties.clone(); + } + + public String serialize() { + return TextUtils.join(divider, getValues()); + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilterGroup.java b/core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilterGroup.java new file mode 100644 index 000000000..7db0456a0 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/SubscriptionsFilterGroup.java @@ -0,0 +1,30 @@ +package de.danoeh.antennapod.core.feed; + +import de.danoeh.antennapod.core.R; + +public enum SubscriptionsFilterGroup { + COUNTER_GREATER_ZERO(new ItemProperties(R.string.subscriptions_counter_greater_zero, "counter_greater_zero")), + AUTO_DOWNLOAD(new ItemProperties(R.string.auto_downloaded, "enabled_auto_download"), + new ItemProperties(R.string.not_auto_downloaded, "disabled_auto_download")), + UPDATED(new ItemProperties(R.string.kept_updated, "enabled_updates"), + new ItemProperties(R.string.not_kept_updated, "disabled_updates")); + + + public final ItemProperties[] values; + + SubscriptionsFilterGroup(ItemProperties... values) { + this.values = values; + } + + public static class ItemProperties { + + public final int displayName; + public final String filterId; + + public ItemProperties(int displayName, String filterId) { + this.displayName = displayName; + this.filterId = filterId; + } + + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 5700bb9a0..1dc4b6093 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -32,6 +32,7 @@ import java.util.concurrent.TimeUnit; import de.danoeh.antennapod.core.R; import de.danoeh.antennapod.core.feed.MediaType; +import de.danoeh.antennapod.core.feed.SubscriptionsFilter; import de.danoeh.antennapod.core.service.download.ProxyConfig; import de.danoeh.antennapod.core.storage.APCleanupAlgorithm; import de.danoeh.antennapod.core.storage.APNullCleanupAlgorithm; @@ -66,7 +67,7 @@ public class UserPreferences { private static final String PREF_SHOW_AUTO_DOWNLOAD_REPORT = "prefShowAutoDownloadReport"; public static final String PREF_BACK_BUTTON_BEHAVIOR = "prefBackButtonBehavior"; private static final String PREF_BACK_BUTTON_GO_TO_PAGE = "prefBackButtonGoToPage"; - public static final String PREF_FILTER_FEED = "prefFeedFilter"; + public static final String PREF_FILTER_FEED = "prefSubscriptionsFilter"; public static final String PREF_QUEUE_KEEP_SORTED = "prefQueueKeepSorted"; public static final String PREF_QUEUE_KEEP_SORTED_ORDER = "prefQueueKeepSortedOrder"; @@ -149,8 +150,6 @@ public class UserPreferences { public static final int FEED_COUNTER_SHOW_UNPLAYED = 2; public static final int FEED_COUNTER_SHOW_NONE = 3; public static final int FEED_COUNTER_SHOW_DOWNLOADED = 4; - public static final int FEED_FILTER_NONE = 0; - public static final int FEED_FILTER_COUNTER_ZERO = 1; private static Context context; private static SharedPreferences prefs; @@ -1046,14 +1045,14 @@ public class UserPreferences { .apply(); } - public static int getFeedFilter() { - String value = prefs.getString(PREF_FILTER_FEED, "" + FEED_FILTER_NONE); - return Integer.parseInt(value); + public static SubscriptionsFilter getSubscriptionsFilter() { + String value = prefs.getString(PREF_FILTER_FEED, ""); + return new SubscriptionsFilter(value); } - public static void setFeedFilter(String value) { + public static void setSubscriptionsFilter(SubscriptionsFilter value) { prefs.edit() - .putString(PREF_FILTER_FEED, value) + .putString(PREF_FILTER_FEED, value.serialize()) .apply(); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index 140fde93b..c8368506b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -19,6 +19,7 @@ import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedPreferences; +import de.danoeh.antennapod.core.feed.SubscriptionsFilter; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.util.LongIntMap; @@ -795,6 +796,7 @@ public final class DBReader { Log.d(TAG, "getNavDrawerData() called with: " + ""); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); + List feeds = getFeedList(adapter); long[] feedIds = new long[feeds.size()]; for (int i = 0; i < feeds.size(); i++) { @@ -802,15 +804,8 @@ public final class DBReader { } final LongIntMap feedCounters = adapter.getFeedCounters(feedIds); - int feedFilter = UserPreferences.getFeedFilter(); - if (feedFilter == UserPreferences.FEED_FILTER_COUNTER_ZERO) { - for (int i = feeds.size() - 1; i >= 0; i--) { - if (feedCounters.get(feeds.get(i).getId()) <= 0) { - feedCounters.delete(feeds.get(i).getId()); - feeds.remove(i); - } - } - } + SubscriptionsFilter subscriptionsFilter = UserPreferences.getSubscriptionsFilter(); + feeds = subscriptionsFilter.filter(getFeedList(adapter), feedCounters); Comparator comparator; int feedOrder = UserPreferences.getFeedOrder(); -- cgit v1.2.3