2020-03-18 21:48:44 +01:00
|
|
|
//
|
|
|
|
// CloudKitAppDelegate.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 3/18/20.
|
|
|
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-03-22 22:35:03 +01:00
|
|
|
import CloudKit
|
2020-04-11 23:45:31 +02:00
|
|
|
import SystemConfiguration
|
2020-03-22 22:35:03 +01:00
|
|
|
import os.log
|
|
|
|
import SyncDatabase
|
2020-03-18 21:48:44 +01:00
|
|
|
import RSCore
|
|
|
|
import RSParser
|
|
|
|
import Articles
|
2020-04-11 00:23:39 +02:00
|
|
|
import ArticlesDatabase
|
2020-03-18 21:48:44 +01:00
|
|
|
import RSWeb
|
2020-04-10 04:07:56 +02:00
|
|
|
import Secrets
|
2020-03-18 21:48:44 +01:00
|
|
|
|
2020-05-02 17:02:58 +02:00
|
|
|
enum CloudKitAccountDelegateError: LocalizedError {
|
|
|
|
case invalidParameter
|
|
|
|
case unknown
|
|
|
|
|
|
|
|
var errorDescription: String? {
|
|
|
|
return NSLocalizedString("An unexpected CloudKit error occurred.", comment: "An unexpected CloudKit error occurred.")
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
final class CloudKitAccountDelegate: AccountDelegate {
|
|
|
|
|
2020-03-22 22:35:03 +01:00
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
|
|
|
|
|
|
|
|
private let database: SyncDatabase
|
|
|
|
|
|
|
|
private let container: CKContainer = {
|
|
|
|
let orgID = Bundle.main.object(forInfoDictionaryKey: "OrganizationIdentifier") as! String
|
|
|
|
return CKContainer(identifier: "iCloud.\(orgID).NetNewsWire")
|
|
|
|
}()
|
|
|
|
|
|
|
|
private let accountZone: CloudKitAccountZone
|
2020-04-01 18:46:37 +02:00
|
|
|
private let articlesZone: CloudKitArticlesZone
|
2020-03-22 22:35:03 +01:00
|
|
|
|
2020-05-02 17:02:58 +02:00
|
|
|
private let mainThreadOperationQueue = MainThreadOperationQueue()
|
|
|
|
|
2020-04-11 19:22:28 +02:00
|
|
|
private lazy var refresher: LocalAccountRefresher = {
|
2020-04-10 18:20:35 +02:00
|
|
|
let refresher = LocalAccountRefresher()
|
|
|
|
refresher.delegate = self
|
|
|
|
return refresher
|
|
|
|
}()
|
2020-03-22 22:35:03 +01:00
|
|
|
|
2020-04-25 22:09:03 +02:00
|
|
|
weak var account: Account?
|
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
let behaviors: AccountBehaviors = []
|
|
|
|
let isOPMLImportInProgress = false
|
|
|
|
|
|
|
|
let server: String? = nil
|
|
|
|
var credentials: Credentials?
|
|
|
|
var accountMetadata: AccountMetadata?
|
|
|
|
|
2020-04-02 19:00:10 +02:00
|
|
|
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
2020-04-28 19:31:53 +02:00
|
|
|
|
2020-03-22 22:35:03 +01:00
|
|
|
init(dataFolder: String) {
|
|
|
|
accountZone = CloudKitAccountZone(container: container)
|
2020-04-01 18:46:37 +02:00
|
|
|
articlesZone = CloudKitArticlesZone(container: container)
|
|
|
|
|
2020-03-22 22:35:03 +01:00
|
|
|
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("Sync.sqlite3")
|
|
|
|
database = SyncDatabase(databaseFilePath: databaseFilePath)
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:48:25 +02:00
|
|
|
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
|
2020-05-02 17:02:58 +02:00
|
|
|
let op = CloudKitRemoteNotificationOperation(accountZone: accountZone, articlesZone: articlesZone, userInfo: userInfo)
|
|
|
|
op.completionBlock = { mainThreadOperaion in
|
2020-03-30 09:48:25 +02:00
|
|
|
completion()
|
|
|
|
}
|
2020-05-02 17:02:58 +02:00
|
|
|
mainThreadOperationQueue.add(op)
|
2020-03-30 09:48:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-05 22:23:11 +02:00
|
|
|
guard refreshProgress.isComplete else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
2020-04-10 18:20:35 +02:00
|
|
|
|
2020-04-11 23:45:31 +02:00
|
|
|
let reachability = SCNetworkReachabilityCreateWithName(nil, "apple.com")
|
|
|
|
var flags = SCNetworkReachabilityFlags()
|
|
|
|
guard SCNetworkReachabilityGetFlags(reachability!, &flags), flags.contains(.reachable) else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:59:41 +02:00
|
|
|
standardRefreshAll(for: account, completion: completion)
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2020-05-01 00:42:56 +02:00
|
|
|
sendArticleStatus(for: account, showProgress: false, completion: completion)
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2020-05-02 17:02:58 +02:00
|
|
|
let op = CloudKitReceiveStatusOperation(articlesZone: articlesZone)
|
|
|
|
op.completionBlock = { mainThreadOperaion in
|
|
|
|
if mainThreadOperaion.isCanceled {
|
|
|
|
completion(.failure(CloudKitAccountDelegateError.unknown))
|
|
|
|
} else {
|
2020-04-01 18:46:37 +02:00
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 17:02:58 +02:00
|
|
|
mainThreadOperationQueue.add(op)
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func importOPML(for account:Account, opmlFile: URL, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-11 00:23:39 +02:00
|
|
|
guard refreshProgress.isComplete else {
|
2020-04-10 18:20:35 +02:00
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
var fileData: Data?
|
|
|
|
|
|
|
|
do {
|
|
|
|
fileData = try Data(contentsOf: opmlFile)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let opmlData = fileData else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let parserData = ParserData(url: opmlFile.absoluteString, data: opmlData)
|
|
|
|
var opmlDocument: RSOPMLDocument?
|
|
|
|
|
|
|
|
do {
|
|
|
|
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
|
|
|
} catch {
|
|
|
|
completion(.failure(error))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let loadDocument = opmlDocument else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:10:35 +02:00
|
|
|
guard let opmlItems = loadDocument.children, let rootExternalID = account.externalID else {
|
2020-03-18 21:48:44 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:10:35 +02:00
|
|
|
let normalizedItems = OPMLNormalizer.normalize(opmlItems)
|
2020-03-18 21:48:44 +01:00
|
|
|
|
2020-05-02 21:39:28 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-04-05 22:05:08 +02:00
|
|
|
self.accountZone.importOPML(rootExternalID: rootExternalID, items: normalizedItems) { _ in
|
2020-05-02 21:39:28 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-04-26 18:26:41 +02:00
|
|
|
self.standardRefreshAll(for: account, completion: completion)
|
2020-04-05 00:35:09 +02:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func createWebFeed(for account: Account, url urlString: String, name: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2020-04-19 00:30:58 +02:00
|
|
|
guard let url = URL(string: urlString), let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
2020-03-18 21:48:44 +01:00
|
|
|
completion(.failure(LocalAccountDelegateError.invalidParameter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
let editedName = name == nil || name!.isEmpty ? nil : name
|
2020-03-29 10:43:20 +02:00
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
// Username should be part of the URL on new feed adds
|
2020-04-24 20:33:43 +02:00
|
|
|
if let feedProvider = FeedProviderManager.shared.best(for: urlComponents) {
|
2020-04-19 00:30:58 +02:00
|
|
|
createProviderWebFeed(for: account, urlComponents: urlComponents, editedName: editedName, container: container, feedProvider: feedProvider, completion: completion)
|
|
|
|
} else {
|
|
|
|
createRSSWebFeed(for: account, url: url, editedName: editedName, container: container, completion: completion)
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
2020-04-19 00:30:58 +02:00
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func renameWebFeed(for account: Account, with feed: WebFeed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-03-30 00:53:11 +02:00
|
|
|
let editedName = name.isEmpty ? nil : name
|
2020-04-02 19:00:10 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-03-30 00:53:11 +02:00
|
|
|
accountZone.renameWebFeed(feed, editedName: editedName) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-30 00:53:11 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
feed.editedName = name
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-30 00:53:11 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-05-11 00:10:44 +02:00
|
|
|
removeWebFeedFromCloud(for: account, with: feed, from: container) { result in
|
2020-03-29 15:52:59 +02:00
|
|
|
switch result {
|
2020-04-05 22:05:08 +02:00
|
|
|
case .success:
|
2020-05-11 00:10:44 +02:00
|
|
|
container.removeWebFeed(feed)
|
|
|
|
completion(.success(()))
|
2020-03-29 15:52:59 +02:00
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 10:30:53 +02:00
|
|
|
func moveWebFeed(for account: Account, with feed: WebFeed, from fromContainer: Container, to toContainer: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-02 19:00:10 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-03-31 10:30:53 +02:00
|
|
|
accountZone.moveWebFeed(feed, from: fromContainer, to: toContainer) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-31 10:30:53 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
fromContainer.removeWebFeed(feed)
|
|
|
|
toContainer.addWebFeed(feed)
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-31 10:30:53 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func addWebFeed(for account: Account, with feed: WebFeed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-02 19:00:10 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-03-31 10:30:53 +02:00
|
|
|
accountZone.addWebFeed(feed, to: container) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-31 10:30:53 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
container.addWebFeed(feed)
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-31 10:30:53 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreWebFeed(for account: Account, feed: WebFeed, container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-27 09:11:20 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(2)
|
2020-05-06 01:25:33 +02:00
|
|
|
accountZone.createWebFeed(url: feed.url, name: feed.name, editedName: feed.editedName, homePageURL: feed.homePageURL, container: container) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-31 18:07:54 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let externalID):
|
|
|
|
feed.externalID = externalID
|
|
|
|
container.addWebFeed(feed)
|
2020-04-27 08:04:38 +02:00
|
|
|
|
|
|
|
account.fetchArticlesAsync(.webFeed(feed)) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let articles):
|
2020-04-27 08:28:00 +02:00
|
|
|
self.articlesZone.saveNewArticles(articles) { result in
|
2020-04-27 09:11:20 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-04-27 08:28:00 +02:00
|
|
|
if case .failure(let error) = result {
|
|
|
|
os_log(.error, log: self.log, "Restore articles error: %@.", error.localizedDescription)
|
|
|
|
}
|
2020-04-27 09:18:49 +02:00
|
|
|
completion(.success(()))
|
2020-04-27 08:28:00 +02:00
|
|
|
}
|
2020-04-27 08:04:38 +02:00
|
|
|
case .failure(let error):
|
2020-04-27 09:11:20 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-27 08:04:38 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:07:54 +02:00
|
|
|
case .failure(let error):
|
2020-04-27 09:11:20 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-31 18:07:54 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
2020-03-30 20:35:02 +02:00
|
|
|
func createFolder(for account: Account, name: String, completion: @escaping (Result<Folder, Error>) -> Void) {
|
2020-04-02 19:00:10 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-03-30 22:15:45 +02:00
|
|
|
accountZone.createFolder(name: name) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-30 22:15:45 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let externalID):
|
|
|
|
if let folder = account.ensureFolder(with: name) {
|
|
|
|
folder.externalID = externalID
|
|
|
|
completion(.success(folder))
|
|
|
|
} else {
|
|
|
|
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-30 22:15:45 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameFolder(for account: Account, with folder: Folder, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-02 19:00:10 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1)
|
2020-03-30 22:15:45 +02:00
|
|
|
accountZone.renameFolder(folder, to: name) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-30 22:15:45 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
folder.name = name
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-30 22:15:45 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeFolder(for account: Account, with folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-05-10 23:59:23 +02:00
|
|
|
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(2)
|
|
|
|
accountZone.findWebFeedExternalIDs(for: folder) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-30 22:15:45 +02:00
|
|
|
switch result {
|
2020-05-10 23:59:23 +02:00
|
|
|
case .success(let webFeedExternalIDs):
|
|
|
|
|
|
|
|
let webFeeds = webFeedExternalIDs.compactMap { account.existingWebFeed(withExternalID: $0) }
|
|
|
|
let group = DispatchGroup()
|
|
|
|
var errorOccurred = false
|
|
|
|
|
|
|
|
for webFeed in webFeeds {
|
|
|
|
group.enter()
|
2020-05-11 00:10:44 +02:00
|
|
|
self.removeWebFeedFromCloud(for: account, with: webFeed, from: folder) { result in
|
2020-05-10 23:59:23 +02:00
|
|
|
group.leave()
|
|
|
|
if case .failure(let error) = result {
|
|
|
|
os_log(.error, log: self.log, "Remove folder, remove webfeed error: %@.", error.localizedDescription)
|
|
|
|
errorOccurred = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.global(qos: .background)) {
|
|
|
|
guard !errorOccurred else {
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
completion(.failure(CloudKitAccountDelegateError.unknown))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.accountZone.removeFolder(folder) { result in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
account.removeFolder(folder)
|
|
|
|
completion(.success(()))
|
|
|
|
case .failure(let error):
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:15:45 +02:00
|
|
|
case .failure(let error):
|
2020-05-10 23:59:23 +02:00
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.refreshProgress.completeTask()
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-30 22:15:45 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 23:59:23 +02:00
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-03-30 22:15:45 +02:00
|
|
|
guard let name = folder.name else {
|
|
|
|
completion(.failure(LocalAccountDelegateError.invalidParameter))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-02 19:00:10 +02:00
|
|
|
let feedsToRestore = folder.topLevelWebFeeds
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(1 + feedsToRestore.count)
|
|
|
|
|
2020-03-30 22:15:45 +02:00
|
|
|
accountZone.createFolder(name: name) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-30 22:15:45 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let externalID):
|
|
|
|
folder.externalID = externalID
|
|
|
|
account.addFolder(folder)
|
2020-03-31 18:07:54 +02:00
|
|
|
|
|
|
|
let group = DispatchGroup()
|
2020-04-02 19:00:10 +02:00
|
|
|
for feed in feedsToRestore {
|
2020-03-31 18:07:54 +02:00
|
|
|
|
|
|
|
folder.topLevelWebFeeds.remove(feed)
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
self.restoreWebFeed(for: account, feed: feed, container: folder) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-03-31 18:07:54 +02:00
|
|
|
group.leave()
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
account.addFolder(folder)
|
|
|
|
completion(.success(()))
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:15:45 +02:00
|
|
|
case .failure(let error):
|
2020-04-06 09:15:28 +02:00
|
|
|
self.processAccountError(account, error)
|
2020-03-30 22:15:45 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
2020-04-01 03:56:34 +02:00
|
|
|
let syncStatuses = articles.map { article in
|
2020-04-27 23:41:45 +02:00
|
|
|
return SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag)
|
2020-04-01 03:56:34 +02:00
|
|
|
}
|
2020-04-29 23:14:18 +02:00
|
|
|
|
|
|
|
try? database.insertStatuses(syncStatuses)
|
|
|
|
let articles = try? account.update(articles, statusKey: statusKey, flag: flag)
|
2020-04-01 03:56:34 +02:00
|
|
|
|
|
|
|
database.selectPendingCount { result in
|
|
|
|
if let count = try? result.get(), count > 100 {
|
2020-05-01 00:42:56 +02:00
|
|
|
self.sendArticleStatus(for: account, showProgress: false) { _ in }
|
2020-04-01 03:56:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 23:14:18 +02:00
|
|
|
return articles
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func accountDidInitialize(_ account: Account) {
|
2020-04-10 18:20:35 +02:00
|
|
|
self.account = account
|
|
|
|
|
2020-04-26 03:20:56 +02:00
|
|
|
accountZone.delegate = CloudKitAcountZoneDelegate(account: account, refreshProgress: refreshProgress, articlesZone: articlesZone)
|
|
|
|
articlesZone.delegate = CloudKitArticlesZoneDelegate(account: account, database: database, articlesZone: articlesZone)
|
2020-03-30 22:15:45 +02:00
|
|
|
|
2020-04-28 02:21:49 +02:00
|
|
|
database.resetAllSelectedForProcessing()
|
|
|
|
|
2020-04-04 20:33:49 +02:00
|
|
|
// Check to see if this is a new account and initialize anything we need
|
2020-03-30 22:15:45 +02:00
|
|
|
if account.externalID == nil {
|
|
|
|
accountZone.findOrCreateAccount() { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let externalID):
|
|
|
|
account.externalID = externalID
|
2020-04-13 11:59:41 +02:00
|
|
|
self.initialRefreshAll(for: account) { _ in }
|
2020-03-30 22:15:45 +02:00
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "Error adding account container: %@", error.localizedDescription)
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 17:02:58 +02:00
|
|
|
accountZone.subscribeToZoneChanges()
|
|
|
|
articlesZone.subscribeToZoneChanges()
|
2020-03-30 22:15:45 +02:00
|
|
|
}
|
|
|
|
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func accountWillBeDeleted(_ account: Account) {
|
2020-05-02 17:02:58 +02:00
|
|
|
accountZone.resetChangeToken()
|
|
|
|
articlesZone.resetChangeToken()
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL? = nil, completion: (Result<Credentials?, Error>) -> Void) {
|
|
|
|
return completion(.success(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Suspend and Resume (for iOS)
|
|
|
|
|
|
|
|
func suspendNetwork() {
|
|
|
|
refresher.suspend()
|
|
|
|
}
|
|
|
|
|
|
|
|
func suspendDatabase() {
|
2020-04-01 03:56:34 +02:00
|
|
|
database.suspend()
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resume() {
|
|
|
|
refresher.resume()
|
2020-04-01 03:56:34 +02:00
|
|
|
database.resume()
|
2020-03-18 21:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 19:00:10 +02:00
|
|
|
|
|
|
|
private extension CloudKitAccountDelegate {
|
|
|
|
|
2020-04-13 11:59:41 +02:00
|
|
|
func initialRefreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-02 19:00:10 +02:00
|
|
|
|
2020-04-13 11:59:41 +02:00
|
|
|
func fail(_ error: Error) {
|
|
|
|
self.processAccountError(account, error)
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
2020-04-02 19:00:10 +02:00
|
|
|
|
2020-04-29 18:07:24 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(3)
|
2020-04-26 18:26:41 +02:00
|
|
|
accountZone.fetchChangesInZone() { result in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
2020-04-26 18:37:12 +02:00
|
|
|
let webFeeds = account.flattenedWebFeeds()
|
|
|
|
self.refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count)
|
|
|
|
|
2020-04-13 11:59:41 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
2020-04-26 18:26:41 +02:00
|
|
|
self.refreshArticleStatus(for: account) { result in
|
|
|
|
self.refreshProgress.completeTask()
|
2020-04-13 11:59:41 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
2020-04-26 18:37:12 +02:00
|
|
|
|
|
|
|
self.combinedRefresh(account, webFeeds) {
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
account.metadata.lastArticleFetchEndTime = Date()
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:59:41 +02:00
|
|
|
case .failure(let error):
|
|
|
|
fail(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
fail(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func standardRefreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
|
|
|
|
let intialWebFeedsCount = account.flattenedWebFeeds().count
|
2020-04-30 06:56:50 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(3 + intialWebFeedsCount)
|
2020-04-02 19:00:10 +02:00
|
|
|
|
2020-04-10 18:20:35 +02:00
|
|
|
func fail(_ error: Error) {
|
|
|
|
self.processAccountError(account, error)
|
|
|
|
self.refreshProgress.clear()
|
2020-04-11 00:23:39 +02:00
|
|
|
completion(.failure(error))
|
2020-04-10 18:20:35 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 19:00:10 +02:00
|
|
|
accountZone.fetchChangesInZone() { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
|
2020-04-30 06:56:50 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-04-02 19:00:10 +02:00
|
|
|
let webFeeds = account.flattenedWebFeeds()
|
2020-04-13 11:59:41 +02:00
|
|
|
self.refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count - intialWebFeedsCount)
|
2020-04-30 06:56:50 +02:00
|
|
|
|
|
|
|
self.refreshArticleStatus(for: account) { result in
|
2020-04-02 19:00:10 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.refreshProgress.completeTask()
|
2020-04-30 06:56:50 +02:00
|
|
|
self.combinedRefresh(account, webFeeds) {
|
2020-05-08 08:48:35 +02:00
|
|
|
self.sendArticleStatus(for: account, showProgress: true) { _ in
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
account.metadata.lastArticleFetchEndTime = Date()
|
|
|
|
}
|
2020-04-02 19:00:10 +02:00
|
|
|
}
|
|
|
|
case .failure(let error):
|
2020-04-10 18:20:35 +02:00
|
|
|
fail(error)
|
2020-04-02 19:00:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-30 06:56:50 +02:00
|
|
|
|
2020-04-02 19:00:10 +02:00
|
|
|
case .failure(let error):
|
2020-04-10 18:20:35 +02:00
|
|
|
fail(error)
|
2020-04-02 19:00:10 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 02:13:57 +02:00
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
}
|
|
|
|
|
2020-04-26 03:20:56 +02:00
|
|
|
func combinedRefresh(_ account: Account, _ webFeeds: Set<WebFeed>, completion: @escaping () -> Void) {
|
|
|
|
|
|
|
|
var refresherWebFeeds = Set<WebFeed>()
|
|
|
|
let group = DispatchGroup()
|
2020-04-27 07:52:53 +02:00
|
|
|
|
2020-04-26 03:20:56 +02:00
|
|
|
for webFeed in webFeeds {
|
|
|
|
if let components = URLComponents(string: webFeed.url), let feedProvider = FeedProviderManager.shared.best(for: components) {
|
|
|
|
group.enter()
|
|
|
|
feedProvider.refresh(webFeed) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let parsedItems):
|
|
|
|
|
|
|
|
account.update(webFeed.webFeedID, with: parsedItems) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let articleChanges):
|
2020-05-28 23:24:10 +02:00
|
|
|
self.storeArticleChanges(new: articleChanges.newArticles, updated: articleChanges.updatedArticles, deleted: articleChanges.deletedArticles) {
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
group.leave()
|
|
|
|
}
|
2020-04-26 03:20:56 +02:00
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "CloudKit Feed refresh update error: %@.", error.localizedDescription)
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
os_log(.error, log: self.log, "CloudKit Feed refresh error: %@.", error.localizedDescription)
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
refresherWebFeeds.insert(webFeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
2020-05-08 09:32:13 +02:00
|
|
|
refresher.refreshFeeds(refresherWebFeeds) {
|
2020-04-29 23:14:18 +02:00
|
|
|
group.leave()
|
2020-04-26 03:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.main) {
|
2020-05-08 08:48:35 +02:00
|
|
|
completion()
|
2020-04-26 03:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
func createProviderWebFeed(for account: Account, urlComponents: URLComponents, editedName: String?, container: Container, feedProvider: FeedProvider, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
2020-04-27 07:52:53 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(6)
|
2020-04-19 00:30:58 +02:00
|
|
|
|
2020-05-06 00:55:27 +02:00
|
|
|
feedProvider.metaData(urlComponents) { result in
|
2020-04-19 00:30:58 +02:00
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
|
2020-05-06 00:55:27 +02:00
|
|
|
case .success(let metaData):
|
2020-04-19 00:30:58 +02:00
|
|
|
|
2020-04-24 20:33:43 +02:00
|
|
|
guard let urlString = urlComponents.url?.absoluteString else {
|
2020-04-19 00:30:58 +02:00
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
self.accountZone.createWebFeed(url: urlString, name: metaData.name, editedName: editedName, homePageURL: metaData.homePageURL, container: container) { result in
|
2020-04-19 00:30:58 +02:00
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
case .success(let externalID):
|
|
|
|
|
2020-05-06 00:55:27 +02:00
|
|
|
let feed = account.createWebFeed(with: metaData.name, url: urlString, webFeedID: urlString, homePageURL: metaData.homePageURL)
|
2020-04-19 00:30:58 +02:00
|
|
|
feed.editedName = editedName
|
|
|
|
feed.externalID = externalID
|
|
|
|
container.addWebFeed(feed)
|
|
|
|
|
|
|
|
feedProvider.refresh(feed) { result in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
case .success(let parsedItems):
|
2020-04-24 02:13:57 +02:00
|
|
|
|
2020-04-24 20:33:43 +02:00
|
|
|
account.update(urlString, with: parsedItems) { result in
|
2020-04-24 02:13:57 +02:00
|
|
|
switch result {
|
2020-04-27 08:17:15 +02:00
|
|
|
case .success:
|
2020-04-27 23:41:45 +02:00
|
|
|
self.sendNewArticlesToTheCloud(account, feed, completion: completion)
|
2020-04-24 02:13:57 +02:00
|
|
|
case .failure(let error):
|
2020-04-24 20:33:43 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-24 02:13:57 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
}
|
2020-04-24 02:13:57 +02:00
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
case .failure:
|
2020-04-24 20:33:43 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-19 00:30:58 +02:00
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case .failure(let error):
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:09:26 +02:00
|
|
|
case .failure:
|
2020-04-19 00:30:58 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-05-07 22:09:26 +02:00
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
2020-04-19 00:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRSSWebFeed(for account: Account, url: URL, editedName: String?, container: Container, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
|
|
|
BatchUpdate.shared.start()
|
2020-04-27 07:52:53 +02:00
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(6)
|
2020-04-19 00:30:58 +02:00
|
|
|
FeedFinder.find(url: url) { result in
|
|
|
|
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
case .success(let feedSpecifiers):
|
|
|
|
guard let bestFeedSpecifier = FeedSpecifier.bestFeed(in: feedSpecifiers), let url = URL(string: bestFeedSpecifier.urlString) else {
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.hasWebFeed(withURL: bestFeedSpecifier.urlString) {
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(AccountError.createErrorAlreadySubscribed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
let feed = account.createWebFeed(with: nil, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
|
|
|
|
feed.editedName = editedName
|
|
|
|
container.addWebFeed(feed)
|
2020-04-19 00:30:58 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
InitialFeedDownloader.download(url) { parsedFeed in
|
2020-04-19 00:30:58 +02:00
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
if let parsedFeed = parsedFeed {
|
|
|
|
account.update(feed, with: parsedFeed) { result in
|
|
|
|
switch result {
|
2020-04-27 08:17:15 +02:00
|
|
|
case .success:
|
2020-04-26 18:26:41 +02:00
|
|
|
BatchUpdate.shared.end()
|
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
self.accountZone.createWebFeed(url: bestFeedSpecifier.urlString,
|
|
|
|
name: parsedFeed.title,
|
|
|
|
editedName: editedName,
|
|
|
|
homePageURL: parsedFeed.homePageURL,
|
|
|
|
container: container) { result in
|
2020-04-19 00:30:58 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
self.refreshProgress.completeTask()
|
2020-04-24 02:13:57 +02:00
|
|
|
switch result {
|
2020-04-26 18:26:41 +02:00
|
|
|
case .success(let externalID):
|
|
|
|
feed.externalID = externalID
|
2020-04-27 23:41:45 +02:00
|
|
|
self.sendNewArticlesToTheCloud(account, feed, completion: completion)
|
2020-04-24 02:13:57 +02:00
|
|
|
case .failure(let error):
|
2020-04-24 20:33:43 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-24 02:13:57 +02:00
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-26 18:26:41 +02:00
|
|
|
|
|
|
|
case .failure(let error):
|
2020-04-27 07:52:53 +02:00
|
|
|
BatchUpdate.shared.end()
|
2020-04-24 20:33:43 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-26 18:26:41 +02:00
|
|
|
completion(.failure(error))
|
2020-04-19 00:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-26 18:26:41 +02:00
|
|
|
} else {
|
2020-04-19 00:30:58 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-04-26 18:26:41 +02:00
|
|
|
completion(.success(feed))
|
2020-04-19 00:30:58 +02:00
|
|
|
}
|
2020-04-26 18:26:41 +02:00
|
|
|
|
2020-04-19 00:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
case .failure:
|
|
|
|
BatchUpdate.shared.end()
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(AccountError.createErrorNotFound))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 23:41:45 +02:00
|
|
|
func sendNewArticlesToTheCloud(_ account: Account, _ feed: WebFeed, completion: @escaping (Result<WebFeed, Error>) -> Void) {
|
|
|
|
account.fetchArticlesAsync(.webFeed(feed)) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let articles):
|
2020-05-28 23:24:10 +02:00
|
|
|
self.storeArticleChanges(new: articles, updated: Set<Article>(), deleted: Set<Article>()) {
|
|
|
|
self.sendArticleStatus(for: account, showProgress: true) { result in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
self.articlesZone.fetchChangesInZone() { _ in
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.success(feed))
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
2020-04-27 23:41:45 +02:00
|
|
|
self.refreshProgress.clear()
|
2020-05-28 23:24:10 +02:00
|
|
|
completion(.failure(error))
|
2020-04-27 23:41:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
self.refreshProgress.clear()
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 09:15:28 +02:00
|
|
|
func processAccountError(_ account: Account, _ error: Error) {
|
|
|
|
if case CloudKitZoneError.userDeletedZone = error {
|
|
|
|
account.removeFeeds(account.topLevelWebFeeds)
|
|
|
|
for folder in account.folders ?? Set<Folder>() {
|
|
|
|
account.removeFolder(folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 23:24:10 +02:00
|
|
|
func storeArticleChanges(new: Set<Article>?, updated: Set<Article>?, deleted: Set<Article>?, completion: @escaping () -> Void) {
|
2020-04-30 23:58:41 +02:00
|
|
|
// New records with a read status aren't really new, they just didn't have the read article stored
|
2020-05-28 23:24:10 +02:00
|
|
|
let group = DispatchGroup()
|
2020-04-30 23:58:41 +02:00
|
|
|
if let new = new {
|
|
|
|
let filteredNew = new.filter { $0.status.read == false }
|
2020-05-28 23:24:10 +02:00
|
|
|
group.enter()
|
|
|
|
insertSyncStatuses(articles: filteredNew, statusKey: .new, flag: true) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
insertSyncStatuses(articles: updated, statusKey: .new, flag: false) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
insertSyncStatuses(articles: deleted, statusKey: .deleted, flag: true) {
|
|
|
|
group.leave()
|
|
|
|
}
|
|
|
|
|
|
|
|
group.notify(queue: DispatchQueue.global(qos: .userInitiated)) {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
completion()
|
|
|
|
}
|
2020-04-30 23:58:41 +02:00
|
|
|
}
|
2020-04-27 23:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 23:24:10 +02:00
|
|
|
func insertSyncStatuses(articles: Set<Article>?, statusKey: SyncStatus.Key, flag: Bool, completion: @escaping () -> Void) {
|
2020-04-30 03:06:14 +02:00
|
|
|
guard let articles = articles, !articles.isEmpty else {
|
2020-05-28 23:24:10 +02:00
|
|
|
completion()
|
2020-04-27 23:41:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
let syncStatuses = articles.map { article in
|
|
|
|
return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag)
|
|
|
|
}
|
2020-05-28 23:24:10 +02:00
|
|
|
database.insertStatuses(syncStatuses) { _ in
|
|
|
|
completion()
|
|
|
|
}
|
2020-04-27 23:41:45 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 00:42:56 +02:00
|
|
|
func sendArticleStatus(for account: Account, showProgress: Bool, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
2020-05-02 17:02:58 +02:00
|
|
|
let op = CloudKitSendStatusOperation(account: account,
|
|
|
|
articlesZone: articlesZone,
|
|
|
|
refreshProgress: refreshProgress,
|
|
|
|
showProgress: showProgress,
|
|
|
|
database: database)
|
|
|
|
op.completionBlock = { mainThreadOperaion in
|
|
|
|
if mainThreadOperaion.isCanceled {
|
|
|
|
completion(.failure(CloudKitAccountDelegateError.unknown))
|
|
|
|
} else {
|
|
|
|
completion(.success(()))
|
2020-05-01 00:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 17:02:58 +02:00
|
|
|
mainThreadOperationQueue.add(op)
|
2020-05-01 00:42:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 00:10:44 +02:00
|
|
|
|
|
|
|
func removeWebFeedFromCloud(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
refreshProgress.addToNumberOfTasksAndRemaining(2)
|
|
|
|
accountZone.removeWebFeed(feed, from: container) { result in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
guard let webFeedExternalID = feed.externalID else {
|
|
|
|
completion(.success(()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
self.articlesZone.deleteArticles(webFeedExternalID) { result in
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
completion(result)
|
|
|
|
}
|
|
|
|
case .failure(let error):
|
|
|
|
self.refreshProgress.completeTask()
|
|
|
|
self.processAccountError(account, error)
|
|
|
|
completion(.failure(error))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 19:00:10 +02:00
|
|
|
}
|
2020-04-10 18:20:35 +02:00
|
|
|
|
|
|
|
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {
|
2020-04-11 00:23:39 +02:00
|
|
|
|
2020-05-28 23:24:10 +02:00
|
|
|
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {
|
2020-04-10 18:20:35 +02:00
|
|
|
refreshProgress.completeTask()
|
2020-05-28 23:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func localAccountRefresher(_ refresher: LocalAccountRefresher, articleChanges: ArticleChanges, completion: @escaping () -> Void) {
|
|
|
|
self.storeArticleChanges(new: articleChanges.newArticles,
|
|
|
|
updated: articleChanges.updatedArticles,
|
|
|
|
deleted: articleChanges.deletedArticles,
|
|
|
|
completion: completion)
|
2020-04-10 18:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-05-02 17:02:58 +02:00
|
|
|
|