2019-10-18 23:21:02 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
2019-12-17 01:26:35 +01:00
|
|
|
|
|
|
|
account.fetchUnreadArticleIDs { articleIDsResult in
|
|
|
|
if let localUnreadArticleIDs = try? articleIDsResult.get() {
|
|
|
|
self.processUnreadArticleIDs(localUnreadArticleIDs)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.didFinish()
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 08:53:36 +01:00
|
|
|
}
|
2019-12-17 01:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension FeedlySetUnreadArticlesOperation {
|
|
|
|
|
|
|
|
private func processUnreadArticleIDs(_ localUnreadArticleIDs: Set<String>) {
|
2019-12-09 08:53:36 +01:00
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2019-12-17 01:26:35 +01:00
|
|
|
let remoteUnreadArticleIDs = allUnreadIdsProvider.entryIds
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2019-12-17 01:26:35 +01:00
|
|
|
// Mark articles as unread
|
|
|
|
account.mark(articleIDs: remoteUnreadArticleIDs, statusKey: .read, flag: false)
|
2019-12-08 05:57:23 +01:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
// Mark articles as read
|
2019-12-17 01:26:35 +01:00
|
|
|
let articleIDsToMarkRead = localUnreadArticleIDs.subtracting(remoteUnreadArticleIDs)
|
|
|
|
account.mark(articleIDs: articleIDsToMarkRead, statusKey: .read, flag: true)
|
2019-12-09 08:53:36 +01:00
|
|
|
|
2019-12-17 01:26:35 +01:00
|
|
|
didFinish()
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|