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 471651e88..415af83d5 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/FeedItemlistFragment.java @@ -34,6 +34,7 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import java.util.Collections; import java.util.List; import de.danoeh.antennapod.R; @@ -52,6 +53,7 @@ import de.danoeh.antennapod.core.feed.FeedEvent; 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.feed.IntraFeedSortOrder; import de.danoeh.antennapod.core.glide.ApGlideSettings; import de.danoeh.antennapod.core.glide.FastBlurTransformation; import de.danoeh.antennapod.core.service.download.DownloadService; @@ -63,6 +65,7 @@ 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.core.util.Optional; +import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator; import de.danoeh.antennapod.core.util.gui.MoreContentListFooterUtil; import de.danoeh.antennapod.dialog.EpisodesApplyActionFragment; import de.danoeh.antennapod.dialog.RenameFeedDialog; @@ -625,13 +628,26 @@ 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()); FeedItemFilter filter = feed.getItemFilter(); feed.setItems(filter.filter(feed.getItems())); } + IntraFeedSortOrder sortOrder = feed.getSortOrder(); + if (sortOrder != null) { + long tS = System.currentTimeMillis(); // TODO-2524: + if (sortOrder == IntraFeedSortOrder.DATE_OLD_NEW) { + List feedItems = feed.getItems(); + Collections.sort(feedItems, FeedItemPubdateComparator.ascending); + feed.setItems(feedItems); + } else { + System.err.println("DBG - to-implement: " + sortOrder); // TODO-2524: + } + long tE = System.currentTimeMillis(); + System.err.println("DBG - sort elapsed time: " + (tE -tS) + + "ms ; " + "num items: " + feed.getItems().size() + ", order: " + sortOrder); // TODO: 2524 + } return Optional.ofNullable(feed); } 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 5e0d65d0c..53d95bd1c 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/FeedMenuHandler.java @@ -122,7 +122,7 @@ public class FeedMenuHandler { @Override protected void updateSort(@NonNull IntraFeedSortOrder sortOrder) { selectedFeed.setSortOrder(sortOrder); - // TODO-2524: update in db + DBWriter.setFeedItemSortOrder(selectedFeed.getId(), sortOrder); } }; sortDialog.openDialog(); diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedEvent.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedEvent.java index b790faadf..15cdf92dc 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedEvent.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedEvent.java @@ -6,7 +6,8 @@ import org.apache.commons.lang3.builder.ToStringStyle; public class FeedEvent { public enum Action { - FILTER_CHANGED + FILTER_CHANGED, + SORT_ORDER_CHANGED } private final Action action; diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index e0e15c1ff..9dd7b826c 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -5,6 +5,7 @@ import android.content.Context; import android.util.Log; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.greenrobot.eventbus.EventBus; @@ -30,6 +31,7 @@ import de.danoeh.antennapod.core.feed.FeedEvent; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedPreferences; +import de.danoeh.antennapod.core.feed.IntraFeedSortOrder; import de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction; import de.danoeh.antennapod.core.preferences.GpodnetPreferences; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; @@ -944,6 +946,19 @@ public class DBWriter { }); } + /** + * Set item sort order of the feed + * + */ + public static Future setFeedItemSortOrder(long feedId, @Nullable IntraFeedSortOrder sortOrder) { + return dbExec.submit(() -> { + PodDBAdapter adapter = PodDBAdapter.getInstance(); + adapter.open(); + // adapter.setFeedItemSortOrder(feedId, sortOrder); + adapter.close(); + EventBus.getDefault().post(new FeedEvent(FeedEvent.Action.SORT_ORDER_CHANGED, feedId)); + }); + } /** * Reset the statistics in DB diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/comparator/FeedItemPubdateComparator.java b/core/src/main/java/de/danoeh/antennapod/core/util/comparator/FeedItemPubdateComparator.java index a96eda3c5..9abbeb6c0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/comparator/FeedItemPubdateComparator.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/comparator/FeedItemPubdateComparator.java @@ -1,5 +1,6 @@ package de.danoeh.antennapod.core.util.comparator; +import java.util.Collections; import java.util.Comparator; import de.danoeh.antennapod.core.feed.FeedItem; @@ -7,7 +8,10 @@ import de.danoeh.antennapod.core.feed.FeedItem; /** Compares the pubDate of two FeedItems for sorting*/ public class FeedItemPubdateComparator implements Comparator { - /** Returns a new instance of this comparator in reverse order. + public static final Comparator descending = new FeedItemPubdateComparator(); + public static final Comparator ascending = Collections.reverseOrder(new FeedItemPubdateComparator()); + + /** Returns a new instance of this comparator in reverse order. public static FeedItemPubdateComparator newInstance() { FeedItemPubdateComparator }*/