Add option to switch a- & descending sort order for Inbox (#6266)

This commit is contained in:
GitStart 2023-02-28 23:41:15 +03:00 committed by GitHub
parent ccea00e405
commit 581e71b306
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 58 additions and 13 deletions

View File

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

View File

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

View File

@ -15,6 +15,12 @@
custom:showAsAction="always"
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"

View File

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

View File

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

View File

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

View File

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

View File

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