diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 016b6c446..042298e28 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -126,6 +126,7 @@ public class UserPreferences { public static final int FEED_ORDER_COUNTER = 0; public static final int FEED_ORDER_ALPHABETICAL = 1; public static final int FEED_ORDER_LAST_UPDATE = 2; + public static final int FEED_ORDER_MOST_PLAYED = 3; public static final int FEED_COUNTER_SHOW_NEW_UNPLAYED_SUM = 0; public static final int FEED_COUNTER_SHOW_NEW = 1; public static final int FEED_COUNTER_SHOW_UNPLAYED = 2; diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java index 1d57d902c..7bda08b7a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBReader.java @@ -1080,6 +1080,21 @@ public final class DBReader { return t1.compareToIgnoreCase(t2); } }; + } else if(feedOrder == UserPreferences.FEED_ORDER_MOST_PLAYED) { + final LongIntMap playedCounters = adapter.getPlayedEpisodesCounters(feedIds); + + 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 { comparator = (lhs, rhs) -> { if(lhs.getItems() == null || lhs.getItems().size() == 0) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java index 111770dab..2d0b5c56a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/PodDBAdapter.java @@ -1463,7 +1463,10 @@ public class PodDBAdapter { default: // NONE return new LongIntMap(0); } + return conditionalFeedCounterRead(whereRead, feedIds); + } + private LongIntMap conditionalFeedCounterRead(String whereRead, long... feedIds) { // work around TextUtils.join wanting only boxed items // and StringUtils.join() causing NoSuchMethodErrors on MIUI StringBuilder builder = new StringBuilder(); @@ -1496,6 +1499,11 @@ public class PodDBAdapter { return result; } + public final LongIntMap getPlayedEpisodesCounters(long... feedIds) { + String whereRead = KEY_READ + "=" + FeedItem.PLAYED; + return conditionalFeedCounterRead(whereRead, feedIds); + } + public final int getNumberOfDownloadedEpisodes() { final String query = "SELECT COUNT(DISTINCT " + KEY_ID + ") AS count FROM " + TABLE_NAME_FEED_MEDIA + " WHERE " + KEY_DOWNLOADED + " > 0"; diff --git a/core/src/main/res/values/arrays.xml b/core/src/main/res/values/arrays.xml index 133a3ed6e..45650495c 100644 --- a/core/src/main/res/values/arrays.xml +++ b/core/src/main/res/values/arrays.xml @@ -157,11 +157,13 @@ @string/drawer_feed_order_unplayed_episodes @string/drawer_feed_order_alphabetical @string/drawer_feed_order_last_update + @string/drawer_feed_order_most_played 0 1 2 + 3 diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index d9b474de6..328dd34e8 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -39,6 +39,7 @@ Sort by counter Sort alphabetically Sort by publication date + Sort by number of played episodes Number of new and unplayed episodes Number of new episodes Number of unplayed episodes