1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
package de.danoeh.antennapod.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.dialog.AllEpisodesFilterDialog;
import de.danoeh.antennapod.dialog.ItemSortDialog;
import de.danoeh.antennapod.event.FeedListUpdateEvent;
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.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
* Shows all episodes (possibly filtered by user).
*/
public class AllEpisodesFragment extends EpisodesListFragment {
public static final String TAG = "EpisodesFragment";
public static final String PREF_NAME = "PrefAllEpisodesFragment";
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View root = super.onCreateView(inflater, container, savedInstanceState);
toolbar.inflateMenu(R.menu.episodes);
toolbar.setTitle(R.string.episodes_label);
updateToolbar();
updateFilterUi();
txtvInformation.setOnClickListener(
v -> AllEpisodesFilterDialog.newInstance(getFilter()).show(getChildFragmentManager(), null));
return root;
}
@NonNull
@Override
protected List<FeedItem> loadData() {
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(),
UserPreferences.getAllEpisodesSortOrder());
}
@Override
protected int loadTotalItemCount() {
return DBReader.getTotalEpisodeCount(getFilter());
}
@Override
protected FeedItemFilter getFilter() {
return new FeedItemFilter(UserPreferences.getPrefFilterAllEpisodes());
}
@Override
protected String getFragmentTag() {
return TAG;
}
@Override
protected String getPrefName() {
return PREF_NAME;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (super.onOptionsItemSelected(item)) {
return true;
}
if (item.getItemId() == R.id.filter_items) {
AllEpisodesFilterDialog.newInstance(getFilter()).show(getChildFragmentManager(), null);
return true;
} else if (item.getItemId() == R.id.action_favorites) {
ArrayList<String> filter = new ArrayList<>(getFilter().getValuesList());
if (filter.contains(FeedItemFilter.IS_FAVORITE)) {
filter.remove(FeedItemFilter.IS_FAVORITE);
} else {
filter.add(FeedItemFilter.IS_FAVORITE);
}
onFilterChanged(new AllEpisodesFilterDialog.AllEpisodesFilterChangedEvent(new HashSet<>(filter)));
return true;
} else if (item.getItemId() == R.id.episodes_sort) {
new AllEpisodesSortDialog().show(getChildFragmentManager().beginTransaction(), "SortDialog");
return true;
}
return false;
}
@Subscribe
public void onFilterChanged(AllEpisodesFilterDialog.AllEpisodesFilterChangedEvent event) {
UserPreferences.setPrefFilterAllEpisodes(StringUtils.join(event.filterValues, ","));
updateFilterUi();
page = 1;
loadItems();
}
private void updateFilterUi() {
swipeActions.setFilter(getFilter());
if (getFilter().getValues().length > 0) {
txtvInformation.setVisibility(View.VISIBLE);
emptyView.setMessage(R.string.no_all_episodes_filtered_label);
} else {
txtvInformation.setVisibility(View.GONE);
emptyView.setMessage(R.string.no_all_episodes_label);
}
toolbar.getMenu().findItem(R.id.action_favorites).setIcon(
getFilter().showIsFavorite ? R.drawable.ic_star : R.drawable.ic_star_border);
}
public static class AllEpisodesSortDialog extends ItemSortDialog {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sortOrder = UserPreferences.getAllEpisodesSortOrder();
}
@Override
protected void onAddItem(int title, SortOrder ascending, SortOrder descending, boolean ascendingIsDefault) {
if (ascending == SortOrder.DATE_OLD_NEW || ascending == SortOrder.DURATION_SHORT_LONG) {
super.onAddItem(title, ascending, descending, ascendingIsDefault);
}
}
@Override
protected void onSelectionChanged() {
super.onSelectionChanged();
UserPreferences.setAllEpisodesSortOrder(sortOrder);
EventBus.getDefault().post(new FeedListUpdateEvent(0));
}
}
}
|