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:
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();

View File

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

View File

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

View File

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

View File

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