Honor sort in episode list view in Android Auto (#6756)

This commit is contained in:
Tony Tam 2023-11-17 13:33:16 -08:00 committed by GitHub
parent 0bb4870820
commit c7d6cd358c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 22 deletions

View File

@ -1,7 +1,5 @@
package de.danoeh.antennapod.fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
@ -14,6 +12,8 @@ import de.danoeh.antennapod.dialog.AllEpisodesFilterDialog;
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 org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.Subscribe;
@ -26,10 +26,7 @@ import java.util.List;
*/
public class AllEpisodesFragment extends EpisodesListFragment {
public static final String TAG = "EpisodesFragment";
private static final String PREF_NAME = "PrefAllEpisodesFragment";
private static final String PREF_FILTER = "filter";
public static final String PREF_SORT = "prefEpisodesSort";
private SharedPreferences prefs;
public static final String PREF_NAME = "PrefAllEpisodesFragment";
@NonNull
@Override
@ -40,7 +37,6 @@ public class AllEpisodesFragment extends EpisodesListFragment {
toolbar.setTitle(R.string.episodes_label);
updateToolbar();
updateFilterUi();
prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
txtvInformation.setOnClickListener(
v -> AllEpisodesFilterDialog.newInstance(getFilter()).show(getChildFragmentManager(), null));
return root;
@ -61,13 +57,15 @@ public class AllEpisodesFragment extends EpisodesListFragment {
@NonNull
@Override
protected List<FeedItem> loadData() {
return DBReader.getEpisodes(0, page * EPISODES_PER_PAGE, getFilter(), getSortOrder());
return DBReader.getEpisodes(0, page * EPISODES_PER_PAGE, getFilter(),
UserPreferences.getAllEpisodesSortOrder());
}
@NonNull
@Override
protected List<FeedItem> loadMoreData(int page) {
return DBReader.getEpisodes((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE, getFilter(), getSortOrder());
return DBReader.getEpisodes((page - 1) * EPISODES_PER_PAGE, EPISODES_PER_PAGE, getFilter(),
UserPreferences.getAllEpisodesSortOrder());
}
@Override
@ -77,8 +75,7 @@ public class AllEpisodesFragment extends EpisodesListFragment {
@Override
protected FeedItemFilter getFilter() {
SharedPreferences prefs = getActivity().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
return new FeedItemFilter(prefs.getString(PREF_FILTER, ""));
return new FeedItemFilter(UserPreferences.getPrefFilterAllEpisodes());
}
@Override
@ -119,13 +116,13 @@ public class AllEpisodesFragment extends EpisodesListFragment {
}
private void saveSortOrderAndRefresh(SortOrder type) {
prefs.edit().putString(PREF_SORT, "" + type.code).apply();
UserPreferences.setAllEpisodesSortOrder(type);
loadItems();
}
@Subscribe
public void onFilterChanged(AllEpisodesFilterDialog.AllEpisodesFilterChangedEvent event) {
prefs.edit().putString(PREF_FILTER, StringUtils.join(event.filterValues, ",")).apply();
UserPreferences.setPrefFilterAllEpisodes(StringUtils.join(event.filterValues, ","));
updateFilterUi();
page = 1;
loadItems();
@ -143,8 +140,4 @@ public class AllEpisodesFragment extends EpisodesListFragment {
toolbar.getMenu().findItem(R.id.action_favorites).setIcon(
getFilter().showIsFavorite ? R.drawable.ic_star : R.drawable.ic_star_border);
}
private SortOrder getSortOrder() {
return SortOrder.fromCodeString(prefs.getString(PREF_SORT, "" + SortOrder.DATE_NEW_OLD.code));
}
}

View File

@ -6,12 +6,15 @@ import android.view.KeyEvent;
import androidx.core.app.NotificationManagerCompat;
import androidx.preference.PreferenceManager;
import org.apache.commons.lang3.StringUtils;
import java.util.concurrent.TimeUnit;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.SleepTimerPreferences;
import de.danoeh.antennapod.error.CrashReportWriter;
import de.danoeh.antennapod.fragment.AllEpisodesFragment;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation;
import de.danoeh.antennapod.fragment.QueueFragment;
@ -149,5 +152,19 @@ public class PreferenceUpgrader {
if (oldVersion < 3020000) {
NotificationManagerCompat.from(context).deleteNotificationChannel("auto_download");
}
if (oldVersion < 3030000) {
SharedPreferences allEpisodesPreferences =
context.getSharedPreferences(AllEpisodesFragment.PREF_NAME, Context.MODE_PRIVATE);
String oldEpisodeSort = allEpisodesPreferences.getString(UserPreferences.PREF_SORT_ALL_EPISODES, "");
if (!StringUtils.isAllEmpty(oldEpisodeSort)) {
prefs.edit().putString(UserPreferences.PREF_SORT_ALL_EPISODES, oldEpisodeSort).apply();
}
String oldEpisodeFilter = allEpisodesPreferences.getString("filter", "");
if (!StringUtils.isAllEmpty(oldEpisodeFilter)) {
prefs.edit().putString(UserPreferences.PREF_FILTER_ALL_EPISODES, oldEpisodeFilter).apply();
}
}
}
}

View File

@ -89,7 +89,6 @@ import de.danoeh.antennapod.model.feed.FeedItem;
import de.danoeh.antennapod.model.feed.FeedItemFilter;
import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.model.feed.FeedPreferences;
import de.danoeh.antennapod.model.feed.SortOrder;
import de.danoeh.antennapod.model.playback.MediaType;
import de.danoeh.antennapod.model.playback.Playable;
import de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer;
@ -429,10 +428,11 @@ public class PlaybackService extends MediaBrowserServiceCompat {
new FeedItemFilter(FeedItemFilter.DOWNLOADED), UserPreferences.getDownloadsSortedOrder());
} else if (parentId.equals(getResources().getString(R.string.episodes_label))) {
feedItems = DBReader.getEpisodes(0, MAX_ANDROID_AUTO_EPISODES_PER_FEED,
new FeedItemFilter(FeedItemFilter.UNPLAYED), SortOrder.DATE_NEW_OLD);
new FeedItemFilter(FeedItemFilter.UNPLAYED), UserPreferences.getAllEpisodesSortOrder());
} else if (parentId.startsWith("FeedId:")) {
long feedId = Long.parseLong(parentId.split(":")[1]);
feedItems = DBReader.getFeedItemList(DBReader.getFeed(feedId));
Feed feed = DBReader.getFeed(feedId);
feedItems = DBReader.getFeedItemList(feed, FeedItemFilter.unfiltered(), feed.getSortOrder());
} else if (parentId.equals(getString(R.string.recently_played_episodes))) {
Playable playable = PlaybackPreferences.createInstanceFromPreferences(this);
if (playable instanceof FeedMedia) {

View File

@ -15,9 +15,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.danoeh.antennapod.core.util.FeedItemPermutors;
import de.danoeh.antennapod.core.util.LongList;
import de.danoeh.antennapod.core.util.comparator.DownloadResultComparator;
import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator;
import de.danoeh.antennapod.core.util.comparator.PlaybackCompletionDateComparator;
import de.danoeh.antennapod.model.feed.Chapter;
import de.danoeh.antennapod.model.feed.Feed;
@ -170,13 +170,18 @@ public final class DBReader {
}
public static List<FeedItem> getFeedItemList(final Feed feed, final FeedItemFilter filter) {
return getFeedItemList(feed, filter, SortOrder.DATE_NEW_OLD);
}
public static List<FeedItem> getFeedItemList(final Feed feed, final FeedItemFilter filter, SortOrder sortOrder) {
Log.d(TAG, "getFeedItemList() called with: " + "feed = [" + feed + "]");
PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open();
try (Cursor cursor = adapter.getItemsOfFeedCursor(feed, filter)) {
List<FeedItem> items = extractItemlistFromCursor(adapter, cursor);
Collections.sort(items, new FeedItemPubdateComparator());
FeedItemPermutors.getPermutor(sortOrder).reorder(items);
feed.setItems(items);
for (FeedItem item : items) {
item.setFeed(feed);
}

View File

@ -67,6 +67,10 @@ public class UserPreferences {
private static final String PREF_DOWNLOADS_SORTED_ORDER = "prefDownloadSortedOrder";
private static final String PREF_INBOX_SORTED_ORDER = "prefInboxSortedOrder";
// Episode
public static final String PREF_SORT_ALL_EPISODES = "prefEpisodesSort";
public static final String PREF_FILTER_ALL_EPISODES = "prefEpisodesFilter";
// Playback
public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect";
public static final String PREF_UNPAUSE_ON_HEADSET_RECONNECT = "prefUnpauseOnHeadsetReconnect";
@ -867,4 +871,21 @@ public class UserPreferences {
public static boolean shouldShowSubscriptionTitle() {
return prefs.getBoolean(PREF_SUBSCRIPTION_TITLE, false);
}
public static void setAllEpisodesSortOrder(SortOrder s) {
prefs.edit().putString(PREF_SORT_ALL_EPISODES, "" + s.code).apply();
}
public static SortOrder getAllEpisodesSortOrder() {
return SortOrder.fromCodeString(prefs.getString(PREF_SORT_ALL_EPISODES,
"" + SortOrder.DATE_NEW_OLD.code));
}
public static String getPrefFilterAllEpisodes() {
return prefs.getString(PREF_FILTER_ALL_EPISODES, "");
}
public static void setPrefFilterAllEpisodes(String filter) {
prefs.edit().putString(PREF_FILTER_ALL_EPISODES, filter).apply();
}
}