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
|
|
|
|
|
|
|
|
class CloudKitAcountZoneDelegate: CloudKitZoneDelegate {
|
|
|
|
|
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-03-29 18:53:52 +02:00
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
init(account: Account, refreshProgress: DownloadProgress) {
|
2020-03-29 18:53:52 +02:00
|
|
|
self.account = account
|
2020-03-30 00:12:34 +02:00
|
|
|
self.refreshProgress = refreshProgress
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cloudKitDidChange(record: CKRecord) {
|
|
|
|
switch record.recordType {
|
|
|
|
case CloudKitAccountZone.CloudKitWebFeed.recordType:
|
2020-03-30 00:12:34 +02:00
|
|
|
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)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloudKitDidDelete(recordType: CKRecord.RecordType, recordID: CKRecord.ID) {
|
|
|
|
switch recordType {
|
|
|
|
case CloudKitAccountZone.CloudKitWebFeed.recordType:
|
|
|
|
removeWebFeed(recordID.externalID)
|
2020-03-30 22:15:45 +02:00
|
|
|
case CloudKitAccountZone.CloudKitContainer.recordType:
|
|
|
|
removeContainer(recordID.externalID)
|
2020-03-29 18:53:52 +02:00
|
|
|
default:
|
|
|
|
assertionFailure("Unknown record type: \(recordID.externalID)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
func addOrUpdateWebFeed(_ record: CKRecord) {
|
|
|
|
guard let account = account else { return }
|
|
|
|
|
|
|
|
let editedName = record[CloudKitAccountZone.CloudKitWebFeed.Fields.editedName] as? String
|
2020-03-29 18:53:52 +02:00
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
if let webFeed = account.existingWebFeed(withExternalID: record.externalID) {
|
|
|
|
webFeed.editedName = editedName
|
|
|
|
} else {
|
|
|
|
if let urlString = record[CloudKitAccountZone.CloudKitWebFeed.Fields.url] as? String, let url = URL(string: urlString) {
|
|
|
|
downloadAndAddWebFeed(url: url, editedName: editedName, externalID: record.externalID)
|
|
|
|
} else {
|
|
|
|
os_log(.error, log: self.log, "Failed to add or update web feed.")
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeWebFeed(_ externalID: String) {
|
2020-03-30 00:12:34 +02:00
|
|
|
if let webFeed = account?.existingWebFeed(withExternalID: externalID) {
|
|
|
|
account?.removeWebFeed(webFeed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:15:45 +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,
|
|
|
|
isAccount != "true" else { return }
|
2020-03-30 22:15:45 +02:00
|
|
|
|
|
|
|
if let folder = account.existingFolder(withExternalID: record.externalID) {
|
|
|
|
folder.name = name
|
|
|
|
} else {
|
2020-03-31 01:03:56 +02:00
|
|
|
let folder = account.ensureFolder(with: name)
|
|
|
|
folder?.externalID = record.externalID
|
2020-03-30 22:15:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeContainer(_ externalID: String) {
|
|
|
|
if let folder = account?.existingFolder(withExternalID: externalID) {
|
|
|
|
account?.removeFolder(folder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension CloudKitAcountZoneDelegate {
|
|
|
|
|
|
|
|
func downloadAndAddWebFeed(url: URL, editedName: String?, externalID: String) {
|
|
|
|
guard let account = account else { return }
|
2020-03-29 18:53:52 +02:00
|
|
|
|
2020-03-30 00:12:34 +02:00
|
|
|
let webFeed = account.createWebFeed(with: editedName, url: url.absoluteString, webFeedID: url.absoluteString, homePageURL: nil)
|
|
|
|
webFeed.editedName = editedName
|
|
|
|
webFeed.externalID = externalID
|
|
|
|
account.addWebFeed(webFeed)
|
|
|
|
|
|
|
|
refreshProgress?.addToNumberOfTasksAndRemaining(1)
|
|
|
|
InitialFeedDownloader.download(url) { parsedFeed in
|
|
|
|
self.refreshProgress?.completeTask()
|
|
|
|
if let parsedFeed = parsedFeed {
|
|
|
|
account.update(webFeed, with: parsedFeed, {_ in })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 18:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|