diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index a3bd9fd1c..62d63c01d 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -781,6 +781,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, // TODO: https://github.com/brentsimmons/NetNewsWire/issues/1420 } + /// Update statuses specified by articleIDs — set a key and value. + /// This updates the database, and sends a .StatusesDidChange notification. + /// Any statuses that don’t exist will be automatically created. + func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping DatabaseCompletionBlock? = nil) { + // TODO + } + /// Fetch statuses for the specified articleIDs. The completion handler will get nil if the app is suspended. /// To update the properties in the database, call the update method that takes Set as first parameter. func fetchStatuses(articleIDs: Set, createIfNeeded: Bool, completion: @escaping ArticleStatusesResultBlock) { diff --git a/Frameworks/Account/Feedly/Operations/FeedlySetUnreadArticlesOperation.swift b/Frameworks/Account/Feedly/Operations/FeedlySetUnreadArticlesOperation.swift index 3dcc72326..97e745214 100644 --- a/Frameworks/Account/Feedly/Operations/FeedlySetUnreadArticlesOperation.swift +++ b/Frameworks/Account/Feedly/Operations/FeedlySetUnreadArticlesOperation.swift @@ -31,46 +31,35 @@ final class FeedlySetUnreadArticlesOperation: FeedlyOperation { didFinish() return } - - account.fetchUnreadArticleIDs(didFetchUnreadArticleIDs(_:)) + + account.fetchUnreadArticleIDs { articleIDsResult in + if let localUnreadArticleIDs = try? articleIDsResult.get() { + self.processUnreadArticleIDs(localUnreadArticleIDs) + } + else { + self.didFinish() + } + } } - - private func didFetchUnreadArticleIDs(_ localUnreadArticleIds: Set) { +} + +private extension FeedlySetUnreadArticlesOperation { + + private func processUnreadArticleIDs(_ localUnreadArticleIDs: Set) { guard !isCancelled else { didFinish() return } - - let group = DispatchGroup() - let remoteUnreadArticleIds = allUnreadIdsProvider.entryIds - + + let remoteUnreadArticleIDs = allUnreadIdsProvider.entryIds + // Mark articles as unread - let deltaUnreadArticleIds = remoteUnreadArticleIds.subtracting(localUnreadArticleIds) - let markUnreadArticles = account.fetchArticles(.articleIDs(deltaUnreadArticleIds)) - account.update(markUnreadArticles, statusKey: .read, flag: false) - - // Save any unread statuses for articles we haven't yet received - let markUnreadArticleIDs = Set(markUnreadArticles.map { $0.articleID }) - let missingUnreadArticleIDs = deltaUnreadArticleIds.subtracting(markUnreadArticleIDs) - - group.enter() - account.ensureStatuses(missingUnreadArticleIDs, true, .read, false) { - group.leave() - } + account.mark(articleIDs: remoteUnreadArticleIDs, statusKey: .read, flag: false) // Mark articles as read - let deltaReadArticleIds = localUnreadArticleIds.subtracting(remoteUnreadArticleIds) - let markReadArticles = account.fetchArticles(.articleIDs(deltaReadArticleIds)) - account.update(markReadArticles, statusKey: .read, flag: true) + let articleIDsToMarkRead = localUnreadArticleIDs.subtracting(remoteUnreadArticleIDs) + account.mark(articleIDs: articleIDsToMarkRead, statusKey: .read, flag: true) - // Save any read statuses for articles we haven't yet received - let markReadArticleIDs = Set(markReadArticles.map { $0.articleID }) - let missingReadArticleIDs = deltaReadArticleIds.subtracting(markReadArticleIDs) - group.enter() - account.ensureStatuses(missingReadArticleIDs, true, .read, true) { - group.leave() - } - - group.notify(queue: .main, execute: didFinish) + didFinish() } }