From e31e1a4d60aae078da3a72fc0e75836d93f917a0 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 14 May 2019 10:06:29 -0500 Subject: [PATCH] Save article statuses from articles we have not received yet --- Frameworks/Account/Account.swift | 4 ++++ .../Feedbin/FeedbinAccountDelegate.swift | 22 +++++++++++++++++++ .../ArticlesDatabase/ArticlesDatabase.swift | 4 ++++ .../ArticlesDatabase/ArticlesTable.swift | 10 +++++++++ 4 files changed, 40 insertions(+) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 196b189cd..5174e9ca0 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -617,6 +617,10 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } + func ensureStatuses(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool) { + database.ensureStatuses(articleIDs, statusKey, flag) + } + // MARK: - Container public func flattenedFeeds() -> Set { diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index 8ca6128df..426db169b 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -1020,17 +1020,28 @@ private extension FeedbinAccountDelegate { let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } ) let currentUnreadArticleIDs = account.fetchUnreadArticleIDs() + // Mark articles as unread let deltaUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs) let markUnreadArticles = account.fetchArticles(forArticleIDs: deltaUnreadArticleIDs) DispatchQueue.main.async { _ = account.markArticles(markUnreadArticles, statusKey: .read, flag: false) } + // Mark articles as read let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(feedbinUnreadArticleIDs) let markReadArticles = account.fetchArticles(forArticleIDs: deltaReadArticleIDs) DispatchQueue.main.async { _ = account.markArticles(markReadArticles, statusKey: .read, flag: true) } + + // Save any unread statuses for articles we haven't yet received + let markUnreadArticleIDs = Set(markUnreadArticles.map { $0.articleID }) + let missingUnreadArticleIDs = deltaUnreadArticleIDs.subtracting(markUnreadArticleIDs) + if !missingUnreadArticleIDs.isEmpty { + DispatchQueue.main.async { + account.ensureStatuses(missingUnreadArticleIDs, .read, false) + } + } } @@ -1043,18 +1054,29 @@ private extension FeedbinAccountDelegate { let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } ) let currentStarredArticleIDs = account.fetchStarredArticleIDs() + // Mark articles as starred let deltaStarredArticleIDs = feedbinStarredArticleIDs.subtracting(currentStarredArticleIDs) let markStarredArticles = account.fetchArticles(forArticleIDs: deltaStarredArticleIDs) DispatchQueue.main.async { _ = account.markArticles(markStarredArticles, statusKey: .starred, flag: true) } + // Mark articles as unstarred let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(feedbinStarredArticleIDs) let markUnstarredArticles = account.fetchArticles(forArticleIDs: deltaUnstarredArticleIDs) DispatchQueue.main.async { _ = account.markArticles(markUnstarredArticles, statusKey: .starred, flag: false) } + // Save any starred statuses for articles we haven't yet received + let markStarredArticleIDs = Set(markStarredArticles.map { $0.articleID }) + let missingStarredArticleIDs = deltaStarredArticleIDs.subtracting(markStarredArticleIDs) + if !missingStarredArticleIDs.isEmpty { + DispatchQueue.main.async { + account.ensureStatuses(missingStarredArticleIDs, .starred, true) + } + } + } } diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index cb6a3af31..e380c0ed2 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -98,6 +98,10 @@ public final class ArticlesDatabase { return articlesTable.update(feedID, parsedItems, defaultRead, completion) } + public func ensureStatuses(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool) { + articlesTable.ensureStatuses(articleIDs, statusKey, flag) + } + // MARK: - Status public func fetchUnreadArticleIDs() -> Set { diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 6e5f00f28..b337f6b1f 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -185,6 +185,16 @@ final class ArticlesTable: DatabaseTable { } } + func ensureStatuses(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool) { + + self.queue.update { (database) in + let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, false, database) + let statuses = Set(statusesDictionary.values) + _ = self.statusesTable.mark(statuses, statusKey, flag) + } + + } + // MARK: Unread Counts func fetchUnreadCounts(_ feedIDs: Set, _ completion: @escaping UnreadCountCompletionBlock) {