diff options
author | Jan-Peter von Hunnius <jan@hunnius.net> | 2019-08-23 17:38:42 +0200 |
---|---|---|
committer | H. Lehmann <ByteHamster@users.noreply.github.com> | 2019-08-23 17:38:42 +0200 |
commit | be957e53b3a6ad112ba66d8b4e32c972486aa09a (patch) | |
tree | 1c05ae71faf283dab0bde80ecfafd0a33d3e5521 /app/src/main/java/de/danoeh/antennapod | |
parent | 91b171acb3140c6d13d6fe8c417aaae11e12d2e8 (diff) | |
download | AntennaPod-be957e53b3a6ad112ba66d8b4e32c972486aa09a.zip |
Implemented filter function for "All Episodes" list (#3321)
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod')
5 files changed, 136 insertions, 39 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java new file mode 100644 index 000000000..607084c42 --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java @@ -0,0 +1,62 @@ +package de.danoeh.antennapod.dialog; + +import android.content.Context; +import android.support.v7.app.AlertDialog; +import android.text.TextUtils; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.core.feed.FeedItemFilter; + +public abstract class FilterDialog { + + protected FeedItemFilter filter; + protected Context context; + + public FilterDialog(Context context, FeedItemFilter feedItemFilter) { + this.context = context; + this.filter = feedItemFilter; + } + + public void openDialog() { + final String[] items = context.getResources().getStringArray(R.array.episode_filter_options); + final String[] values = context.getResources().getStringArray(R.array.episode_filter_values); + final boolean[] checkedItems = new boolean[items.length]; + + final Set<String> filterValues = new HashSet<>(Arrays.asList(filter.getValues())); + + // make sure we have no empty strings in the filter list + for (String filterValue : filterValues) { + if (TextUtils.isEmpty(filterValue)) { + filterValues.remove(filterValue); + } + } + + for (int i = 0; i < values.length; i++) { + String value = values[i]; + if (filterValues.contains(value)) { + checkedItems[i] = true; + } + } + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.filter); + builder.setMultiChoiceItems(items, checkedItems, (dialog, which, isChecked) -> { + if (isChecked) { + filterValues.add(values[which]); + } else { + filterValues.remove(values[which]); + } + }); + builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { + updateFilter(filterValues); + }); + builder.setNegativeButton(R.string.cancel_label, null); + builder.create().show(); + } + + protected abstract void updateFilter(Set<String> filterValues); +} diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java index 62d798cf6..41fc523d7 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/AllEpisodesFragment.java @@ -21,8 +21,10 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ProgressBar; +import android.widget.TextView; import android.widget.Toast; +import com.joanzapata.iconify.Iconify; import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration; import org.greenrobot.eventbus.EventBus; @@ -31,6 +33,7 @@ import org.greenrobot.eventbus.ThreadMode; import java.util.ArrayList; import java.util.List; +import java.util.Set; import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.MainActivity; @@ -42,6 +45,7 @@ import de.danoeh.antennapod.core.event.FeedItemEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedItemFilter; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.DownloadRequest; @@ -53,6 +57,7 @@ import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.FeedItemUtil; import de.danoeh.antennapod.core.util.LongList; +import de.danoeh.antennapod.dialog.FilterDialog; import de.danoeh.antennapod.menuhandler.FeedItemMenuHandler; import de.danoeh.antennapod.menuhandler.MenuItemUtils; import de.danoeh.antennapod.view.EmptyViewHandler; @@ -93,6 +98,9 @@ public class AllEpisodesFragment extends Fragment { Disposable disposable; private LinearLayoutManager layoutManager; + protected TextView txtvInformation; + private static FeedItemFilter feedItemFilter = new FeedItemFilter(""); + boolean showOnlyNewEpisodes() { return false; } @@ -246,6 +254,9 @@ public class AllEpisodesFragment extends Fragment { }; removeAllNewFlagsConfirmationDialog.createNewDialog().show(); return true; + case R.id.filter_items: + showFilterDialog(); + return true; default: return false; } @@ -292,6 +303,7 @@ public class AllEpisodesFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View root = inflater.inflate(R.layout.all_episodes_fragment, container, false); + txtvInformation = root.findViewById(R.id.txtvInformation); layoutManager = new LinearLayoutManager(getActivity()); recyclerView = root.findViewById(android.R.id.list); @@ -320,14 +332,21 @@ public class AllEpisodesFragment extends Fragment { return root; } - private void onFragmentLoaded(List<FeedItem> episodes) { - this.episodes = episodes; + protected void onFragmentLoaded(List<FeedItem> episodes) { listAdapter.notifyDataSetChanged(); if (episodes.size() == 0) { createRecycleAdapter(recyclerView, emptyView); } + if (feedItemFilter.getValues().length > 0) { + txtvInformation.setText("{fa-info-circle} " + this.getString(R.string.filtered_label)); + Iconify.addIcons(txtvInformation); + txtvInformation.setVisibility(View.VISIBLE); + } else { + txtvInformation.setVisibility(View.GONE); + } + restoreScrollPosition(); requireActivity().invalidateOptionsMenu(); } @@ -458,13 +477,14 @@ public class AllEpisodesFragment extends Fragment { .observeOn(AndroidSchedulers.mainThread()) .subscribe(data -> { progLoading.setVisibility(View.GONE); - onFragmentLoaded(data); + episodes = data; + onFragmentLoaded(episodes); }, error -> Log.e(TAG, Log.getStackTraceString(error))); } @NonNull List<FeedItem> loadData() { - return DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT); + return feedItemFilter.filter( DBReader.getRecentlyPublishedEpisodes(RECENT_EPISODES_LIMIT) ); } void removeNewFlagWithUndo(FeedItem item) { @@ -498,4 +518,16 @@ public class AllEpisodesFragment extends Fragment { snackbar.show(); h.postDelayed(r, (int) Math.ceil(snackbar.getDuration() * 1.05f)); } + + private void showFilterDialog() { + FilterDialog filterDialog = new FilterDialog(getContext(), feedItemFilter) { + @Override + protected void updateFilter(Set<String> filterValues) { + feedItemFilter = new FeedItemFilter(filterValues.toArray(new String[filterValues.size()])); + loadItems(); + } + }; + + filterDialog.openDialog(); + } } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/FavoriteEpisodesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/FavoriteEpisodesFragment.java index bb029b731..7a22cb1eb 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FavoriteEpisodesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FavoriteEpisodesFragment.java @@ -7,6 +7,7 @@ import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; import android.view.View; import android.view.ViewGroup; @@ -84,6 +85,19 @@ public class FavoriteEpisodesFragment extends AllEpisodesFragment { return root; } + @Override + public void onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + + menu.removeItem(R.id.filter_items); + } + + @Override + protected void onFragmentLoaded(List<FeedItem> episodes) { + super.onFragmentLoaded(episodes); + txtvInformation.setVisibility(View.GONE); + } + @NonNull @Override protected List<FeedItem> loadData() { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/NewEpisodesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/NewEpisodesFragment.java index 1bf907aee..eaa385fa5 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/NewEpisodesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/NewEpisodesFragment.java @@ -5,6 +5,7 @@ import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.helper.ItemTouchHelper; import android.view.LayoutInflater; +import android.view.Menu; import android.view.View; import android.view.ViewGroup; @@ -92,6 +93,19 @@ public class NewEpisodesFragment extends AllEpisodesFragment { return root; } + @Override + public void onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + + menu.removeItem(R.id.filter_items); + } + + @Override + protected void onFragmentLoaded(List<FeedItem> episodes) { + super.onFragmentLoaded(episodes); + txtvInformation.setVisibility(View.GONE); + } + @NonNull @Override protected List<FeedItem> loadData() { 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 0928cfd62..3949119fc 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java @@ -20,11 +20,13 @@ 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.FeedItemFilter; 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; /** * Handles interactions with the FeedItemMenu. @@ -105,42 +107,15 @@ public class FeedMenuHandler { return true; } - private static void showFilterDialog(final Context context, final Feed feed) { - final String[] items = context.getResources().getStringArray(R.array.episode_filter_options); - final String[] values = context.getResources().getStringArray(R.array.episode_filter_values); - final boolean[] checkedItems = new boolean[items.length]; - - final Set<String> filter = new HashSet<>(Arrays.asList(feed.getItemFilter().getValues())); - Iterator<String> it = filter.iterator(); - while(it.hasNext()) { - // make sure we have no empty strings in the filter list - if(TextUtils.isEmpty(it.next())) { - it.remove(); - } - } - for(int i=0; i < values.length; i++) { - String value = values[i]; - if(filter.contains(value)) { - checkedItems[i] = true; + private static void showFilterDialog(Context context, Feed selectedFeed) { + FilterDialog filterDialog = new FilterDialog(context, selectedFeed.getItemFilter()) { + @Override + protected void updateFilter(Set<String> filterValues) { + selectedFeed.setItemFilter(filterValues.toArray(new String[filterValues.size()])); + DBWriter.setFeedItemsFilter(selectedFeed.getId(), filterValues); } - } - - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.filter); - builder.setMultiChoiceItems(items, checkedItems, (dialog, which, isChecked) -> { - if (isChecked) { - filter.add(values[which]); - } else { - filter.remove(values[which]); - } - }); - builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { - feed.setItemFilter(filter.toArray(new String[filter.size()])); - DBWriter.setFeedItemsFilter(feed.getId(), filter); - }); - builder.setNegativeButton(R.string.cancel_label, null); - builder.create().show(); + }; + filterDialog.openDialog(); } - } |