Queue: Keep sorted #1556

This commit is contained in:
damoasda 2019-08-10 21:25:59 +02:00
parent db2661fb3f
commit be676c6faa
11 changed files with 108 additions and 84 deletions

View File

@ -276,12 +276,22 @@ public class QueueFragment extends Fragment {
MenuItemUtils.refreshLockItem(getActivity(), menu);
// Show Lock Item and Sort Item only if queue is sorted manually
boolean sortedManually = UserPreferences.isQueueSortedManually();
// Show Lock Item only if queue is sorted manually
boolean sortedAutomatically = UserPreferences.isQueueSortedAutomatically();
MenuItem lockItem = menu.findItem(R.id.queue_lock);
lockItem.setVisible(sortedManually);
MenuItem sortItem = menu.findItem(R.id.queue_sort);
sortItem.setVisible(sortedManually);
lockItem.setVisible(!sortedAutomatically);
// Set keep sorted checkbox
MenuItem sortedAutomaticallyItem = menu.findItem(R.id.queue_sort_automatically);
sortedAutomaticallyItem.setChecked(sortedAutomatically);
// Hide sort menu items for sort orders that are not supported by automatic sort.
MenuItem sortRandomItem = menu.findItem(R.id.queue_sort_random);
sortRandomItem.setVisible(!sortedAutomatically);
MenuItem sortSmart1Item = menu.findItem(R.id.queue_sort_smart_shuffle_asc);
sortSmart1Item.setVisible(!sortedAutomatically);
MenuItem sortSmart2Item = menu.findItem(R.id.queue_sort_smart_shuffle_desc);
sortSmart2Item.setVisible(!sortedAutomatically);
isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker);
}
@ -331,38 +341,52 @@ public class QueueFragment extends Fragment {
((MainActivity) requireActivity()) .loadChildFragment(
EpisodesApplyActionFragment.newInstance(queue, ACTION_DELETE | ACTION_REMOVE_FROM_QUEUE));
return true;
case R.id.queue_sort_automatically:
boolean sortedAutomaticallyOld = UserPreferences.isQueueSortedAutomatically();
boolean sortedAutomaticallyNew = !sortedAutomaticallyOld;
if (sortedAutomaticallyNew) {
// We have to choose an initially sort order, let's sort by episode date
UserPreferences.QueueSortOrder sortOrder = UserPreferences.QueueSortOrder.DATE_NEW_OLD;
UserPreferences.setQueueSortOrder(sortOrder);
QueueSorter.sort(QueueSorter.queueSortOrder2Rule(sortOrder), true);
} else {
UserPreferences.setQueueSortOrder(UserPreferences.QueueSortOrder.MANUALLY);
}
// Update sort menu items visibility and state
getActivity().invalidateOptionsMenu();
return true;
case R.id.queue_sort_episode_title_asc:
QueueSorter.sort(QueueSorter.Rule.EPISODE_TITLE_ASC, true);
queueSortOrderChanged(QueueSorter.Rule.EPISODE_TITLE_ASC);
return true;
case R.id.queue_sort_episode_title_desc:
QueueSorter.sort(QueueSorter.Rule.EPISODE_TITLE_DESC, true);
queueSortOrderChanged(QueueSorter.Rule.EPISODE_TITLE_DESC);
return true;
case R.id.queue_sort_date_asc:
QueueSorter.sort(QueueSorter.Rule.DATE_ASC, true);
queueSortOrderChanged(QueueSorter.Rule.DATE_ASC);
return true;
case R.id.queue_sort_date_desc:
QueueSorter.sort(QueueSorter.Rule.DATE_DESC, true);
queueSortOrderChanged(QueueSorter.Rule.DATE_DESC);
return true;
case R.id.queue_sort_duration_asc:
QueueSorter.sort(QueueSorter.Rule.DURATION_ASC, true);
queueSortOrderChanged(QueueSorter.Rule.DURATION_ASC);
return true;
case R.id.queue_sort_duration_desc:
QueueSorter.sort(QueueSorter.Rule.DURATION_DESC, true);
queueSortOrderChanged(QueueSorter.Rule.DURATION_DESC);
return true;
case R.id.queue_sort_feed_title_asc:
QueueSorter.sort(QueueSorter.Rule.FEED_TITLE_ASC, true);
queueSortOrderChanged(QueueSorter.Rule.FEED_TITLE_ASC);
return true;
case R.id.queue_sort_feed_title_desc:
QueueSorter.sort(QueueSorter.Rule.FEED_TITLE_DESC, true);
queueSortOrderChanged(QueueSorter.Rule.FEED_TITLE_DESC);
return true;
case R.id.queue_sort_random:
QueueSorter.sort(QueueSorter.Rule.RANDOM, true);
queueSortOrderChanged(QueueSorter.Rule.RANDOM);
return true;
case R.id.queue_sort_smart_shuffle_asc:
QueueSorter.sort(QueueSorter.Rule.SMART_SHUFFLE_ASC, true);
queueSortOrderChanged(QueueSorter.Rule.SMART_SHUFFLE_ASC);
return true;
case R.id.queue_sort_smart_shuffle_desc:
QueueSorter.sort(QueueSorter.Rule.SMART_SHUFFLE_DESC, true);
queueSortOrderChanged(QueueSorter.Rule.SMART_SHUFFLE_DESC);
return true;
default:
return false;
@ -372,6 +396,22 @@ public class QueueFragment extends Fragment {
}
}
/**
* Sorts the queue on user interaction.
* If the queue is sorted automatically, the new sort order is stored in the preferences.
*
* @param rule Sort rule.
*/
private void queueSortOrderChanged(QueueSorter.Rule rule) {
boolean sortedAutomatically = UserPreferences.isQueueSortedAutomatically();
UserPreferences.QueueSortOrder sortOrder = QueueSorter.rule2QueueSortOrder(rule);
// remember sort order to keep queue sorted
if (sortedAutomatically && sortOrder != null) {
UserPreferences.setQueueSortOrder(sortOrder);
}
// Sort queue
QueueSorter.sort(rule, true);
}
@Override
public boolean onContextItemSelected(MenuItem item) {

View File

@ -11,11 +11,7 @@ import android.widget.ListView;
import android.widget.Toast;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.Permutor;
import de.danoeh.antennapod.core.util.QueueSorter;
import org.apache.commons.lang3.ArrayUtils;
@ -94,17 +90,6 @@ public class UserInterfacePreferencesFragment extends PreferenceFragmentCompat {
if (Build.VERSION.SDK_INT >= 26) {
findPreference(UserPreferences.PREF_EXPANDED_NOTIFICATION).setVisible(false);
}
findPreference(UserPreferences.PREF_QUEUE_SORT_ORDER)
.setOnPreferenceChangeListener((preference, newValue) -> {
UserPreferences.QueueSortOrder newSortOrder = UserPreferences.parseQueueSortOrder((String) newValue);
if (newSortOrder != UserPreferences.QueueSortOrder.MANUALLY) {
QueueSorter.Rule sortRule = QueueSorter.queueSortOrder2Rule(newSortOrder);
Permutor<FeedItem> permutor = QueueSorter.getPermutor(sortRule);
DBWriter.reorderQueue(permutor, true);
}
return true;
});
}
private void showDrawerPreferencesDialog() {

View File

@ -15,7 +15,6 @@ import de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction.Action;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.util.FeedItemUtil;
import de.danoeh.antennapod.core.util.IntentUtils;
@ -68,17 +67,17 @@ public class FeedItemMenuHandler {
}
boolean hasMedia = selectedItem.getMedia() != null;
boolean isPlaying = hasMedia && selectedItem.getState() == FeedItem.State.PLAYING;
boolean sortedManually = UserPreferences.isQueueSortedManually();
boolean sortedAutomatically = UserPreferences.isQueueSortedAutomatically();
if (!isPlaying) {
mi.setItemVisibility(R.id.skip_episode_item, false);
}
boolean isInQueue = selectedItem.isTagged(FeedItem.TAG_QUEUE);
if (queueAccess == null || queueAccess.size() == 0 || queueAccess.get(0) == selectedItem.getId() || !sortedManually) {
if (queueAccess == null || queueAccess.size() == 0 || queueAccess.get(0) == selectedItem.getId() || sortedAutomatically) {
mi.setItemVisibility(R.id.move_to_top_item, false);
}
if (queueAccess == null || queueAccess.size() == 0 || queueAccess.get(queueAccess.size()-1) == selectedItem.getId() || !sortedManually) {
if (queueAccess == null || queueAccess.size() == 0 || queueAccess.get(queueAccess.size()-1) == selectedItem.getId() || sortedAutomatically) {
mi.setItemVisibility(R.id.move_to_bottom_item, false);
}
if (!isInQueue) {

View File

@ -28,6 +28,11 @@
android:title="@string/sort">
<menu>
<item
android:id="@+id/queue_sort_automatically"
android:title="@string/sort_automatically"
android:checkable="true" />
<item
android:id="@+id/queue_sort_date"
android:title="@string/date">

View File

@ -32,14 +32,6 @@
android:summary="@string/pref_nav_drawer_feed_counter_sum"
android:defaultValue="0"
app:useStockLayout="true"/>
<ListPreference
android:entryValues="@array/nav_queue_sort_values"
android:entries="@array/nav_queue_sort_options"
android:title="@string/pref_queue_sort_title"
android:key="prefQueueSortOrder"
android:summary="@string/pref_queue_sort_sum"
android:defaultValue="MANUALLY"
app:useStockLayout="true"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/external_elements">
<SwitchPreference

View File

@ -493,7 +493,7 @@ public class UserPreferences {
public static boolean isQueueLocked() {
return prefs.getBoolean(PREF_QUEUE_LOCKED, false)
|| !isQueueSortedManually();
|| isQueueSortedAutomatically();
}
public static void setFastForwardSecs(int secs) {
@ -883,6 +883,12 @@ public class UserPreferences {
return parseQueueSortOrder(sortOrderStr);
}
public static void setQueueSortOrder(QueueSortOrder queueSortOrder) {
prefs.edit()
.putString(PREF_QUEUE_SORT_ORDER, queueSortOrder.name())
.apply();
}
public static QueueSortOrder parseQueueSortOrder(String value) {
try {
return QueueSortOrder.valueOf(value);
@ -892,8 +898,8 @@ public class UserPreferences {
}
}
public static boolean isQueueSortedManually() {
public static boolean isQueueSortedAutomatically() {
QueueSortOrder sortedOrder = getQueueSortOrder();
return sortedOrder == QueueSortOrder.MANUALLY;
return sortedOrder != QueueSortOrder.MANUALLY;
}
}

View File

@ -410,7 +410,7 @@ public class DBWriter {
* @param events Replaces the events by a single SORT event if the list has to be sorted automatically.
*/
private static void applySortOrder(List<FeedItem> queue, List<QueueEvent> events) {
if (UserPreferences.isQueueSortedManually()) {
if (!UserPreferences.isQueueSortedAutomatically()) {
// automatic sort order is disabled, don't change anything
return;
}

View File

@ -149,6 +149,38 @@ public class QueueSorter {
}
}
/**
* Converts a Rule value to its corresponding QueueSortOrder value.
*
* @param rule Rule value.
* @return QueueSortOrder value corresponding to the given Rule value. <code>null</code> if the Rule value is unsupported or <code>null</code>.
*/
public static UserPreferences.QueueSortOrder rule2QueueSortOrder(Rule rule) {
if (rule == null) {
return null;
}
switch (rule) {
case EPISODE_TITLE_ASC:
return UserPreferences.QueueSortOrder.EPISODE_TITLE_A_Z;
case EPISODE_TITLE_DESC:
return UserPreferences.QueueSortOrder.EPISODE_TITLE_Z_A;
case DATE_ASC:
return UserPreferences.QueueSortOrder.DATE_OLD_NEW;
case DATE_DESC:
return UserPreferences.QueueSortOrder.DATE_NEW_OLD;
case DURATION_ASC:
return UserPreferences.QueueSortOrder.DURATION_SHORT_LONG;
case DURATION_DESC:
return UserPreferences.QueueSortOrder.DURATION_LONG_SHORT;
case FEED_TITLE_ASC:
return UserPreferences.QueueSortOrder.FEED_TITLE_A_Z;
case FEED_TITLE_DESC:
return UserPreferences.QueueSortOrder.FEED_TITLE_Z_A;
default:
return null;
}
}
/**
* Implements a reordering by pubdate that avoids consecutive episodes from the same feed in
* the queue.

View File

@ -276,6 +276,7 @@
<string name="move_to_top_label">Zum Anfang verschieben</string>
<string name="move_to_bottom_label">Zum Ende verschieben</string>
<string name="sort">Sortieren</string>
<string name="sort_automatically">Automatisch sortieren</string>
<string name="date">Datum</string>
<string name="duration">Dauer</string>
<string name="episode_title">Episodentitel</string>
@ -416,8 +417,6 @@
<string name="pref_nav_drawer_feed_order_sum">Ändere die Reihenfolge deiner Abonnements</string>
<string name="pref_nav_drawer_feed_counter_title">Abonnement-Zähler einstellen</string>
<string name="pref_nav_drawer_feed_counter_sum">Ändere die durch den Abonnementszähler angezeigten Informationen. Betrifft auch die Sortierung der Abonnements wenn \"Reihenfolge der Abonnements\" auf \"Zähler\" gesetzt ist.</string>
<string name="pref_queue_sort_title">Sortierung der Abspielliste einstellen</string>
<string name="pref_queue_sort_sum">Ändere die Sortierreihenfolge der Episoden in der Abspielliste.</string>
<string name="pref_set_theme_sum">Ändere das Aussehen von AntennaPod.</string>
<string name="pref_automatic_download_title">Automatisches Herunterladen</string>
<string name="pref_automatic_download_sum">Konfiguriere das automatische Herunterladen von Episoden.</string>
@ -690,11 +689,6 @@
<string name="sort_date_old_new">Datum (alt \u2192 neu)</string>
<string name="sort_duration_short_long">Dauer (kurz \u2192 lang)</string>
<string name="sort_duration_long_short">Dauer (lang \u2192 kurz)</string>
<string name="sort_episode_title_a_z">Episodentitel (A \u2192 Z)</string>
<string name="sort_episode_title_z_a">Episodentitel (Z \u2192 A)</string>
<string name="sort_feed_title_a_z">Podcasttitel (A \u2192 Z)</string>
<string name="sort_feed_title_z_a">Podcasttitel (Z \u2192 A)</string>
<string name="sort_manually">Manuell</string>
<!--Rating dialog-->
<string name="rating_title">Gefällt dir AntennaPod?</string>
<string name="rating_message">Wir würden uns freuen, wenn du dir kurz die Zeit nimmst, AntennaPod zu bewerten.</string>

View File

@ -187,29 +187,6 @@
<item>3</item>
</string-array>
<string-array name="nav_queue_sort_options">
<item>@string/sort_manually</item>
<item>@string/sort_date_new_old</item>
<item>@string/sort_date_old_new</item>
<item>@string/sort_duration_short_long</item>
<item>@string/sort_duration_long_short</item>
<item>@string/sort_episode_title_a_z</item>
<item>@string/sort_episode_title_z_a</item>
<item>@string/sort_feed_title_a_z</item>
<item>@string/sort_feed_title_z_a</item>
</string-array>
<string-array name="nav_queue_sort_values">
<item>MANUALLY</item>
<item>DATE_NEW_OLD</item>
<item>DATE_OLD_NEW</item>
<item>DURATION_SHORT_LONG</item>
<item>DURATION_LONG_SHORT</item>
<item>EPISODE_TITLE_A_Z</item>
<item>EPISODE_TITLE_Z_A</item>
<item>FEED_TITLE_A_Z</item>
<item>FEED_TITLE_Z_A</item>
</string-array>
<string-array name="nav_drawer_feed_counter_options">
<item>@string/drawer_feed_counter_new_unplayed</item>
<item>@string/drawer_feed_counter_new</item>

View File

@ -294,6 +294,7 @@
<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="sort_automatically">Keep sorted</string>
<string name="date">Date</string>
<string name="duration">Duration</string>
<string name="episode_title">Episode title</string>
@ -401,8 +402,6 @@
<string name="pref_nav_drawer_feed_order_sum">Change the order of your subscriptions</string>
<string name="pref_nav_drawer_feed_counter_title">Set Subscription Counter</string>
<string name="pref_nav_drawer_feed_counter_sum">Change the information displayed by the subscription counter. Also affects the sorting of subscriptions if \'Subscription Order\' is set to \'Counter\'.</string>
<string name="pref_queue_sort_title">Set Queue Sort Order</string>
<string name="pref_queue_sort_sum">Change the sort order of the episodes in the queue.</string>
<string name="pref_set_theme_sum">Change the appearance of AntennaPod.</string>
<string name="pref_automatic_download_title">Automatic Download</string>
<string name="pref_automatic_download_sum">Configure the automatic download of episodes.</string>
@ -697,11 +696,6 @@
<string name="sort_date_old_new">Date (Old \u2192 New)</string>
<string name="sort_duration_short_long">Duration (Short \u2192 Long)</string>
<string name="sort_duration_long_short">Duration (Long \u2192 Short)</string>
<string name="sort_episode_title_a_z">Episode title (A \u2192 Z)</string>
<string name="sort_episode_title_z_a">Episode title (Z \u2192 A)</string>
<string name="sort_feed_title_a_z">Podcast title (A \u2192 Z)</string>
<string name="sort_feed_title_z_a">Podcast title (Z \u2192 A)</string>
<string name="sort_manually">Manually</string>
<!-- Rating dialog -->
<string name="rating_title">Like AntennaPod?</string>