2019-10-15 09:31:24 +02:00
|
|
|
//
|
|
|
|
// FeedlySetStarredArticlesOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 14/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
protocol FeedlyStarredEntryIdProviding {
|
|
|
|
var entryIds: Set<String> { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Single responsibility is to associate a starred status for ingested and remote
|
|
|
|
/// articles identfied by the provided identifiers *for the entire account.*
|
2019-10-15 09:31:24 +02:00
|
|
|
final class FeedlySetStarredArticlesOperation: FeedlyOperation {
|
|
|
|
private let account: Account
|
2019-10-18 23:21:02 +02:00
|
|
|
private let allStarredEntryIdsProvider: FeedlyStarredEntryIdProviding
|
2019-10-15 09:31:24 +02:00
|
|
|
private let log: OSLog
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
init(account: Account, allStarredEntryIdsProvider: FeedlyStarredEntryIdProviding, log: OSLog) {
|
2019-10-15 09:31:24 +02:00
|
|
|
self.account = account
|
2019-10-18 23:21:02 +02:00
|
|
|
self.allStarredEntryIdsProvider = allStarredEntryIdsProvider
|
2019-10-15 09:31:24 +02:00
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
override func main() {
|
|
|
|
guard !isCancelled else {
|
2019-11-06 01:22:10 +01:00
|
|
|
didFinish()
|
2019-10-15 09:31:24 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-17 00:55:37 +01:00
|
|
|
|
|
|
|
account.fetchStarredArticleIDs { (articleIDsResult) in
|
|
|
|
if let localStarredArticleIDs = try? articleIDsResult.get() {
|
2019-12-17 02:03:05 +01:00
|
|
|
self.processStarredArticleIDs(localStarredArticleIDs)
|
2019-12-17 00:55:37 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.didFinish()
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 08:53:36 +01:00
|
|
|
}
|
2019-12-17 00:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension FeedlySetStarredArticlesOperation {
|
|
|
|
|
2019-12-17 02:03:05 +01:00
|
|
|
func processStarredArticleIDs(_ localStarredArticleIDs: Set<String>) {
|
2019-12-09 08:53:36 +01:00
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
2019-10-15 09:31:24 +02:00
|
|
|
|
2019-12-17 02:03:05 +01:00
|
|
|
// Mark as starred
|
|
|
|
let remoteStarredArticleIDs = allStarredEntryIdsProvider.entryIds
|
|
|
|
account.mark(articleIDs: remoteStarredArticleIDs, statusKey: .starred, flag: true)
|
2019-12-08 07:23:44 +01:00
|
|
|
|
2019-12-17 02:03:05 +01:00
|
|
|
// Mark as unstarred
|
|
|
|
let deltaUnstarredArticleIDs = localStarredArticleIDs.subtracting(remoteStarredArticleIDs)
|
|
|
|
account.mark(articleIDs: deltaUnstarredArticleIDs, statusKey: .starred, flag: false)
|
2019-12-09 08:53:36 +01:00
|
|
|
|
2019-12-17 02:03:05 +01:00
|
|
|
didFinish()
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
}
|