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-09 08:53:36 +01:00
|
|
|
account.fetchUnreadArticleIDs(didFetchUnreadArticleIDs(_:))
|
|
|
|
}
|
|
|
|
|
|
|
|
private func didFetchUnreadArticleIDs(_ localUnreadArticleIds: Set<String>) {
|
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
2019-11-06 03:20:48 +01:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
let group = DispatchGroup()
|
2019-10-18 23:21:02 +02:00
|
|
|
let remoteUnreadArticleIds = allUnreadIdsProvider.entryIds
|
2019-12-09 08:53:36 +01:00
|
|
|
|
|
|
|
// Mark articles as unread
|
|
|
|
let deltaUnreadArticleIds = remoteUnreadArticleIds.subtracting(localUnreadArticleIds)
|
|
|
|
let markUnreadArticles = account.fetchArticles(.articleIDs(deltaUnreadArticleIds))
|
|
|
|
account.update(markUnreadArticles, statusKey: .read, flag: false)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
// Save any unread statuses for articles we haven't yet received
|
|
|
|
let markUnreadArticleIDs = Set(markUnreadArticles.map { $0.articleID })
|
|
|
|
let missingUnreadArticleIDs = deltaUnreadArticleIds.subtracting(markUnreadArticleIDs)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
group.enter()
|
|
|
|
account.ensureStatuses(missingUnreadArticleIDs, true, .read, false) {
|
|
|
|
group.leave()
|
|
|
|
}
|
2019-12-08 05:57:23 +01:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
// Mark articles as read
|
|
|
|
let deltaReadArticleIds = localUnreadArticleIds.subtracting(remoteUnreadArticleIds)
|
|
|
|
let markReadArticles = account.fetchArticles(.articleIDs(deltaReadArticleIds))
|
|
|
|
account.update(markReadArticles, statusKey: .read, flag: true)
|
2019-12-08 05:57:23 +01:00
|
|
|
|
2019-12-09 08:53:36 +01:00
|
|
|
// 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()
|
2019-11-06 03:20:48 +01:00
|
|
|
}
|
2019-12-09 08:53:36 +01:00
|
|
|
|
|
|
|
group.notify(queue: .main, execute: didFinish)
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
}
|