From 1620d2954982195d2033444e227214dbcd90a245 Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 17 Oct 2019 11:44:00 -0700 Subject: [PATCH] Sort in Podcast screen - UI --- .../dialog/IntraFeedSortDialog.java | 51 +++++++++++++++++++ .../fragment/FeedItemlistFragment.java | 13 +++-- .../menuhandler/FeedMenuHandler.java | 20 ++++++++ app/src/main/res/menu/feedlist.xml | 7 +++ .../de/danoeh/antennapod/core/feed/Feed.java | 14 ++++- .../core/feed/IntraFeedSortOrder.java | 31 +++++++++++ core/src/main/res/values/arrays.xml | 19 +++++++ 7 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/de/danoeh/antennapod/dialog/IntraFeedSortDialog.java create mode 100644 core/src/main/java/de/danoeh/antennapod/core/feed/IntraFeedSortOrder.java diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/IntraFeedSortDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/IntraFeedSortDialog.java new file mode 100644 index 000000000..a554f8d76 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/IntraFeedSortDialog.java @@ -0,0 +1,51 @@ +package de.danoeh.antennapod.dialog; + +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; + +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.feed.IntraFeedSortOrder; + +public abstract class IntraFeedSortDialog { + + @Nullable + protected IntraFeedSortOrder currentSortOrder; + @NonNull + protected Context context; + + public IntraFeedSortDialog(@NonNull Context context, @Nullable IntraFeedSortOrder sortOrder) { + this.context = context; + this.currentSortOrder = sortOrder; + } + + public void openDialog() { + final String[] items = context.getResources().getStringArray(R.array.feed_episodes_sort_options); + final String[] valueStrs = context.getResources().getStringArray(R.array.feed_episodes_sort_values); + final IntraFeedSortOrder[] values = new IntraFeedSortOrder[valueStrs.length]; + for (int i = 0; i < valueStrs.length; i++) { + values[i] = IntraFeedSortOrder.valueOf(valueStrs[i]); + } + + int idxCurrentSort = -1; + for (int i = 0; i < values.length; i++) { + if (currentSortOrder == values[i]) { + idxCurrentSort = i; + break; + } + } + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.sort); + builder.setSingleChoiceItems(items, idxCurrentSort, (dialog, idxNewSort) -> { + updateSort(values[idxNewSort]); + dialog.dismiss(); + }); + builder.setNegativeButton(R.string.cancel_label, null); + builder.create().show(); + } + + protected abstract void updateSort(@NonNull IntraFeedSortOrder sortOrder); +} 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 0c33dce5a..471651e88 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java @@ -5,10 +5,6 @@ import android.content.Context; import android.content.DialogInterface; import android.graphics.LightingColorFilter; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.ListFragment; -import androidx.core.view.MenuItemCompat; -import androidx.appcompat.widget.SearchView; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; @@ -23,12 +19,16 @@ import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.appcompat.widget.SearchView; +import androidx.core.view.MenuItemCompat; +import androidx.fragment.app.ListFragment; + import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.joanzapata.iconify.Iconify; import com.joanzapata.iconify.widget.IconTextView; -import de.danoeh.antennapod.core.event.PlaybackPositionEvent; import org.apache.commons.lang3.Validate; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; @@ -45,6 +45,7 @@ import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator; 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.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedEvent; @@ -192,6 +193,7 @@ public class FeedItemlistFragment extends ListFragment { searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { + menu.findItem(R.id.sort_items).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); menu.findItem(R.id.filter_items).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); menu.findItem(R.id.episode_actions).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); menu.findItem(R.id.refresh_item).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); @@ -623,6 +625,7 @@ public class FeedItemlistFragment extends ListFragment { @NonNull private Optional loadData() { + // TODO-2524: apply sorting, either at db level or here Feed feed = DBReader.getFeed(feedID); if (feed != null && feed.getItemFilter() != null) { DBReader.loadAdditionalFeedItemListData(feed.getItems()); diff --git a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java index f7aae8cde..5e0d65d0c 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java @@ -7,6 +7,8 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; +import androidx.annotation.NonNull; + import org.apache.commons.lang3.StringUtils; import java.util.Set; @@ -14,12 +16,14 @@ import java.util.Set; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.dialog.ConfirmationDialog; import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.IntraFeedSortOrder; import de.danoeh.antennapod.core.storage.DBTasks; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DownloadRequestException; import de.danoeh.antennapod.core.util.IntentUtils; import de.danoeh.antennapod.core.util.ShareUtils; import de.danoeh.antennapod.dialog.FilterDialog; +import de.danoeh.antennapod.dialog.IntraFeedSortDialog; /** * Handles interactions with the FeedItemMenu. @@ -65,6 +69,9 @@ public class FeedMenuHandler { case R.id.refresh_complete_item: DBTasks.forceRefreshCompleteFeed(context, selectedFeed); break; + case R.id.sort_items: + showSortDialog(context, selectedFeed); + break; case R.id.filter_items: showFilterDialog(context, selectedFeed); break; @@ -108,4 +115,17 @@ public class FeedMenuHandler { filterDialog.openDialog(); } + + + private static void showSortDialog(Context context, Feed selectedFeed) { + IntraFeedSortDialog sortDialog = new IntraFeedSortDialog(context, selectedFeed.getSortOrder()) { + @Override + protected void updateSort(@NonNull IntraFeedSortOrder sortOrder) { + selectedFeed.setSortOrder(sortOrder); + // TODO-2524: update in db + } + }; + sortDialog.openDialog(); + } + } diff --git a/app/src/main/res/menu/feedlist.xml b/app/src/main/res/menu/feedlist.xml index fdd0e01bc..8ff569505 100644 --- a/app/src/main/res/menu/feedlist.xml +++ b/app/src/main/res/menu/feedlist.xml @@ -2,6 +2,13 @@ + + is_favorite + + + @string/sort_date_new_old + @string/sort_date_old_new + @string/sort_title_a_z + @string/sort_title_z_a + @string/sort_duration_short_long + @string/sort_duration_long_short + + + + DATE_NEW_OLD + DATE_OLD_NEW + EPISODE_TITLE_A_Z + EPISODE_TITLE_Z_A + DURATION_SHORT_LONG + DURATION_LONG_SHORT + + 20 MiB 50 MiB