Simplify FeedlySetUnreadArticlesOperation.
This commit is contained in:
parent
67f65545b3
commit
3883d24ebe
|
@ -781,6 +781,13 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
|
||||||
// TODO: https://github.com/brentsimmons/NetNewsWire/issues/1420
|
// 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<String>, 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.
|
/// 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<ArticleStatus> as first parameter.
|
/// To update the properties in the database, call the update method that takes Set<ArticleStatus> as first parameter.
|
||||||
func fetchStatuses(articleIDs: Set<String>, createIfNeeded: Bool, completion: @escaping ArticleStatusesResultBlock) {
|
func fetchStatuses(articleIDs: Set<String>, createIfNeeded: Bool, completion: @escaping ArticleStatusesResultBlock) {
|
||||||
|
|
|
@ -31,46 +31,35 @@ final class FeedlySetUnreadArticlesOperation: FeedlyOperation {
|
||||||
didFinish()
|
didFinish()
|
||||||
return
|
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<String>) {
|
|
||||||
|
private extension FeedlySetUnreadArticlesOperation {
|
||||||
|
|
||||||
|
private func processUnreadArticleIDs(_ localUnreadArticleIDs: Set<String>) {
|
||||||
guard !isCancelled else {
|
guard !isCancelled else {
|
||||||
didFinish()
|
didFinish()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let group = DispatchGroup()
|
let remoteUnreadArticleIDs = allUnreadIdsProvider.entryIds
|
||||||
let remoteUnreadArticleIds = allUnreadIdsProvider.entryIds
|
|
||||||
|
|
||||||
// Mark articles as unread
|
// Mark articles as unread
|
||||||
let deltaUnreadArticleIds = remoteUnreadArticleIds.subtracting(localUnreadArticleIds)
|
account.mark(articleIDs: remoteUnreadArticleIDs, statusKey: .read, flag: false)
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark articles as read
|
// Mark articles as read
|
||||||
let deltaReadArticleIds = localUnreadArticleIds.subtracting(remoteUnreadArticleIds)
|
let articleIDsToMarkRead = localUnreadArticleIDs.subtracting(remoteUnreadArticleIDs)
|
||||||
let markReadArticles = account.fetchArticles(.articleIDs(deltaReadArticleIds))
|
account.mark(articleIDs: articleIDsToMarkRead, statusKey: .read, flag: true)
|
||||||
account.update(markReadArticles, statusKey: .read, flag: true)
|
|
||||||
|
|
||||||
// Save any read statuses for articles we haven't yet received
|
didFinish()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue