mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2024-12-12 09:26:26 +01:00
63 lines
1.6 KiB
Swift
63 lines
1.6 KiB
Swift
//
|
|
// FeedlySetUnreadArticlesOperation.swift
|
|
// Account
|
|
//
|
|
// Created by Kiel Gillard on 25/9/19.
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import os.log
|
|
|
|
protocol FeedlyUnreadEntryIdProviding {
|
|
var entryIds: Set<String> { get }
|
|
}
|
|
|
|
/// Single responsibility is to associate a read status for ingested and remote articles
|
|
/// where the provided article identifers identify the unread articles *for the entire account.*
|
|
final class FeedlySetUnreadArticlesOperation: FeedlyOperation {
|
|
private let account: Account
|
|
private let allUnreadIdsProvider: FeedlyUnreadEntryIdProviding
|
|
private let log: OSLog
|
|
|
|
init(account: Account, allUnreadIdsProvider: FeedlyUnreadEntryIdProviding, log: OSLog) {
|
|
self.account = account
|
|
self.allUnreadIdsProvider = allUnreadIdsProvider
|
|
self.log = log
|
|
}
|
|
|
|
override func main() {
|
|
guard !isCancelled else {
|
|
didFinish()
|
|
return
|
|
}
|
|
|
|
account.fetchUnreadArticleIDs { articleIDsResult in
|
|
if let localUnreadArticleIDs = try? articleIDsResult.get() {
|
|
self.processUnreadArticleIDs(localUnreadArticleIDs)
|
|
}
|
|
else {
|
|
self.didFinish()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension FeedlySetUnreadArticlesOperation {
|
|
|
|
private func processUnreadArticleIDs(_ localUnreadArticleIDs: Set<String>) {
|
|
guard !isCancelled else {
|
|
didFinish()
|
|
return
|
|
}
|
|
|
|
let remoteUnreadArticleIDs = allUnreadIdsProvider.entryIds
|
|
account.markAsUnread(remoteUnreadArticleIDs)
|
|
|
|
let articleIDsToMarkRead = localUnreadArticleIDs.subtracting(remoteUnreadArticleIDs)
|
|
account.markAsRead(articleIDsToMarkRead)
|
|
|
|
didFinish()
|
|
}
|
|
}
|