From 922395a4489c8a9387319bcaf515dea58c4914e4 Mon Sep 17 00:00:00 2001 From: Rahmat Ramadhan Date: Thu, 28 Sep 2023 02:59:58 +0700 Subject: [PATCH] Sort downloads by size (#6659) --- .../fragment/CompletedDownloadsFragment.java | 1 + .../MenuItemToSortOrderConverter.java | 4 +++ app/src/main/res/menu/sort_menu.xml | 16 ++++++++++++ .../core/util/FeedItemPermutors.java | 10 +++++++ .../core/util/FeedItemPermutorsTest.java | 26 ++++++++++++++++--- .../antennapod/model/feed/SortOrder.java | 2 ++ .../database/mapper/FeedItemSortQuery.java | 6 +++++ ui/i18n/src/main/res/values/strings.xml | 3 +++ 8 files changed, 65 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java index ba328adba..730a39189 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/CompletedDownloadsFragment.java @@ -159,6 +159,7 @@ public class CompletedDownloadsFragment extends Fragment menu.findItem(R.id.sort_random).setVisible(false); menu.findItem(R.id.sort_smart_shuffle).setVisible(false); menu.findItem(R.id.keep_sorted).setVisible(false); + menu.findItem(R.id.sort_size).setVisible(true); } @Override diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/MenuItemToSortOrderConverter.java b/app/src/main/java/de/danoeh/antennapod/fragment/MenuItemToSortOrderConverter.java index 51911b122..d4150fbdb 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/MenuItemToSortOrderConverter.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/MenuItemToSortOrderConverter.java @@ -32,6 +32,10 @@ public class MenuItemToSortOrderConverter { return SortOrder.SMART_SHUFFLE_OLD_NEW; } else if (itemId == R.id.sort_smart_shuffle_desc) { return SortOrder.SMART_SHUFFLE_NEW_OLD; + } else if (itemId == R.id.sort_size_small_large) { + return SortOrder.SIZE_SMALL_LARGE; + } else if (itemId == R.id.sort_size_large_small) { + return SortOrder.SIZE_LARGE_SMALL; } return null; diff --git a/app/src/main/res/menu/sort_menu.xml b/app/src/main/res/menu/sort_menu.xml index f0a22042b..96ad4b9f6 100644 --- a/app/src/main/res/menu/sort_menu.xml +++ b/app/src/main/res/menu/sort_menu.xml @@ -75,6 +75,22 @@ android:title="@string/sort_new_old"/> + + + + + + + + + smartShuffle(queue, false); break; + case SIZE_SMALL_LARGE: + comparator = (f1, f2) -> Long.compare(size(f1), size(f2)); + break; + case SIZE_LARGE_SMALL: + comparator = (f1, f2) -> Long.compare(size(f2), size(f1)); + break; } if (comparator != null) { @@ -97,6 +103,10 @@ public class FeedItemPermutors { return (item != null && item.getMedia() != null) ? item.getMedia().getDuration() : 0; } + private static long size(@Nullable FeedItem item) { + return (item != null && item.getMedia() != null) ? item.getMedia().getSize() : 0; + } + @NonNull private static String itemLink(@Nullable FeedItem item) { return (item != null && item.getLink() != null) diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java index 9c48f8df7..be4012cb6 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java @@ -112,6 +112,26 @@ public class FeedItemPermutorsTest { assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting } + @Test + public void testPermutorForRule_size_asc() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.SIZE_SMALL_LARGE); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting + } + + @Test + public void testPermutorForRule_size_desc() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.SIZE_LARGE_SMALL); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting + } + @Test public void testPermutorForRule_DURATION_DESC_NullMedia() { Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); @@ -166,21 +186,21 @@ public class FeedItemPermutorsTest { calendar.set(2019, 0, 1); // January 1st Feed feed1 = new Feed(null, null, "Feed title 1"); FeedItem feedItem1 = new FeedItem(1, "Title 1", null, null, calendar.getTime(), 0, feed1); - FeedMedia feedMedia1 = new FeedMedia(0, feedItem1, 1000, 0, 0, null, null, null, true, null, 0, 0); + FeedMedia feedMedia1 = new FeedMedia(0, feedItem1, 1000, 0, 100, null, null, null, true, null, 0, 0); feedItem1.setMedia(feedMedia1); itemList.add(feedItem1); calendar.set(2019, 2, 1); // March 1st Feed feed2 = new Feed(null, null, "Feed title 3"); FeedItem feedItem2 = new FeedItem(3, "Title 3", null, null, calendar.getTime(), 0, feed2); - FeedMedia feedMedia2 = new FeedMedia(0, feedItem2, 3000, 0, 0, null, null, null, true, null, 0, 0); + FeedMedia feedMedia2 = new FeedMedia(0, feedItem2, 3000, 0, 300, null, null, null, true, null, 0, 0); feedItem2.setMedia(feedMedia2); itemList.add(feedItem2); calendar.set(2019, 1, 1); // February 1st Feed feed3 = new Feed(null, null, "Feed title 2"); FeedItem feedItem3 = new FeedItem(2, "Title 2", null, null, calendar.getTime(), 0, feed3); - FeedMedia feedMedia3 = new FeedMedia(0, feedItem3, 2000, 0, 0, null, null, null, true, null, 0, 0); + FeedMedia feedMedia3 = new FeedMedia(0, feedItem3, 2000, 0, 200, null, null, null, true, null, 0, 0); feedItem3.setMedia(feedMedia3); itemList.add(feedItem3); diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/SortOrder.java b/model/src/main/java/de/danoeh/antennapod/model/feed/SortOrder.java index 1f9687b2e..08504b445 100644 --- a/model/src/main/java/de/danoeh/antennapod/model/feed/SortOrder.java +++ b/model/src/main/java/de/danoeh/antennapod/model/feed/SortOrder.java @@ -20,6 +20,8 @@ public enum SortOrder { DURATION_LONG_SHORT(6, INTRA_FEED), EPISODE_FILENAME_A_Z(7, INTRA_FEED), EPISODE_FILENAME_Z_A(8, INTRA_FEED), + SIZE_SMALL_LARGE(9, INTRA_FEED), + SIZE_LARGE_SMALL(10, INTRA_FEED), FEED_TITLE_A_Z(101, INTER_FEED), FEED_TITLE_Z_A(102, INTER_FEED), RANDOM(103, INTER_FEED), diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java index aae5154d3..d4404a082 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/mapper/FeedItemSortQuery.java @@ -25,6 +25,12 @@ public class FeedItemSortQuery { case DURATION_LONG_SHORT: sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_DURATION + " " + "DESC"; break; + case SIZE_SMALL_LARGE: + sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_SIZE + " " + "ASC"; + break; + case SIZE_LARGE_SMALL: + sortQuery = PodDBAdapter.TABLE_NAME_FEED_MEDIA + "." + PodDBAdapter.KEY_SIZE + " " + "DESC"; + break; default: sortQuery = ""; break; diff --git a/ui/i18n/src/main/res/values/strings.xml b/ui/i18n/src/main/res/values/strings.xml index 0697a2f53..73423f4b5 100644 --- a/ui/i18n/src/main/res/values/strings.xml +++ b/ui/i18n/src/main/res/values/strings.xml @@ -343,6 +343,7 @@ Podcast title Random Smart shuffle + Size Please confirm that you want to clear the queue of ALL of the episodes in it Time left:\u0020 @@ -771,6 +772,8 @@ Old \u2192 New Short \u2192 Long Long \u2192 Short + Small \u2192 Large + Large \u2192 Small Include playback position