NetNewsWire/Frameworks/Account/CloudKit/CloudKitAccountZoneDelegate...

182 lines
6.3 KiB
Swift
Raw Normal View History

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
import os.log
import RSWeb
2020-03-29 18:53:52 +02:00
import CloudKit
class CloudKitAcountZoneDelegate: CloudKitZoneDelegate {
private typealias UnclaimedWebFeed = (url: URL, editedName: String?, webFeedExternalID: String)
private var unclaimedWebFeeds = [String: [UnclaimedWebFeed]]()
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
2020-03-29 18:53:52 +02:00
weak var account: Account?
weak var refreshProgress: DownloadProgress?
2020-03-29 18:53:52 +02:00
init(account: Account, refreshProgress: DownloadProgress) {
2020-03-29 18:53:52 +02:00
self.account = account
self.refreshProgress = refreshProgress
2020-03-29 18:53:52 +02:00
}
func cloudKitDidChange(record: CKRecord) {
switch record.recordType {
case CloudKitAccountZone.CloudKitWebFeed.recordType:
addOrUpdateWebFeed(record)
2020-03-30 22:15:45 +02:00
case CloudKitAccountZone.CloudKitContainer.recordType:
addOrUpdateContainer(record)
2020-03-29 18:53:52 +02:00
default:
assertionFailure("Unknown record type: \(record.recordType)")
}
}
2020-04-01 19:22:59 +02:00
func cloudKitDidDelete(recordKey: CloudKitRecordKey) {
switch recordKey.recordType {
2020-03-29 18:53:52 +02:00
case CloudKitAccountZone.CloudKitWebFeed.recordType:
2020-04-01 19:22:59 +02:00
removeWebFeed(recordKey.recordID.externalID)
2020-03-30 22:15:45 +02:00
case CloudKitAccountZone.CloudKitContainer.recordType:
2020-04-01 19:22:59 +02:00
removeContainer(recordKey.recordID.externalID)
2020-03-29 18:53:52 +02:00
default:
2020-04-01 19:22:59 +02:00
assertionFailure("Unknown record type: \(recordKey.recordType)")
2020-03-29 18:53:52 +02:00
}
}
2020-04-01 19:22:59 +02:00
func cloudKitDidChange(records: [CKRecord]) {
// We don't batch process these records
}
func cloudKitDidDelete(recordKeys: [CloudKitRecordKey]) {
// We don't batch process these records
}
2020-03-29 18:53:52 +02:00
func addOrUpdateWebFeed(_ record: CKRecord) {
guard let account = account,
let urlString = record[CloudKitAccountZone.CloudKitWebFeed.Fields.url] as? String,
let containerExternalIDs = record[CloudKitAccountZone.CloudKitWebFeed.Fields.containerExternalIDs] as? [String],
let url = URL(string: urlString) else { return }
let editedName = record[CloudKitAccountZone.CloudKitWebFeed.Fields.editedName] as? String
2020-03-29 18:53:52 +02:00
if let webFeed = account.existingWebFeed(withExternalID: record.externalID) {
updateWebFeed(webFeed, editedName: editedName, containerExternalIDs: containerExternalIDs)
} else {
var webFeed: WebFeed? = nil
for containerExternalID in containerExternalIDs {
if let container = account.existingContainer(withExternalID: containerExternalID) {
if webFeed == nil {
webFeed = createWebFeed(url: url, editedName: editedName, webFeedExternalID: record.externalID)
}
container.addWebFeed(webFeed!)
} else {
addUnclaimedWebFeed(url: url, editedName: editedName, webFeedExternalID: record.externalID, containerExternalID: containerExternalID)
}
}
}
}
func removeWebFeed(_ externalID: String) {
if let webFeed = account?.existingWebFeed(withExternalID: externalID), let containers = account?.existingContainers(withWebFeed: webFeed) {
containers.forEach { $0.removeWebFeed(webFeed) }
}
2020-03-29 18:53:52 +02:00
}
2020-03-30 22:15:45 +02:00
func addOrUpdateContainer(_ record: CKRecord) {
guard let account = account,
let name = record[CloudKitAccountZone.CloudKitContainer.Fields.name] as? String,
let isAccount = record[CloudKitAccountZone.CloudKitContainer.Fields.isAccount] as? String,
2020-04-01 03:42:39 +02:00
isAccount != "1" else { return }
2020-03-30 22:15:45 +02:00
var folder = account.existingFolder(withExternalID: record.externalID)
folder?.name = name
if folder == nil {
folder = account.ensureFolder(with: name)
folder?.externalID = record.externalID
2020-03-30 22:15:45 +02:00
}
if let folder = folder, let containerExternalID = folder.externalID, let unclaimedWebFeeds = unclaimedWebFeeds[containerExternalID] {
for unclaimedWebFeed in unclaimedWebFeeds {
var webFeed = account.existingWebFeed(withExternalID: unclaimedWebFeed.webFeedExternalID)
if webFeed == nil {
webFeed = createWebFeed(url: unclaimedWebFeed.url, editedName: unclaimedWebFeed.editedName, webFeedExternalID: unclaimedWebFeed.webFeedExternalID)
}
if let webFeed = webFeed {
folder.addWebFeed(webFeed)
}
}
self.unclaimedWebFeeds.removeValue(forKey: containerExternalID)
}
2020-03-30 22:15:45 +02:00
}
func removeContainer(_ externalID: String) {
if let folder = account?.existingFolder(withExternalID: externalID) {
account?.removeFolder(folder)
}
}
}
private extension CloudKitAcountZoneDelegate {
func updateWebFeed(_ webFeed: WebFeed, editedName: String?, containerExternalIDs: [String]) {
guard let account = account else { return }
webFeed.editedName = editedName
2020-03-31 04:11:57 +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
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, _):
if let container = account.existingContainer(withExternalID: externalID) {
container.addWebFeed(webFeed)
}
}
2020-03-31 04:11:57 +02:00
}
}
func createWebFeed(url: URL, editedName: String?, webFeedExternalID: String) -> WebFeed? {
guard let account = account else { return nil }
2020-03-31 04:11:57 +02:00
let webFeed = account.createWebFeed(with: editedName, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
webFeed.editedName = editedName
2020-03-31 04:11:57 +02:00
webFeed.externalID = webFeedExternalID
refreshProgress?.addToNumberOfTasksAndRemaining(1)
InitialFeedDownloader.download(url) { parsedFeed in
self.refreshProgress?.completeTask()
if let parsedFeed = parsedFeed {
account.update(webFeed, with: parsedFeed, {_ in })
}
}
return webFeed
}
func addUnclaimedWebFeed(url: URL, editedName: String?, webFeedExternalID: String, containerExternalID: String) {
if var unclaimedWebFeeds = self.unclaimedWebFeeds[containerExternalID] {
unclaimedWebFeeds.append(UnclaimedWebFeed(url: url, editedName: editedName, webFeedExternalID: webFeedExternalID))
self.unclaimedWebFeeds[containerExternalID] = unclaimedWebFeeds
} else {
var unclaimedWebFeeds = [UnclaimedWebFeed]()
unclaimedWebFeeds.append(UnclaimedWebFeed(url: url, editedName: editedName, webFeedExternalID: webFeedExternalID))
self.unclaimedWebFeeds[containerExternalID] = unclaimedWebFeeds
}
2020-03-29 18:53:52 +02:00
}
}