Search in sources :

Example 1 with SubscriptionsFilter

use of de.danoeh.antennapod.core.feed.SubscriptionsFilter in project AntennaPod by AntennaPod.

the class DBReader method getNavDrawerData.

/**
 * Returns data necessary for displaying the navigation drawer. This includes
 * the list of subscriptions, the number of items in the queue and the number of unread
 * items.
 */
@NonNull
public static NavDrawerData getNavDrawerData() {
    Log.d(TAG, "getNavDrawerData() called with: " + "");
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    final LongIntMap feedCounters = adapter.getFeedCounters();
    SubscriptionsFilter subscriptionsFilter = UserPreferences.getSubscriptionsFilter();
    List<Feed> feeds = subscriptionsFilter.filter(getFeedList(adapter), feedCounters);
    Comparator<Feed> comparator;
    int feedOrder = UserPreferences.getFeedOrder();
    if (feedOrder == UserPreferences.FEED_ORDER_COUNTER) {
        comparator = (lhs, rhs) -> {
            long counterLhs = feedCounters.get(lhs.getId());
            long counterRhs = feedCounters.get(rhs.getId());
            if (counterLhs > counterRhs) {
                // reverse natural order: podcast with most unplayed episodes first
                return -1;
            } else if (counterLhs == counterRhs) {
                return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
            } else {
                return 1;
            }
        };
    } else if (feedOrder == UserPreferences.FEED_ORDER_ALPHABETICAL) {
        comparator = (lhs, rhs) -> {
            String t1 = lhs.getTitle();
            String t2 = rhs.getTitle();
            if (t1 == null) {
                return 1;
            } else if (t2 == null) {
                return -1;
            } else {
                return t1.compareToIgnoreCase(t2);
            }
        };
    } else if (feedOrder == UserPreferences.FEED_ORDER_MOST_PLAYED) {
        final LongIntMap playedCounters = adapter.getPlayedEpisodesCounters();
        comparator = (lhs, rhs) -> {
            long counterLhs = playedCounters.get(lhs.getId());
            long counterRhs = playedCounters.get(rhs.getId());
            if (counterLhs > counterRhs) {
                // podcast with most played episodes first
                return -1;
            } else if (counterLhs == counterRhs) {
                return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
            } else {
                return 1;
            }
        };
    } else {
        final Map<Long, Long> recentPubDates = adapter.getMostRecentItemDates();
        comparator = (lhs, rhs) -> {
            long dateLhs = recentPubDates.containsKey(lhs.getId()) ? recentPubDates.get(lhs.getId()) : 0;
            long dateRhs = recentPubDates.containsKey(rhs.getId()) ? recentPubDates.get(rhs.getId()) : 0;
            return Long.compare(dateRhs, dateLhs);
        };
    }
    Collections.sort(feeds, comparator);
    int queueSize = adapter.getQueueSize();
    int numNewItems = adapter.getNumberOfNewItems();
    int numDownloadedItems = adapter.getNumberOfDownloadedEpisodes();
    List<NavDrawerData.DrawerItem> items = new ArrayList<>();
    Map<String, NavDrawerData.TagDrawerItem> folders = new HashMap<>();
    for (Feed feed : feeds) {
        for (String tag : feed.getPreferences().getTags()) {
            NavDrawerData.FeedDrawerItem drawerItem = new NavDrawerData.FeedDrawerItem(feed, feed.getId(), feedCounters.get(feed.getId()));
            if (FeedPreferences.TAG_ROOT.equals(tag)) {
                items.add(drawerItem);
                continue;
            }
            NavDrawerData.TagDrawerItem folder;
            if (folders.containsKey(tag)) {
                folder = folders.get(tag);
            } else {
                folder = new NavDrawerData.TagDrawerItem(tag);
                folders.put(tag, folder);
            }
            drawerItem.id |= folder.id;
            folder.children.add(drawerItem);
        }
    }
    List<NavDrawerData.TagDrawerItem> foldersSorted = new ArrayList<>(folders.values());
    Collections.sort(foldersSorted, (o1, o2) -> o1.getTitle().compareToIgnoreCase(o2.getTitle()));
    items.addAll(foldersSorted);
    NavDrawerData result = new NavDrawerData(items, queueSize, numNewItems, numDownloadedItems, feedCounters, UserPreferences.getEpisodeCleanupAlgorithm().getReclaimableItems());
    adapter.close();
    return result;
}
Also used : FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) NonNull(androidx.annotation.NonNull) DownloadStatusComparator(de.danoeh.antennapod.core.util.comparator.DownloadStatusComparator) HashMap(java.util.HashMap) LongList(de.danoeh.antennapod.core.util.LongList) FeedPreferencesCursorMapper(de.danoeh.antennapod.core.storage.mapper.FeedPreferencesCursorMapper) ArrayList(java.util.ArrayList) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) FeedItemCursorMapper(de.danoeh.antennapod.core.storage.mapper.FeedItemCursorMapper) FeedPreferences(de.danoeh.antennapod.model.feed.FeedPreferences) FeedItemFilter(de.danoeh.antennapod.model.feed.FeedItemFilter) Map(java.util.Map) Chapter(de.danoeh.antennapod.model.feed.Chapter) DownloadStatus(de.danoeh.antennapod.core.service.download.DownloadStatus) Log(android.util.Log) Feed(de.danoeh.antennapod.model.feed.Feed) PlaybackCompletionDateComparator(de.danoeh.antennapod.core.util.comparator.PlaybackCompletionDateComparator) Cursor(android.database.Cursor) ArrayMap(androidx.collection.ArrayMap) FeedMediaCursorMapper(de.danoeh.antennapod.core.storage.mapper.FeedMediaCursorMapper) LongIntMap(de.danoeh.antennapod.core.util.LongIntMap) FeedMedia(de.danoeh.antennapod.model.feed.FeedMedia) FeedItem(de.danoeh.antennapod.model.feed.FeedItem) TextUtils(android.text.TextUtils) File(java.io.File) SubscriptionsFilter(de.danoeh.antennapod.core.feed.SubscriptionsFilter) List(java.util.List) Nullable(androidx.annotation.Nullable) ChapterCursorMapper(de.danoeh.antennapod.core.storage.mapper.ChapterCursorMapper) FeedCursorMapper(de.danoeh.antennapod.core.storage.mapper.FeedCursorMapper) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LongIntMap(de.danoeh.antennapod.core.util.LongIntMap) SubscriptionsFilter(de.danoeh.antennapod.core.feed.SubscriptionsFilter) Feed(de.danoeh.antennapod.model.feed.Feed) NonNull(androidx.annotation.NonNull)

Example 2 with SubscriptionsFilter

use of de.danoeh.antennapod.core.feed.SubscriptionsFilter in project AntennaPod by AntennaPod.

the class SubscriptionsFilterDialog method showDialog.

public static void showDialog(Context context) {
    SubscriptionsFilter subscriptionsFilter = UserPreferences.getSubscriptionsFilter();
    final Set<String> filterValues = new HashSet<>(Arrays.asList(subscriptionsFilter.getValues()));
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(context.getString(R.string.pref_filter_feed_title));
    LayoutInflater inflater = LayoutInflater.from(context);
    View layout = inflater.inflate(R.layout.filter_dialog, null, false);
    LinearLayout rows = layout.findViewById(R.id.filter_rows);
    builder.setView(layout);
    for (SubscriptionsFilterGroup item : SubscriptionsFilterGroup.values()) {
        RecursiveRadioGroup row = (RecursiveRadioGroup) inflater.inflate(R.layout.filter_dialog_row, null);
        RadioButton filter1 = row.findViewById(R.id.filter_dialog_radioButton1);
        RadioButton filter2 = row.findViewById(R.id.filter_dialog_radioButton2);
        filter1.setText(item.values[0].displayName);
        filter1.setTag(item.values[0].filterId);
        if (item.values.length == 2) {
            filter2.setText(item.values[1].displayName);
            filter2.setTag(item.values[1].filterId);
        } else {
            filter2.setVisibility(View.GONE);
        }
        rows.addView(row);
    }
    for (String filterId : filterValues) {
        if (!TextUtils.isEmpty(filterId)) {
            ((RadioButton) layout.findViewWithTag(filterId)).setChecked(true);
        }
    }
    builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
        filterValues.clear();
        for (int i = 0; i < rows.getChildCount(); i++) {
            if (!(rows.getChildAt(i) instanceof RecursiveRadioGroup)) {
                continue;
            }
            RecursiveRadioGroup group = (RecursiveRadioGroup) rows.getChildAt(i);
            if (group.getCheckedButton() != null) {
                String tag = (String) group.getCheckedButton().getTag();
                if (tag != null) {
                    // Clear buttons use no tag
                    filterValues.add((String) group.getCheckedButton().getTag());
                }
            }
        }
        updateFilter(filterValues);
    });
    builder.setNegativeButton(R.string.cancel_label, null);
    builder.show();
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) SubscriptionsFilterGroup(de.danoeh.antennapod.core.feed.SubscriptionsFilterGroup) RadioButton(android.widget.RadioButton) View(android.view.View) LayoutInflater(android.view.LayoutInflater) RecursiveRadioGroup(de.danoeh.antennapod.ui.common.RecursiveRadioGroup) SubscriptionsFilter(de.danoeh.antennapod.core.feed.SubscriptionsFilter) LinearLayout(android.widget.LinearLayout) HashSet(java.util.HashSet)

Example 3 with SubscriptionsFilter

use of de.danoeh.antennapod.core.feed.SubscriptionsFilter in project AntennaPod by AntennaPod.

the class SubscriptionsFilterDialog method updateFilter.

private static void updateFilter(Set<String> filterValues) {
    SubscriptionsFilter subscriptionsFilter = new SubscriptionsFilter(filterValues.toArray(new String[0]));
    UserPreferences.setSubscriptionsFilter(subscriptionsFilter);
    EventBus.getDefault().post(new UnreadItemsUpdateEvent());
}
Also used : UnreadItemsUpdateEvent(de.danoeh.antennapod.event.UnreadItemsUpdateEvent) SubscriptionsFilter(de.danoeh.antennapod.core.feed.SubscriptionsFilter)

Aggregations

SubscriptionsFilter (de.danoeh.antennapod.core.feed.SubscriptionsFilter)3 Cursor (android.database.Cursor)1 TextUtils (android.text.TextUtils)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 LinearLayout (android.widget.LinearLayout)1 RadioButton (android.widget.RadioButton)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 ArrayMap (androidx.collection.ArrayMap)1 SubscriptionsFilterGroup (de.danoeh.antennapod.core.feed.SubscriptionsFilterGroup)1 UserPreferences (de.danoeh.antennapod.core.preferences.UserPreferences)1 DownloadStatus (de.danoeh.antennapod.core.service.download.DownloadStatus)1 ChapterCursorMapper (de.danoeh.antennapod.core.storage.mapper.ChapterCursorMapper)1 FeedCursorMapper (de.danoeh.antennapod.core.storage.mapper.FeedCursorMapper)1 FeedItemCursorMapper (de.danoeh.antennapod.core.storage.mapper.FeedItemCursorMapper)1 FeedMediaCursorMapper (de.danoeh.antennapod.core.storage.mapper.FeedMediaCursorMapper)1 FeedPreferencesCursorMapper (de.danoeh.antennapod.core.storage.mapper.FeedPreferencesCursorMapper)1