diff options
author | GitStart <1501599+gitstart@users.noreply.github.com> | 2023-02-28 23:41:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-28 21:41:15 +0100 |
commit | 581e71b306bbc28dc504bde1d1bba6d6121e26e1 (patch) | |
tree | 9c559a79a01cf8345719af7e9848ff2ff8c564fa | |
parent | ccea00e4056b85d5be0ebfa898a1ac961c2e3c56 (diff) | |
download | AntennaPod-581e71b306bbc28dc504bde1d1bba6d6121e26e1.zip |
Add option to switch a- & descending sort order for Inbox (#6266)
8 files changed, 58 insertions, 13 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java index 37cd40b0e..c04adee6d 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/InboxFragment.java @@ -4,6 +4,8 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; @@ -20,6 +22,8 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedItemFilter; +import de.danoeh.antennapod.model.feed.SortOrder; +import de.danoeh.antennapod.storage.preferences.UserPreferences; import java.util.List; @@ -38,6 +42,8 @@ public class InboxFragment extends EpisodesListFragment { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View root = super.onCreateView(inflater, container, savedInstanceState); toolbar.inflateMenu(R.menu.inbox); + inflateSortMenu(); + toolbar.setTitle(R.string.inbox_label); prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); updateToolbar(); @@ -77,6 +83,13 @@ public class InboxFragment extends EpisodesListFragment { showRemoveAllDialog(); } return true; + } else { + SortOrder sortOrder = MenuItemToSortOrderConverter.convert(item); + if (sortOrder != null) { + UserPreferences.setInboxSortedOrder(sortOrder); + loadItems(); + return true; + } } return false; } @@ -84,13 +97,14 @@ public class InboxFragment extends EpisodesListFragment { @NonNull @Override protected List<FeedItem> loadData() { - return DBReader.getNewItemsList(0, page * EPISODES_PER_PAGE); + return DBReader.getNewItemsList(0, page * EPISODES_PER_PAGE, UserPreferences.getInboxSortedOrder()); } @NonNull @Override protected List<FeedItem> loadMoreData(int page) { - return DBReader.getNewItemsList((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE); + return DBReader.getNewItemsList((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE, + UserPreferences.getInboxSortedOrder()); } @Override @@ -103,6 +117,20 @@ public class InboxFragment extends EpisodesListFragment { ((MainActivity) getActivity()).showSnackbarAbovePlayer(R.string.removed_all_inbox_msg, Toast.LENGTH_SHORT); } + private void inflateSortMenu() { + Menu menu = toolbar.getMenu(); + MenuItem downloadsItem = menu.findItem(R.id.inbox_sort); + MenuInflater menuInflater = getActivity().getMenuInflater(); + menuInflater.inflate(R.menu.sort_menu, downloadsItem.getSubMenu()); + + // Remove the sorting options that are not needed in this fragment + toolbar.getMenu().findItem(R.id.sort_episode_title).setVisible(false); + toolbar.getMenu().findItem(R.id.sort_feed_title).setVisible(false); + toolbar.getMenu().findItem(R.id.sort_random).setVisible(false); + toolbar.getMenu().findItem(R.id.sort_smart_shuffle).setVisible(false); + toolbar.getMenu().findItem(R.id.keep_sorted).setVisible(false); + } + private void showRemoveAllDialog() { MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext()); builder.setTitle(R.string.remove_all_inbox_label); diff --git a/app/src/main/java/de/danoeh/antennapod/ui/home/sections/InboxSection.java b/app/src/main/java/de/danoeh/antennapod/ui/home/sections/InboxSection.java index e635e9538..6baea05e6 100644 --- a/app/src/main/java/de/danoeh/antennapod/ui/home/sections/InboxSection.java +++ b/app/src/main/java/de/danoeh/antennapod/ui/home/sections/InboxSection.java @@ -27,6 +27,7 @@ import de.danoeh.antennapod.fragment.swipeactions.SwipeActions; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedItemFilter; import de.danoeh.antennapod.storage.database.PodDBAdapter; +import de.danoeh.antennapod.storage.preferences.UserPreferences; import de.danoeh.antennapod.ui.home.HomeSection; import io.reactivex.Observable; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -125,7 +126,7 @@ public class InboxSection extends HomeSection { disposable.dispose(); } disposable = Observable.fromCallable(() -> - new Pair<>(DBReader.getNewItemsList(0, NUM_EPISODES), + new Pair<>(DBReader.getNewItemsList(0, NUM_EPISODES, UserPreferences.getInboxSortedOrder()), PodDBAdapter.getInstance().getNumberOfNewItems())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) diff --git a/app/src/main/res/menu/inbox.xml b/app/src/main/res/menu/inbox.xml index a284a2f56..fba8eefdb 100644 --- a/app/src/main/res/menu/inbox.xml +++ b/app/src/main/res/menu/inbox.xml @@ -16,6 +16,12 @@ android:icon="@drawable/ic_refresh"/> <item + android:id="@+id/inbox_sort" + android:title="@string/sort"> + <menu></menu> + </item> + + <item android:id="@+id/remove_all_inbox_item" android:title="@string/remove_all_inbox_label" android:menuCategory="container" diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java index 5fbbbbc19..c9f37fd00 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/AutomaticDownloadAlgorithm.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import de.danoeh.antennapod.model.feed.SortOrder; import de.danoeh.antennapod.net.download.serviceinterface.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadRequestCreator; import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface; @@ -52,7 +53,7 @@ public class AutomaticDownloadAlgorithm { List<FeedItem> candidates; final List<FeedItem> queue = DBReader.getQueue(); - final List<FeedItem> newItems = DBReader.getNewItemsList(0, Integer.MAX_VALUE); + final List<FeedItem> newItems = DBReader.getNewItemsList(0, Integer.MAX_VALUE, SortOrder.DATE_NEW_OLD); candidates = new ArrayList<>(queue.size() + newItems.size()); candidates.addAll(queue); for (FeedItem newItem : newItems) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index 62b1db3cb..b76284347 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -328,12 +328,12 @@ public final class DBReader { * @param limit The maximum number of episodes that should be loaded. * @return A list of FeedItems that are considered new. */ - public static List<FeedItem> getNewItemsList(int offset, int limit) { + public static List<FeedItem> getNewItemsList(int offset, int limit, SortOrder sortOrder) { Log.d(TAG, "getNewItemsList() called"); PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); - try (Cursor cursor = adapter.getNewItemsCursor(offset, limit)) { + try (Cursor cursor = adapter.getNewItemsCursor(offset, limit, sortOrder)) { List<FeedItem> items = extractItemlistFromCursor(adapter, cursor); loadAdditionalFeedItemListData(items); return items; diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java index c44c6ae43..886c6821b 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java @@ -10,9 +10,11 @@ import java.util.List; import java.util.Random; import androidx.test.platform.app.InstrumentationRegistry; + import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.model.feed.FeedItem; import de.danoeh.antennapod.model.feed.FeedMedia; +import de.danoeh.antennapod.model.feed.SortOrder; import de.danoeh.antennapod.storage.preferences.UserPreferences; import de.danoeh.antennapod.core.util.LongList; import de.danoeh.antennapod.storage.database.PodDBAdapter; @@ -281,7 +283,7 @@ public class DbReaderTest { for (int i = 0; i < newItems.size(); i++) { unreadIds[i] = newItems.get(i).getId(); } - List<FeedItem> newItemsSaved = DBReader.getNewItemsList(0, Integer.MAX_VALUE); + List<FeedItem> newItemsSaved = DBReader.getNewItemsList(0, Integer.MAX_VALUE, SortOrder.DATE_NEW_OLD); assertNotNull(newItemsSaved); assertEquals(newItemsSaved.size(), newItems.size()); for (FeedItem feedItem : newItemsSaved) { diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java index 0909ea4b7..825dea84e 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/PodDBAdapter.java @@ -1054,14 +1054,14 @@ public class PodDBAdapter { * Excludes those feeds that do not have 'Keep Updated' enabled. * The returned cursor uses the FEEDITEM_SEL_FI_SMALL selection. */ - public final Cursor getNewItemsCursor(int offset, int limit) { + public final Cursor getNewItemsCursor(int offset, int limit, SortOrder sortOrder) { + String sortQuery = FeedItemSortQuery.generateFrom(sortOrder); final String query = SELECT_FEED_ITEMS_AND_MEDIA + " INNER JOIN " + TABLE_NAME_FEEDS + " ON " + TABLE_NAME_FEED_ITEMS + "." + KEY_FEED + "=" + TABLE_NAME_FEEDS + "." + KEY_ID + " WHERE " + TABLE_NAME_FEED_ITEMS + "." + KEY_READ + "=" + FeedItem.NEW + " AND " + TABLE_NAME_FEEDS + "." + KEY_KEEP_UPDATED + " > 0" - + " ORDER BY " + TABLE_NAME_FEED_ITEMS + "." + KEY_PUBDATE + " DESC" - + " LIMIT " + offset + ", " + limit; + + " ORDER BY " + sortQuery + " LIMIT " + offset + ", " + limit; return db.rawQuery(query, null); } diff --git a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java index 93792121b..92acb6319 100644 --- a/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java +++ b/storage/preferences/src/main/java/de/danoeh/antennapod/storage/preferences/UserPreferences.java @@ -70,6 +70,7 @@ public class UserPreferences { public static final String PREF_QUEUE_KEEP_SORTED_ORDER = "prefQueueKeepSortedOrder"; public static final String PREF_NEW_EPISODES_ACTION = "prefNewEpisodesAction"; private static final String PREF_DOWNLOADS_SORTED_ORDER = "prefDownloadSortedOrder"; + private static final String PREF_INBOX_SORTED_ORDER = "prefInboxSortedOrder"; // Playback public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect"; @@ -942,12 +943,18 @@ public class UserPreferences { * Sets the sort order for the downloads. */ public static void setDownloadsSortedOrder(SortOrder sortOrder) { - if (sortOrder == null) { - return; - } prefs.edit().putString(PREF_DOWNLOADS_SORTED_ORDER, "" + sortOrder.code).apply(); } + public static SortOrder getInboxSortedOrder() { + String sortOrderStr = prefs.getString(PREF_INBOX_SORTED_ORDER, "" + SortOrder.DATE_NEW_OLD.code); + return SortOrder.fromCodeString(sortOrderStr); + } + + public static void setInboxSortedOrder(SortOrder sortOrder) { + prefs.edit().putString(PREF_INBOX_SORTED_ORDER, "" + sortOrder.code).apply(); + } + public static SubscriptionsFilter getSubscriptionsFilter() { String value = prefs.getString(PREF_FILTER_FEED, ""); return new SubscriptionsFilter(value); |