diff options
author | ByteHamster <info@bytehamster.com> | 2021-03-03 18:23:29 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2021-03-03 18:23:29 +0100 |
commit | dd8341a858f560f045f0d90c8c32f27d6a0891d4 (patch) | |
tree | 860aff97bdf0eaac900b8fc27e8af83104cafb27 /core | |
parent | fae32c4fee6e6dd4653abae22b3522f8f0d6174f (diff) | |
download | AntennaPod-dd8341a858f560f045f0d90c8c32f27d6a0891d4.zip |
Replace Java-based feed filtering with SQL-based
Diffstat (limited to 'core')
4 files changed, 33 insertions, 71 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItemFilter.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItemFilter.java index bd30a3953..ac742e765 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItemFilter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedItemFilter.java @@ -1,15 +1,7 @@ package de.danoeh.antennapod.core.feed; import android.text.TextUtils; - -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; - -import de.danoeh.antennapod.core.storage.DBReader; -import de.danoeh.antennapod.core.util.LongList; - -import static de.danoeh.antennapod.core.feed.FeedItem.TAG_FAVORITE; public class FeedItemFilter { @@ -58,53 +50,6 @@ public class FeedItemFilter { return Arrays.asList(properties).contains(property); } - /** - * Run a list of feed items through the filter. - */ - public List<FeedItem> filter(List<FeedItem> items) { - if (properties.length == 0) { - return items; - } - - List<FeedItem> result = new ArrayList<>(); - - // Check for filter combinations that will always return an empty list - // (e.g. requiring played and unplayed at the same time) - if (showPlayed && showUnplayed) return result; - if (showQueued && showNotQueued) return result; - if (showDownloaded && showNotDownloaded) return result; - - final LongList queuedIds = DBReader.getQueueIDList(); - for (FeedItem item : items) { - // If the item does not meet a requirement, skip it. - - if (showPlayed && !item.isPlayed()) continue; - if (showUnplayed && item.isPlayed()) continue; - - if (showPaused && item.getState() != FeedItem.State.IN_PROGRESS) continue; - if (showNotPaused && item.getState() == FeedItem.State.IN_PROGRESS) continue; - - boolean queued = queuedIds.contains(item.getId()); - if (showQueued && !queued) continue; - if (showNotQueued && queued) continue; - - boolean downloaded = item.getMedia() != null && item.getMedia().isDownloaded(); - if (showDownloaded && !downloaded) continue; - if (showNotDownloaded && downloaded) continue; - - if (showHasMedia && !item.hasMedia()) continue; - if (showNoMedia && item.hasMedia()) continue; - - if (showIsFavorite && !item.isTagged(TAG_FAVORITE)) continue; - if (showNotFavorite && item.isTagged(TAG_FAVORITE)) continue; - - // If the item reaches here, it meets all criteria - result.add(item); - } - - return result; - } - public String[] getValues() { return properties.clone(); } 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 fcf61b070..7aa5f8abe 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 @@ -161,11 +161,15 @@ public final class DBReader { * The method does NOT change the items-attribute of the feed. */ public static List<FeedItem> getFeedItemList(final Feed feed) { + return getFeedItemList(feed, FeedItemFilter.unfiltered()); + } + + public static List<FeedItem> getFeedItemList(final Feed feed, final FeedItemFilter filter) { Log.d(TAG, "getFeedItemList() called with: " + "feed = [" + feed + "]"); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try (Cursor cursor = adapter.getAllItemsOfFeedCursor(feed)) { + try (Cursor cursor = adapter.getItemsOfFeedCursor(feed, filter)) { List<FeedItem> items = extractItemlistFromCursor(adapter, cursor); Collections.sort(items, new FeedItemPubdateComparator()); for (FeedItem item : items) { @@ -480,31 +484,41 @@ public final class DBReader { * * @param feedId The ID of the Feed * @return The Feed or null if the Feed could not be found. The Feeds FeedItems will also be loaded from the - * database and the items-attribute will be set correctly. + * database and the items-attribute will be set correctly. */ + @Nullable public static Feed getFeed(final long feedId) { - Log.d(TAG, "getFeed() called with: " + "feedId = [" + feedId + "]"); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - try { - return getFeed(feedId, adapter); - } finally { - adapter.close(); - } + return getFeed(feedId, false); } + /** + * Loads a specific Feed from the database. + * + * @param feedId The ID of the Feed + * @param filtered <code>true</code> if only the visible items should be loaded according to the feed filter. + * @return The Feed or null if the Feed could not be found. The Feeds FeedItems will also be loaded from the + * database and the items-attribute will be set correctly. + */ @Nullable - static Feed getFeed(final long feedId, PodDBAdapter adapter) { + public static Feed getFeed(final long feedId, boolean filtered) { + Log.d(TAG, "getFeed() called with: " + "feedId = [" + feedId + "]"); + PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); Feed feed = null; try (Cursor cursor = adapter.getFeedCursor(feedId)) { if (cursor.moveToNext()) { feed = extractFeedFromCursorRow(cursor); - feed.setItems(getFeedItemList(feed)); + if (filtered) { + feed.setItems(getFeedItemList(feed, feed.getItemFilter())); + } else { + feed.setItems(getFeedItemList(feed)); + } } else { Log.e(TAG, "getFeed could not find feed with id " + feedId); } return feed; + } finally { + adapter.close(); } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java index 596ab624e..d16432cd6 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBTasks.java @@ -320,7 +320,7 @@ public final class DBTasks { private static Feed searchFeedByIdentifyingValueOrID(PodDBAdapter adapter, Feed feed) { if (feed.getId() != 0) { - return DBReader.getFeed(feed.getId(), adapter); + return DBReader.getFeed(feed.getId()); } else { List<Feed> feeds = DBReader.getFeedList(); for (Feed f : feeds) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index adb5e6a74..445b1945b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -951,9 +951,12 @@ public class PodDBAdapter { * @param feed The feed you want to get the FeedItems from. * @return The cursor of the query */ - public final Cursor getAllItemsOfFeedCursor(final Feed feed) { + public final Cursor getItemsOfFeedCursor(final Feed feed, FeedItemFilter filter) { + String filterQuery = FeedItemFilterQuery.generateFrom(filter); + String whereClauseAnd = "".equals(filterQuery) ? "" : " AND " + filterQuery; final String query = SELECT_FEED_ITEMS_AND_MEDIA - + " WHERE " + TABLE_NAME_FEED_ITEMS + "." + KEY_FEED + "=" + feed.getId(); + + " WHERE " + TABLE_NAME_FEED_ITEMS + "." + KEY_FEED + "=" + feed.getId() + + whereClauseAnd; return db.rawQuery(query, null); } |