Options to sort queue refactor, add sort by alpha and duration

This commit is contained in:
drabux 2015-01-24 13:17:19 -05:00
parent 520ed8327c
commit 9764b80935
5 changed files with 156 additions and 30 deletions

View File

@ -40,6 +40,7 @@ import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.QueueSorter;
import de.danoeh.antennapod.menuhandler.MenuItemUtils;
import de.danoeh.antennapod.menuhandler.NavDrawerActivity;
@ -174,11 +175,23 @@ public class QueueFragment extends Fragment {
DBTasks.refreshAllFeeds(getActivity(), feeds);
}
return true;
case R.id.queue_sort_alpha_asc:
QueueSorter.sort(getActivity(), QueueSorter.Rule.ALPHA_ASC, true);
return true;
case R.id.queue_sort_alpha_desc:
QueueSorter.sort(getActivity(), QueueSorter.Rule.ALPHA_DESC, true);
return true;
case R.id.queue_sort_date_asc:
DBWriter.sortQueueItemByDate(getActivity(), true, true);
QueueSorter.sort(getActivity(), QueueSorter.Rule.DATE_ASC, true);
return true;
case R.id.queue_sort_date_desc:
DBWriter.sortQueueItemByDate(getActivity(), false, true);
QueueSorter.sort(getActivity(), QueueSorter.Rule.DATE_DESC, true);
return true;
case R.id.queue_sort_duration_asc:
QueueSorter.sort(getActivity(), QueueSorter.Rule.DURATION_ASC, true);
return true;
case R.id.queue_sort_duration_desc:
QueueSorter.sort(getActivity(), QueueSorter.Rule.DURATION_DESC, true);
return true;
default:
return false;

View File

@ -15,13 +15,49 @@
android:title="@string/sort">
<menu>
<item
android:id="@+id/queue_sort_date_asc"
android:title="@string/date_asc"/>
<item
android:id="@+id/queue_sort_date_desc"
android:title="@string/date_desc"/>
<item
android:id="@+id/queue_sort_alpha"
android:title="@string/alpha">
<menu>
<item
android:id="@+id/queue_sort_alpha_asc"
android:title="@string/ascending"/>
<item
android:id="@+id/queue_sort_alpha_desc"
android:title="@string/descending"/>
</menu>
</item>
<item
android:id="@+id/queue_sort_date"
android:title="@string/date">
<menu>
<item
android:id="@+id/queue_sort_date_asc"
android:title="@string/ascending"/>
<item
android:id="@+id/queue_sort_date_desc"
android:title="@string/descending"/>
</menu>
</item>
<item
android:id="@+id/queue_sort_duration"
android:title="@string/duration">
<menu>
<item
android:id="@+id/queue_sort_duration_asc"
android:title="@string/ascending"/>
<item
android:id="@+id/queue_sort_duration_desc"
android:title="@string/descending"/>
</menu>
</item>
</menu>
</item>
</item>
</menu>

View File

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

View File

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

View File

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