2020-03-29 18:53:52 +02:00
|
|
|
//
|
|
|
|
// CloudKitAccountZoneDelegate.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 3/29/20.
|
|
|
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-03-30 00:12:34 +02:00
|
|
|
import os.log
|
|
|
|
import RSWeb
|
2020-03-29 18:53:52 +02:00
|
|
|
import CloudKit
|
2020-04-26 03:20:56 +02:00
|
|
|
import RSCore
|
|
|
|
import Articles
|
2020-03-29 18:53:52 +02:00
|
|
|
|
|
|
|
class CloudKitAcountZoneDelegate: CloudKitZoneDelegate {
|
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
private typealias UnclaimedWebFeed = (url: URL, name: String?, editedName: String?, homePageURL: String?, webFeedExternalID: String)
|
2020-03-31 17:32:04 +02:00
|
|
|
private var unclaimedWebFeeds = [String: [UnclaimedWebFeed]]()
|
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
|
|
|
|
|
2020-03-29 18:53:52 +02:00
|
|
|
weak var account: Account?
|
2020-03-30 00:12:34 +02:00
|
|
|
weak var refreshProgress: DownloadProgress?
|
2020-04-26 03:20:56 +02:00
|
|
|
weak var articlesZone: CloudKitArticlesZone?
|
|
|
|
|
|
|
|
init(account: Account, refreshProgress: DownloadProgress, articlesZone: CloudKitArticlesZone) {
|
2020-03-29 18:53:52 +02:00
|
|
|
self.account = account
|
2020-03-30 00:12:34 +02:00
|
|
|
self.refreshProgress = refreshProgress
|
2020-04-26 03:20:56 +02:00
|
|
|
self.articlesZone = articlesZone
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 22:57:00 +02:00
|
|
|
func cloudKitDidModify(changed: [CKRecord], deleted: [CloudKitRecordKey], completion: @escaping (Result<Void, Error>) -> Void) {
|
2020-04-13 11:26:28 +02:00
|
|
|
for deletedRecordKey in deleted {
|
|
|
|
switch deletedRecordKey.recordType {
|
|
|
|
case CloudKitAccountZone.CloudKitWebFeed.recordType:
|
|
|
|
removeWebFeed(deletedRecordKey.recordID.externalID)
|
|
|
|
case CloudKitAccountZone.CloudKitContainer.recordType:
|
|
|
|
removeContainer(deletedRecordKey.recordID.externalID)
|
|
|
|
default:
|
|
|
|
assertionFailure("Unknown record type: \(deletedRecordKey.recordType)")
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 22:57:00 +02:00
|
|
|
|
|
|
|
for changedRecord in changed {
|
|
|
|
switch changedRecord.recordType {
|
|
|
|
case CloudKitAccountZone.CloudKitWebFeed.recordType:
|
2020-04-26 18:26:41 +02:00
|
|
|
addOrUpdateWebFeed(changedRecord)
|
2020-04-12 22:57:00 +02:00
|
|
|
case CloudKitAccountZone.CloudKitContainer.recordType:
|
2020-04-26 18:26:41 +02:00
|
|
|
addOrUpdateContainer(changedRecord)
|
2020-04-12 22:57:00 +02:00
|
|
|
default:
|
|
|
|
assertionFailure("Unknown record type: \(changedRecord.recordType)")
|
|
|
|
}
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
2020-04-12 22:57:00 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
completion(.success(()))
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
2020-04-01 19:22:59 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
func addOrUpdateWebFeed(_ record: CKRecord) {
|
2020-03-31 09:20:47 +02:00
|
|
|
guard let account = account,
|
|
|
|
let urlString = record[CloudKitAccountZone.CloudKitWebFeed.Fields.url] as? String,
|
|
|
|
let containerExternalIDs = record[CloudKitAccountZone.CloudKitWebFeed.Fields.containerExternalIDs] as? [String],
|
2020-04-13 11:26:28 +02:00
|
|
|
let url = URL(string: urlString) else {
|
|
|
|
return
|
|
|
|
}
|
2020-03-30 00:12:34 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
let name = record[CloudKitAccountZone.CloudKitWebFeed.Fields.name] as? String
|
2020-03-30 00:12:34 +02:00
|
|
|
let editedName = record[CloudKitAccountZone.CloudKitWebFeed.Fields.editedName] as? String
|
2020-05-06 01:25:33 +02:00
|
|
|
let homePageURL = record[CloudKitAccountZone.CloudKitWebFeed.Fields.homePageURL] as? String
|
2020-04-23 00:42:20 +02:00
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
if let webFeed = account.existingWebFeed(withExternalID: record.externalID) {
|
2020-05-06 01:25:33 +02:00
|
|
|
updateWebFeed(webFeed, name: name, editedName: editedName, homePageURL: homePageURL, containerExternalIDs: containerExternalIDs)
|
2020-03-30 00:12:34 +02:00
|
|
|
} else {
|
2020-03-31 17:32:04 +02:00
|
|
|
for containerExternalID in containerExternalIDs {
|
|
|
|
if let container = account.existingContainer(withExternalID: containerExternalID) {
|
2020-05-06 01:25:33 +02:00
|
|
|
createWebFeedIfNecessary(url: url, name: name, editedName: editedName, homePageURL: homePageURL, webFeedExternalID: record.externalID, container: container)
|
2020-03-31 17:32:04 +02:00
|
|
|
} else {
|
2020-05-06 01:25:33 +02:00
|
|
|
addUnclaimedWebFeed(url: url, name: name, editedName: editedName, homePageURL: homePageURL, webFeedExternalID: record.externalID, containerExternalID: containerExternalID)
|
2020-03-31 17:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 09:20:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeWebFeed(_ externalID: String) {
|
2020-03-31 10:30:53 +02:00
|
|
|
if let webFeed = account?.existingWebFeed(withExternalID: externalID), let containers = account?.existingContainers(withWebFeed: webFeed) {
|
|
|
|
containers.forEach { $0.removeWebFeed(webFeed) }
|
2020-03-30 00:12:34 +02:00
|
|
|
}
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
func addOrUpdateContainer(_ record: CKRecord) {
|
2020-03-31 01:03:56 +02:00
|
|
|
guard let account = account,
|
|
|
|
let name = record[CloudKitAccountZone.CloudKitContainer.Fields.name] as? String,
|
|
|
|
let isAccount = record[CloudKitAccountZone.CloudKitContainer.Fields.isAccount] as? String,
|
2020-04-13 11:26:28 +02:00
|
|
|
isAccount != "1" else {
|
|
|
|
return
|
|
|
|
}
|
2020-03-30 22:15:45 +02:00
|
|
|
|
2020-03-31 17:32:04 +02:00
|
|
|
var folder = account.existingFolder(withExternalID: record.externalID)
|
|
|
|
folder?.name = name
|
|
|
|
|
|
|
|
if folder == nil {
|
|
|
|
folder = account.ensureFolder(with: name)
|
2020-03-31 01:03:56 +02:00
|
|
|
folder?.externalID = record.externalID
|
2020-03-30 22:15:45 +02:00
|
|
|
}
|
2020-03-31 17:32:04 +02:00
|
|
|
|
|
|
|
if let folder = folder, let containerExternalID = folder.externalID, let unclaimedWebFeeds = unclaimedWebFeeds[containerExternalID] {
|
|
|
|
for unclaimedWebFeed in unclaimedWebFeeds {
|
2020-04-26 18:26:41 +02:00
|
|
|
createWebFeedIfNecessary(url: unclaimedWebFeed.url,
|
|
|
|
name: unclaimedWebFeed.name,
|
|
|
|
editedName: unclaimedWebFeed.editedName,
|
2020-05-06 01:25:33 +02:00
|
|
|
homePageURL: unclaimedWebFeed.homePageURL,
|
2020-04-26 18:26:41 +02:00
|
|
|
webFeedExternalID: unclaimedWebFeed.webFeedExternalID,
|
|
|
|
container: folder)
|
2020-03-31 17:32:04 +02:00
|
|
|
}
|
2020-04-12 22:57:00 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
self.unclaimedWebFeeds.removeValue(forKey: containerExternalID)
|
2020-03-31 17:32:04 +02:00
|
|
|
}
|
2020-04-26 18:26:41 +02:00
|
|
|
|
2020-03-30 22:15:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeContainer(_ externalID: String) {
|
|
|
|
if let folder = account?.existingFolder(withExternalID: externalID) {
|
|
|
|
account?.removeFolder(folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension CloudKitAcountZoneDelegate {
|
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
func updateWebFeed(_ webFeed: WebFeed, name: String?, editedName: String?, homePageURL: String?, containerExternalIDs: [String]) {
|
2020-03-31 09:20:47 +02:00
|
|
|
guard let account = account else { return }
|
2020-04-26 18:26:41 +02:00
|
|
|
|
|
|
|
webFeed.name = name
|
2020-03-31 09:20:47 +02:00
|
|
|
webFeed.editedName = editedName
|
2020-05-06 01:25:33 +02:00
|
|
|
webFeed.homePageURL = homePageURL
|
2020-03-31 04:11:57 +02:00
|
|
|
|
2020-03-31 09:20:47 +02:00
|
|
|
let existingContainers = account.existingContainers(withWebFeed: webFeed)
|
|
|
|
let existingContainerExternalIds = existingContainers.compactMap { $0.externalID }
|
|
|
|
|
|
|
|
let diff = containerExternalIDs.difference(from: existingContainerExternalIds)
|
2020-03-31 04:11:57 +02:00
|
|
|
|
2020-03-31 09:20:47 +02:00
|
|
|
for change in diff {
|
|
|
|
switch change {
|
|
|
|
case .remove(_, let externalID, _):
|
|
|
|
if let container = existingContainers.first(where: { $0.externalID == externalID }) {
|
|
|
|
container.removeWebFeed(webFeed)
|
|
|
|
}
|
|
|
|
case .insert(_, let externalID, _):
|
2020-03-31 10:30:53 +02:00
|
|
|
if let container = account.existingContainer(withExternalID: externalID) {
|
2020-03-31 09:20:47 +02:00
|
|
|
container.addWebFeed(webFeed)
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 04:11:57 +02:00
|
|
|
}
|
2020-03-31 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
func createWebFeedIfNecessary(url: URL, name: String?, editedName: String?, homePageURL: String?, webFeedExternalID: String, container: Container) {
|
2020-04-26 18:26:41 +02:00
|
|
|
guard let account = account else { return }
|
2020-04-12 22:57:00 +02:00
|
|
|
|
2020-04-26 18:26:41 +02:00
|
|
|
if account.existingWebFeed(withExternalID: webFeedExternalID) != nil {
|
2020-04-12 22:57:00 +02:00
|
|
|
return
|
|
|
|
}
|
2020-03-31 04:11:57 +02:00
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
let webFeed = account.createWebFeed(with: name, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: homePageURL)
|
2020-03-31 09:20:47 +02:00
|
|
|
webFeed.editedName = editedName
|
2020-03-31 04:11:57 +02:00
|
|
|
webFeed.externalID = webFeedExternalID
|
2020-04-26 18:26:41 +02:00
|
|
|
container.addWebFeed(webFeed)
|
2020-03-31 17:32:04 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:25:33 +02:00
|
|
|
func addUnclaimedWebFeed(url: URL, name: String?, editedName: String?, homePageURL: String?, webFeedExternalID: String, containerExternalID: String) {
|
2020-03-31 17:32:04 +02:00
|
|
|
if var unclaimedWebFeeds = self.unclaimedWebFeeds[containerExternalID] {
|
2020-05-06 01:25:33 +02:00
|
|
|
unclaimedWebFeeds.append(UnclaimedWebFeed(url: url, name: name, editedName: editedName, homePageURL: homePageURL, webFeedExternalID: webFeedExternalID))
|
2020-03-31 17:32:04 +02:00
|
|
|
self.unclaimedWebFeeds[containerExternalID] = unclaimedWebFeeds
|
|
|
|
} else {
|
|
|
|
var unclaimedWebFeeds = [UnclaimedWebFeed]()
|
2020-05-06 01:25:33 +02:00
|
|
|
unclaimedWebFeeds.append(UnclaimedWebFeed(url: url, name: name, editedName: editedName, homePageURL: homePageURL, webFeedExternalID: webFeedExternalID))
|
2020-03-31 17:32:04 +02:00
|
|
|
self.unclaimedWebFeeds[containerExternalID] = unclaimedWebFeeds
|
|
|
|
}
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|