diff options
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java | 4 | ||||
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java | 26 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java | 14 | ||||
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/util/FeedItemPermutors.java (renamed from core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java) | 29 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java (renamed from core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java) | 26 |
5 files changed, 41 insertions, 58 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java index 88f48003f..076320754 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java @@ -60,10 +60,10 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DownloadRequestException; import de.danoeh.antennapod.core.storage.DownloadRequester; +import de.danoeh.antennapod.core.util.FeedItemPermutors; import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.Optional; -import de.danoeh.antennapod.core.util.QueueSorter; import de.danoeh.antennapod.core.util.gui.MoreContentListFooterUtil; import de.danoeh.antennapod.dialog.EpisodesApplyActionFragment; import de.danoeh.antennapod.dialog.RenameFeedDialog; @@ -634,7 +634,7 @@ public class FeedItemlistFragment extends ListFragment { } if (feed != null && feed.getSortOrder() != null) { List<FeedItem> feedItems = feed.getItems(); - QueueSorter.getPermutor(feed.getSortOrder()).reorder(feedItems); + FeedItemPermutors.getPermutor(feed.getSortOrder()).reorder(feedItems); feed.setItems(feedItems); } return Optional.ofNullable(feed); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index 7c1bcf034..ac381bec5 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -4,15 +4,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; -import com.google.android.material.snackbar.Snackbar; -import androidx.fragment.app.Fragment; -import androidx.core.view.MenuItemCompat; -import androidx.appcompat.app.AlertDialog; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.appcompat.widget.SearchView; -import androidx.recyclerview.widget.SimpleItemAnimator; -import androidx.recyclerview.widget.ItemTouchHelper; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -24,9 +15,18 @@ import android.widget.CheckBox; import android.widget.ProgressBar; import android.widget.TextView; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.widget.SearchView; +import androidx.core.view.MenuItemCompat; +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.ItemTouchHelper; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.SimpleItemAnimator; + +import com.google.android.material.snackbar.Snackbar; import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration; -import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -40,6 +40,7 @@ import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.event.DownloadEvent; import de.danoeh.antennapod.core.event.DownloaderUpdate; import de.danoeh.antennapod.core.event.FeedItemEvent; +import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import de.danoeh.antennapod.core.event.QueueEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.FeedItem; @@ -54,7 +55,6 @@ import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.Converter; import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.LongList; -import de.danoeh.antennapod.core.util.QueueSorter; import de.danoeh.antennapod.core.util.SortOrder; import de.danoeh.antennapod.core.util.download.AutoUpdateManager; import de.danoeh.antennapod.dialog.EpisodesApplyActionFragment; @@ -373,7 +373,7 @@ public class QueueFragment extends Fragment { UserPreferences.setQueueKeepSorted(keepSortedNew); if (keepSortedNew) { SortOrder sortOrder = UserPreferences.getQueueKeepSortedOrder(); - QueueSorter.sort(sortOrder, true); + DBWriter.reorderQueue(sortOrder, true); if (recyclerAdapter != null) { recyclerAdapter.setLocked(true); } @@ -439,7 +439,7 @@ public class QueueFragment extends Fragment { */ private void setSortOrder(SortOrder sortOrder) { UserPreferences.setQueueKeepSortedOrder(sortOrder); - QueueSorter.sort(sortOrder, true); + DBWriter.reorderQueue(sortOrder, true); } @Override 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 912c67da3..9fe87b5d7 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 @@ -37,10 +37,10 @@ import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.playback.PlaybackService; +import de.danoeh.antennapod.core.util.FeedItemPermutors; import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.core.util.Permutor; -import de.danoeh.antennapod.core.util.QueueSorter; import de.danoeh.antennapod.core.util.SortOrder; /** @@ -386,7 +386,7 @@ public class DBWriter { // do not shuffle the list on every change return; } - Permutor<FeedItem> permutor = QueueSorter.getPermutor(sortOrder); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(sortOrder); permutor.reorder(queue); // Replace ADDED events by a single SORTED event @@ -846,14 +846,18 @@ public class DBWriter { } /** - * Sort the FeedItems in the queue with the given Permutor. + * Sort the FeedItems in the queue with the given the named sort order. * - * @param permutor Encapsulates whole-Queue reordering logic. * @param broadcastUpdate <code>true</code> if this operation should trigger a * QueueUpdateBroadcast. This option should be set to <code>false</code> * if the caller wants to avoid unexpected updates of the GUI. */ - public static Future<?> reorderQueue(final Permutor<FeedItem> permutor, final boolean broadcastUpdate) { + public static Future<?> reorderQueue(@Nullable SortOrder sortOrder, final boolean broadcastUpdate) { + if (sortOrder == null) { + Log.w(TAG, "reorderQueue() - sortOrder is null. Do nothing."); + return dbExec.submit(() -> { }); + } + final Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(sortOrder); return dbExec.submit(() -> { final PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java b/core/src/main/java/de/danoeh/antennapod/core/util/FeedItemPermutors.java index 0c21ca393..2d1d6e658 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/QueueSorter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/FeedItemPermutors.java @@ -1,5 +1,7 @@ package de.danoeh.antennapod.core.util; +import androidx.annotation.NonNull; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -9,36 +11,19 @@ import java.util.Map; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.storage.DBWriter; /** - * Provides method for sorting the queue according to rules. + * Provides method for sorting the a list of {@link FeedItem} according to rules. */ -public class QueueSorter { - - /** - * Sorts the queue by the given sort order and sends a broadcast update. - * - * @param sortOrder Sort order. - * @param broadcastUpdate Send broadcast update? - */ - public static void sort(SortOrder sortOrder, boolean broadcastUpdate) { - Permutor<FeedItem> permutor = getPermutor(sortOrder); - if (permutor != null) { - DBWriter.reorderQueue(permutor, broadcastUpdate); - } - } +public class FeedItemPermutors { /** * Returns a Permutor that sorts a list appropriate to the given sort order. * - * @param sortOrder Sort order. - * @return Permutor that sorts a list appropriate to the given sort order. <code>null</code> if the order is unknown or <code>null</code>. + * @return Permutor that sorts a list appropriate to the given sort order. */ - public static Permutor<FeedItem> getPermutor(SortOrder sortOrder) { - if (sortOrder == null) { - return null; - } + @NonNull + public static Permutor<FeedItem> getPermutor(@NonNull SortOrder sortOrder) { Comparator<FeedItem> comparator = null; Permutor<FeedItem> permutor = null; diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java index 791b6a75b..82b3a794e 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java @@ -11,21 +11,15 @@ import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertNull; /** - * Test class for QueueSorter. + * Test class for FeedItemPermutors. */ -public class QueueSorterTest { - - @Test - public void testPermutorForRule_null() { - assertNull(QueueSorter.getPermutor(null)); - } +public class FeedItemPermutorsTest { @Test public void testPermutorForRule_EPISODE_TITLE_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.EPISODE_TITLE_A_Z); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -35,7 +29,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_EPISODE_TITLE_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.EPISODE_TITLE_Z_A); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_Z_A); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -45,7 +39,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DATE_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DATE_OLD_NEW); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -55,7 +49,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DATE_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DATE_NEW_OLD); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_NEW_OLD); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -65,7 +59,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DURATION_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DURATION_SHORT_LONG); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_SHORT_LONG); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -75,7 +69,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DURATION_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DURATION_LONG_SHORT); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -85,7 +79,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_FEED_TITLE_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.FEED_TITLE_A_Z); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_A_Z); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -95,7 +89,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_FEED_TITLE_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.FEED_TITLE_Z_A); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting |