From 659eeaa48b874e65dcc0b74884fd3ffad8dabb3c Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Thu, 7 Feb 2019 14:32:49 +0000 Subject: [PATCH] Add items sorting before insertion --- .../com/readrops/app/LocalFeedRepository.java | 27 ++++++++++++++++--- .../com/readrops/app/{ => utils}/Utils.java | 2 +- 2 files changed, 24 insertions(+), 5 deletions(-) rename app/src/main/java/com/readrops/app/{ => utils}/Utils.java (95%) diff --git a/app/src/main/java/com/readrops/app/LocalFeedRepository.java b/app/src/main/java/com/readrops/app/LocalFeedRepository.java index f136d12e..5ecc2207 100644 --- a/app/src/main/java/com/readrops/app/LocalFeedRepository.java +++ b/app/src/main/java/com/readrops/app/LocalFeedRepository.java @@ -22,12 +22,16 @@ import com.readrops.readropslibrary.localfeed.atom.ATOMFeed; import com.readrops.readropslibrary.localfeed.json.JSONFeed; import com.readrops.readropslibrary.localfeed.rss.RSSFeed; +import org.joda.time.LocalDateTime; import org.jsoup.Jsoup; import java.io.IOException; import java.io.InputStream; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.TreeMap; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -134,7 +138,12 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { database.feedDao().updateHeaders(rssFeed.getEtag(), rssFeed.getLastModified(), dbFeed.getId()); List dbItems = Item.itemsFromRSS(rssFeed.getChannel().getItems(), dbFeed); - insertItems(dbItems, dbFeed); + TreeMap sortedItems = new TreeMap<>(LocalDateTime::compareTo); + for (Item item : dbItems) { + sortedItems.put(item.getPubDate(), item); + } + + insertItems(sortedItems.values(), dbFeed); } catch (Exception e) { failureCallBackInMainThread(e); @@ -154,7 +163,12 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { database.feedDao().updateHeaders(feed.getEtag(), feed.getLastModified(), dbFeed.getId()); List dbItems = Item.itemsFromATOM(feed.getEntries(), dbFeed); - insertItems(dbItems, dbFeed); + TreeMap sortedItems = new TreeMap<>(LocalDateTime::compareTo); + for (Item item : dbItems) { + sortedItems.put(item.getPubDate(), item); + } + + insertItems(sortedItems.values(), dbFeed); } catch (Exception e) { failureCallBackInMainThread(e); @@ -173,14 +187,19 @@ public class LocalFeedRepository extends ARepository implements QueryCallback { database.feedDao().updateHeaders(feed.getEtag(), feed.getLastModified(), dbFeed.getId()); List dbItems = Item.itemsFromJSON(feed.getItems(), dbFeed); - insertItems(dbItems, dbFeed); + TreeMap sortedItems = new TreeMap<>(LocalDateTime::compareTo); + for (Item item : dbItems) { + sortedItems.put(item.getPubDate(), item); + } + + insertItems(sortedItems.values(), dbFeed); } catch (Exception e) { failureCallBackInMainThread(e); } } - private void insertItems(List items, Feed feed) { + private void insertItems(Collection items, Feed feed) { for (Item dbItem : items) { if (!Boolean.valueOf(database.itemDao().guidExist(dbItem.getGuid()))) { if (dbItem.getDescription() != null) { diff --git a/app/src/main/java/com/readrops/app/Utils.java b/app/src/main/java/com/readrops/app/utils/Utils.java similarity index 95% rename from app/src/main/java/com/readrops/app/Utils.java rename to app/src/main/java/com/readrops/app/utils/Utils.java index 63d34bdf..bec47a15 100644 --- a/app/src/main/java/com/readrops/app/Utils.java +++ b/app/src/main/java/com/readrops/app/utils/Utils.java @@ -1,4 +1,4 @@ -package com.readrops.app; +package com.readrops.app.utils; import android.content.Context; import android.os.Handler;