Replace Java-based feed filtering with SQL-based

This commit is contained in:
ByteHamster 2021-03-03 18:23:29 +01:00
parent fae32c4fee
commit dd8341a858
5 changed files with 38 additions and 77 deletions

View File

@ -563,13 +563,12 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
@Nullable
private Feed loadData() {
Feed feed = DBReader.getFeed(feedID);
if (feed != null && feed.getItemFilter() != null) {
DBReader.loadAdditionalFeedItemListData(feed.getItems());
FeedItemFilter filter = feed.getItemFilter();
feed.setItems(filter.filter(feed.getItems()));
Feed feed = DBReader.getFeed(feedID, true);
if (feed == null) {
return null;
}
if (feed != null && feed.getSortOrder() != null) {
DBReader.loadAdditionalFeedItemListData(feed.getItems());
if (feed.getSortOrder() != null) {
List<FeedItem> feedItems = feed.getItems();
FeedItemPermutors.getPermutor(feed.getSortOrder()).reorder(feedItems);
feed.setItems(feedItems);

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -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) {

View File

@ -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);
}