diff options
author | Tom Hennen <TomHennen@users.noreply.github.com> | 2016-01-25 11:13:06 -0500 |
---|---|---|
committer | Tom Hennen <TomHennen@users.noreply.github.com> | 2016-01-25 11:13:06 -0500 |
commit | 7f02570f88091ac46e447e7231583fe998cf6358 (patch) | |
tree | 70a3744029336a4ccc7232e4bcc3b8fff36f0dbe | |
parent | 703dde3c7f215406a055a1271e5971e2eb16ab36 (diff) | |
parent | 4329aef26d01704c16bf2e3571538291f0bf3494 (diff) | |
download | AntennaPod-7f02570f88091ac46e447e7231583fe998cf6358.zip |
Merge pull request #1595 from recalculated/positive-filter
Use positive/reverse filtering
6 files changed, 100 insertions, 51 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java index 2d6d4cac1..830e9d419 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java @@ -68,8 +68,8 @@ public class FeedMenuHandler { case R.id.refresh_complete_item: DBTasks.refreshCompleteFeed(context, selectedFeed); break; - case R.id.hide_items: - showHideDialog(context, selectedFeed); + case R.id.filter_items: + showFilterDialog(context, selectedFeed); break; case R.id.mark_all_read_item: ConfirmationDialog conDialog = new ConfirmationDialog(context, @@ -110,14 +110,13 @@ public class FeedMenuHandler { return true; } - private static void showHideDialog(final Context context, final Feed feed) { - - final String[] items = context.getResources().getStringArray(R.array.episode_hide_options); - final String[] values = context.getResources().getStringArray(R.array.episode_hide_values); + private static void showFilterDialog(final Context context, final Feed feed) { + final String[] items = context.getResources().getStringArray(R.array.episode_filter_options); + final String[] values = context.getResources().getStringArray(R.array.episode_filter_values); final boolean[] checkedItems = new boolean[items.length]; - final Set<String> hidden = new HashSet<String>(Arrays.asList(feed.getItemFilter().getValues())); - Iterator<String> it = hidden.iterator(); + final Set<String> filter = new HashSet<>(Arrays.asList(feed.getItemFilter().getValues())); + Iterator<String> it = filter.iterator(); while(it.hasNext()) { // make sure we have no empty strings in the filter list if(TextUtils.isEmpty(it.next())) { @@ -126,23 +125,23 @@ public class FeedMenuHandler { } for(int i=0; i < values.length; i++) { String value = values[i]; - if(hidden.contains(value)) { + if(filter.contains(value)) { checkedItems[i] = true; } } AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.hide_episodes_title); + builder.setTitle(R.string.filter); builder.setMultiChoiceItems(items, checkedItems, (dialog, which, isChecked) -> { if (isChecked) { - hidden.add(values[which]); + filter.add(values[which]); } else { - hidden.remove(values[which]); + filter.remove(values[which]); } }); builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { - feed.setHiddenItemProperties(hidden.toArray(new String[hidden.size()])); - DBWriter.setFeedItemsFilter(feed.getId(), hidden); + feed.setItemFilter(filter.toArray(new String[filter.size()])); + DBWriter.setFeedItemsFilter(feed.getId(), filter); }); builder.setNegativeButton(R.string.cancel_label, null); builder.create().show(); diff --git a/app/src/main/res/menu/feedlist.xml b/app/src/main/res/menu/feedlist.xml index 54b90f5c6..ed03c08d6 100644 --- a/app/src/main/res/menu/feedlist.xml +++ b/app/src/main/res/menu/feedlist.xml @@ -3,10 +3,10 @@ xmlns:custom="http://schemas.android.com/apk/res-auto"> <item - android:id="@+id/hide_items" + android:id="@+id/filter_items" android:icon="?attr/ic_filter" android:menuCategory="container" - android:title="@string/hide_episodes_title" + android:title="@string/filter" custom:showAsAction="always"> </item> <item diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java b/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java index 0de046fe5..4be788f33 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/Feed.java @@ -544,7 +544,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource { return itemfilter; } - public void setHiddenItemProperties(String[] properties) { + public void setItemFilter(String[] properties) { if (properties != null) { this.itemfilter = new FeedItemFilter(properties); } 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 8636ac50a..fdde4b34c 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 @@ -8,73 +8,87 @@ import java.util.List; import de.danoeh.antennapod.core.storage.DBReader; public class FeedItemFilter { + private final String[] mProperties; - private final String[] properties; - - private boolean hideUnplayed = false; - private boolean hidePaused = false; - private boolean hidePlayed = false; - private boolean hideQueued = false; - private boolean hideNotQueued = false; - private boolean hideDownloaded = false; - private boolean hideNotDownloaded = false; + private boolean showPlayed = false; + private boolean showUnplayed = false; + private boolean showPaused = false; + private boolean showQueued = false; + private boolean showNotQueued = false; + private boolean showDownloaded = false; + private boolean showNotDownloaded = false; public FeedItemFilter(String properties) { this(TextUtils.split(properties, ",")); } public FeedItemFilter(String[] properties) { - this.properties = properties; + this.mProperties = properties; for(String property : properties) { // see R.arrays.feed_filter_values switch(property) { case "unplayed": - hideUnplayed = true; + showUnplayed = true; break; case "paused": - hidePaused = true; + showPaused = true; break; case "played": - hidePlayed = true; + showPlayed = true; break; case "queued": - hideQueued = true; + showQueued = true; break; case "not_queued": - hideNotQueued = true; + showNotQueued = true; break; case "downloaded": - hideDownloaded = true; + showDownloaded = true; break; case "not_downloaded": - hideNotDownloaded = true; + showNotDownloaded = true; break; } } } + /** + * 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<FeedItem>(); + if(mProperties.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; + for(FeedItem item : items) { - if(hideUnplayed && false == item.isPlayed()) continue; - if(hidePaused && item.getState() == FeedItem.State.IN_PROGRESS) continue; - if(hidePlayed && item.isPlayed()) continue; - boolean isQueued = DBReader.getQueueIDList().contains(item.getId()); - if(hideQueued && isQueued) continue; - if(hideNotQueued && false == isQueued) continue; - boolean isDownloaded = item.getMedia() != null && item.getMedia().isDownloaded(); - if(hideDownloaded && isDownloaded) continue; - if(hideNotDownloaded && false == isDownloaded) continue; + // 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; + + boolean queued = DBReader.getQueueIDList().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 the item reaches here, it meets all criteria result.add(item); } + return result; } public String[] getValues() { - return properties.clone(); + return mProperties.clone(); } } 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 915ca14c7..63b563525 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 @@ -1501,7 +1501,7 @@ public class PodDBAdapter { */ private static class PodDBHelper extends SQLiteOpenHelper { - private final static int VERSION = 1040013; + private final static int VERSION = 1050003; private Context context; @@ -1745,6 +1745,42 @@ public class PodDBAdapter { db.execSQL(PodDBAdapter.CREATE_INDEX_FEEDITEMS_PUBDATE); db.execSQL(PodDBAdapter.CREATE_INDEX_FEEDITEMS_READ); } + if (oldVersion < 1050003) { + // Migrates feed list filter data + + db.beginTransaction(); + + // Change to intermediate values to avoid overwriting in the following find/replace + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'unplayed', 'noplay')"); + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'not_queued', 'noqueue')"); + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'not_downloaded', 'nodl')"); + + // Replace played, queued, and downloaded with their opposites + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'played', 'unplayed')"); + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'queued', 'not_queued')"); + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'downloaded', 'not_downloaded')"); + + // Now replace intermediates for unplayed, not queued, etc. with their opposites + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'noplay', 'played')"); + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'noqueue', 'queued')"); + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'nodl', 'downloaded')"); + + // Paused doesn't have an opposite, so unplayed is the next best option + db.execSQL("UPDATE " + TABLE_NAME_FEEDS + "\n" + + "SET " + KEY_HIDE + " = replace(" + KEY_HIDE + ", 'paused', 'unplayed')"); + + db.setTransactionSuccessful(); + db.endTransaction(); + } EventBus.getDefault().post(ProgressEvent.end()); } diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index 55da0ee04..0de515292 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -175,7 +175,7 @@ <item>3</item> </string-array> - <string-array name="episode_hide_options"> + <string-array name="episode_filter_options"> <item>@string/hide_unplayed_episodes_label</item> <item>@string/hide_paused_episodes_label</item> <item>@string/hide_played_episodes_label</item> @@ -185,7 +185,7 @@ <item>@string/hide_not_downloaded_episodes_label</item> </string-array> - <string-array name="episode_hide_values"> + <string-array name="episode_filter_values"> <item>unplayed</item> <item>paused</item> <item>played</item> |