Use positive/reverse filtering

This commit is contained in:
recalculated 2016-01-23 20:16:53 -06:00
parent 7e9e3bb946
commit 9ef3c99899
5 changed files with 63 additions and 50 deletions

View File

@ -68,8 +68,8 @@ public class FeedMenuHandler {
case R.id.refresh_complete_item: case R.id.refresh_complete_item:
DBTasks.refreshCompleteFeed(context, selectedFeed); DBTasks.refreshCompleteFeed(context, selectedFeed);
break; break;
case R.id.hide_items: case R.id.filter_items:
showHideDialog(context, selectedFeed); showFilterDialog(context, selectedFeed);
break; break;
case R.id.mark_all_read_item: case R.id.mark_all_read_item:
ConfirmationDialog conDialog = new ConfirmationDialog(context, ConfirmationDialog conDialog = new ConfirmationDialog(context,
@ -110,14 +110,13 @@ public class FeedMenuHandler {
return true; return true;
} }
private static void showHideDialog(final Context context, final Feed feed) { private static void showFilterDialog(final Context context, final Feed feed) {
final String[] items = context.getResources().getStringArray(R.array.episode_filter_options);
final String[] items = context.getResources().getStringArray(R.array.episode_hide_options); final String[] values = context.getResources().getStringArray(R.array.episode_filter_values);
final String[] values = context.getResources().getStringArray(R.array.episode_hide_values);
final boolean[] checkedItems = new boolean[items.length]; final boolean[] checkedItems = new boolean[items.length];
final Set<String> hidden = new HashSet<String>(Arrays.asList(feed.getItemFilter().getValues())); final Set<String> filter = new HashSet<>(Arrays.asList(feed.getItemFilter().getValues()));
Iterator<String> it = hidden.iterator(); Iterator<String> it = filter.iterator();
while(it.hasNext()) { while(it.hasNext()) {
// make sure we have no empty strings in the filter list // make sure we have no empty strings in the filter list
if(TextUtils.isEmpty(it.next())) { if(TextUtils.isEmpty(it.next())) {
@ -126,23 +125,23 @@ public class FeedMenuHandler {
} }
for(int i=0; i < values.length; i++) { for(int i=0; i < values.length; i++) {
String value = values[i]; String value = values[i];
if(hidden.contains(value)) { if(filter.contains(value)) {
checkedItems[i] = true; checkedItems[i] = true;
} }
} }
AlertDialog.Builder builder = new AlertDialog.Builder(context); 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) -> { builder.setMultiChoiceItems(items, checkedItems, (dialog, which, isChecked) -> {
if (isChecked) { if (isChecked) {
hidden.add(values[which]); filter.add(values[which]);
} else { } else {
hidden.remove(values[which]); filter.remove(values[which]);
} }
}); });
builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
feed.setHiddenItemProperties(hidden.toArray(new String[hidden.size()])); feed.setItemFilter(filter.toArray(new String[filter.size()]));
DBWriter.setFeedItemsFilter(feed.getId(), hidden); DBWriter.setFeedItemsFilter(feed.getId(), filter);
}); });
builder.setNegativeButton(R.string.cancel_label, null); builder.setNegativeButton(R.string.cancel_label, null);
builder.create().show(); builder.create().show();

View File

@ -3,10 +3,10 @@
xmlns:custom="http://schemas.android.com/apk/res-auto"> xmlns:custom="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/hide_items" android:id="@+id/filter_items"
android:icon="?attr/ic_filter" android:icon="?attr/ic_filter"
android:menuCategory="container" android:menuCategory="container"
android:title="@string/hide_episodes_title" android:title="@string/filter"
custom:showAsAction="always"> custom:showAsAction="always">
</item> </item>
<item <item

View File

@ -544,7 +544,7 @@ public class Feed extends FeedFile implements FlattrThing, ImageResource {
return itemfilter; return itemfilter;
} }
public void setHiddenItemProperties(String[] properties) { public void setItemFilter(String[] properties) {
if (properties != null) { if (properties != null) {
this.itemfilter = new FeedItemFilter(properties); this.itemfilter = new FeedItemFilter(properties);
} }

View File

@ -8,73 +8,87 @@ import java.util.List;
import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBReader;
public class FeedItemFilter { public class FeedItemFilter {
private final String[] mProperties;
private final String[] properties; private boolean showPlayed = false;
private boolean showUnplayed = false;
private boolean hideUnplayed = false; private boolean showPaused = false;
private boolean hidePaused = false; private boolean showQueued = false;
private boolean hidePlayed = false; private boolean showNotQueued = false;
private boolean hideQueued = false; private boolean showDownloaded = false;
private boolean hideNotQueued = false; private boolean showNotDownloaded = false;
private boolean hideDownloaded = false;
private boolean hideNotDownloaded = false;
public FeedItemFilter(String properties) { public FeedItemFilter(String properties) {
this(TextUtils.split(properties, ",")); this(TextUtils.split(properties, ","));
} }
public FeedItemFilter(String[] properties) { public FeedItemFilter(String[] properties) {
this.properties = properties; this.mProperties = properties;
for(String property : properties) { for(String property : properties) {
// see R.arrays.feed_filter_values // see R.arrays.feed_filter_values
switch(property) { switch(property) {
case "unplayed": case "unplayed":
hideUnplayed = true; showUnplayed = true;
break; break;
case "paused": case "paused":
hidePaused = true; showPaused = true;
break; break;
case "played": case "played":
hidePlayed = true; showPlayed = true;
break; break;
case "queued": case "queued":
hideQueued = true; showQueued = true;
break; break;
case "not_queued": case "not_queued":
hideNotQueued = true; showNotQueued = true;
break; break;
case "downloaded": case "downloaded":
hideDownloaded = true; showDownloaded = true;
break; break;
case "not_downloaded": case "not_downloaded":
hideNotDownloaded = true; showNotDownloaded = true;
break; break;
} }
} }
} }
/**
* Run a list of feed items through the filter.
*/
public List<FeedItem> filter(List<FeedItem> items) { public List<FeedItem> filter(List<FeedItem> items) {
if(properties.length == 0) { if(mProperties.length == 0) return items;
return items;
} List<FeedItem> result = new ArrayList<>();
List<FeedItem> result = new ArrayList<FeedItem>();
// 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) { for(FeedItem item : items) {
if(hideUnplayed && false == item.isPlayed()) continue; // If the item does not meet a requirement, skip it.
if(hidePaused && item.getState() == FeedItem.State.IN_PROGRESS) continue; if (showPlayed && !item.isPlayed()) continue;
if(hidePlayed && item.isPlayed()) continue; if (showUnplayed && item.isPlayed()) continue;
boolean isQueued = DBReader.getQueueIDList().contains(item.getId()); if (showPaused && item.getState() != FeedItem.State.IN_PROGRESS) continue;
if(hideQueued && isQueued) continue;
if(hideNotQueued && false == isQueued) continue; boolean queued = DBReader.getQueueIDList().contains(item.getId());
boolean isDownloaded = item.getMedia() != null && item.getMedia().isDownloaded(); if (showQueued && !queued) continue;
if(hideDownloaded && isDownloaded) continue; if (showNotQueued && queued) continue;
if(hideNotDownloaded && false == isDownloaded) 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); result.add(item);
} }
return result; return result;
} }
public String[] getValues() { public String[] getValues() {
return properties.clone(); return mProperties.clone();
} }
} }

View File

@ -175,7 +175,7 @@
<item>3</item> <item>3</item>
</string-array> </string-array>
<string-array name="episode_hide_options"> <string-array name="episode_filter_options">
<item>@string/hide_unplayed_episodes_label</item> <item>@string/hide_unplayed_episodes_label</item>
<item>@string/hide_paused_episodes_label</item> <item>@string/hide_paused_episodes_label</item>
<item>@string/hide_played_episodes_label</item> <item>@string/hide_played_episodes_label</item>
@ -185,7 +185,7 @@
<item>@string/hide_not_downloaded_episodes_label</item> <item>@string/hide_not_downloaded_episodes_label</item>
</string-array> </string-array>
<string-array name="episode_hide_values"> <string-array name="episode_filter_values">
<item>unplayed</item> <item>unplayed</item>
<item>paused</item> <item>paused</item>
<item>played</item> <item>played</item>