diff options
author | drabux <drabux@fouragan.net> | 2015-01-24 13:17:19 -0500 |
---|---|---|
committer | drabux <drabux@fouragan.net> | 2015-01-24 13:18:29 -0500 |
commit | 9764b8093544857261bb5abf99c1ab9dbfcda65a (patch) | |
tree | 39a5c276e32f48f9d687a7430c16f627a8e38b58 /core/src/main | |
parent | 520ed8327c5d04134619f47eebf3c81615665f13 (diff) | |
download | AntennaPod-9764b8093544857261bb5abf99c1ab9dbfcda65a.zip |
Options to sort queue refactor, add sort by alpha and duration
Diffstat (limited to 'core/src/main')
3 files changed, 98 insertions, 21 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index 41dde8656..07c3c78e6 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -595,39 +595,24 @@ public class DBWriter { * This function must be run using the ExecutorService (dbExec). * * @param context A context that is used for opening a database connection. - * @param asc true sort by ascending order - * false sort by descending order + * @param comparator FeedItem comparator * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to * false if the caller wants to avoid unexpected updates of the GUI. */ - public static void sortQueueItemByDate(final Context context, final boolean asc, final boolean broadcastUpdate) { + public static void sort (final Context context, Comparator<FeedItem> comparator, final boolean broadcastUpdate) { final PodDBAdapter adapter = new PodDBAdapter(context); adapter.open(); final List<FeedItem> queue = DBReader.getQueue(context, adapter); if (queue != null) { - if (asc) { - Collections.sort(queue, new Comparator<FeedItem>() { - public int compare(FeedItem f1, FeedItem f2) { - return f1.getPubDate().compareTo(f2.getPubDate()); - } - }); - } else { - Collections.sort(queue, new Comparator<FeedItem>() { - public int compare(FeedItem f1, FeedItem f2) { - return f2.getPubDate().compareTo(f1.getPubDate()); - } - }); - } - + Collections.sort(queue, comparator); adapter.setQueue(queue); if (broadcastUpdate) { EventDistributor.getInstance() .sendQueueUpdateBroadcast(); } - } else { - Log.e(TAG, "sortQueueItemByDate: Could not load queue"); + Log.e(TAG, "sort: Could not load queue"); } adapter.close(); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java b/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java new file mode 100644 index 000000000..b6ea8c977 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java @@ -0,0 +1,89 @@ +package de.danoeh.antennapod.core.util; + +import android.content.Context; + +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.storage.DBWriter; + +import java.util.Comparator; + +/** + * Provides method for sorting the queue according to rules. + */ +public class QueueSorter { + public enum Rule { + ALPHA_ASC, + ALPHA_DESC, + DATE_ASC, + DATE_DESC, + DURATION_ASC, + DURATION_DESC + } + + public static void sort(final Context context, final Rule rule, final boolean broadcastUpdate) { + Comparator<FeedItem> comparator = null; + + switch (rule) { + case ALPHA_ASC: + comparator = new Comparator<FeedItem>() { + public int compare(FeedItem f1, FeedItem f2) { + return f1.getTitle().compareTo(f2.getTitle()); + } + }; + break; + case ALPHA_DESC: + comparator = new Comparator<FeedItem>() { + public int compare(FeedItem f1, FeedItem f2) { + return f2.getTitle().compareTo(f1.getTitle()); + } + }; + break; + case DATE_ASC: + comparator = new Comparator<FeedItem>() { + public int compare(FeedItem f1, FeedItem f2) { + return f1.getPubDate().compareTo(f2.getPubDate()); + } + }; + break; + case DATE_DESC: + comparator = new Comparator<FeedItem>() { + public int compare(FeedItem f1, FeedItem f2) { + return f2.getPubDate().compareTo(f1.getPubDate()); + } + }; + break; + case DURATION_ASC: + comparator = new Comparator<FeedItem>() { + public int compare(FeedItem f1, FeedItem f2) { + FeedMedia f1Media = f1.getMedia(); + FeedMedia f2Media = f2.getMedia(); + int duration1 = f1Media != null ? f1Media.getDuration() : -1; + int duration2 = f2Media != null ? f2Media.getDuration() : -1; + + if (duration1 == -1 || duration2 == -1) + return duration2 - duration1; + else + return duration1 - duration2; + } + }; + break; + case DURATION_DESC: + comparator = new Comparator<FeedItem>() { + public int compare(FeedItem f1, FeedItem f2) { + FeedMedia f1Media = f1.getMedia(); + FeedMedia f2Media = f2.getMedia(); + int duration1 = f1Media != null ? f1Media.getDuration() : -1; + int duration2 = f2Media != null ? f2Media.getDuration() : -1; + + return -1 * (duration1 - duration2); + } + }; + default: + } + + if (comparator != null) { + DBWriter.sort(context, comparator, broadcastUpdate); + } + } +} diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index d76cc533b..b68013674 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -160,8 +160,11 @@ <string name="move_to_top_label">Move to top</string> <string name="move_to_bottom_label">Move to bottom</string> <string name="sort">Sort</string> - <string name="date_asc">ascending date</string> - <string name="date_desc">descending date</string> + <string name="alpha">Alphabetically</string> + <string name="date">Date</string> + <string name="duration">Duration</string> + <string name="ascending">Ascending</string> + <string name="descending">Descending</string> <!-- Flattr --> <string name="flattr_auth_label">Flattr sign-in</string> |