Add CloudKit status syncing
This commit is contained in:
parent
1ab21bd3e3
commit
694be77e96
|
@ -116,6 +116,11 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
|||
database.selectForProcessing { result in
|
||||
|
||||
func processStatuses(_ syncStatuses: [SyncStatus]) {
|
||||
guard syncStatuses.count > 0 else {
|
||||
completion(.success(()))
|
||||
return
|
||||
}
|
||||
|
||||
self.articlesZone.sendArticleStatus(syncStatuses) { result in
|
||||
switch result {
|
||||
case .success:
|
||||
|
@ -409,7 +414,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
|||
|
||||
func accountDidInitialize(_ account: Account) {
|
||||
accountZone.delegate = CloudKitAcountZoneDelegate(account: account, refreshProgress: refreshProgress)
|
||||
articlesZone.delegate = CloudKitArticlesZoneDelegate(account: account)
|
||||
articlesZone.delegate = CloudKitArticlesZoneDelegate(account: account, database: database)
|
||||
|
||||
if account.externalID == nil {
|
||||
accountZone.findOrCreateAccount() { result in
|
||||
|
|
|
@ -9,15 +9,18 @@
|
|||
import Foundation
|
||||
import os.log
|
||||
import CloudKit
|
||||
import SyncDatabase
|
||||
|
||||
class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
|
||||
|
||||
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
|
||||
|
||||
weak var account: Account?
|
||||
var database: SyncDatabase
|
||||
|
||||
init(account: Account) {
|
||||
init(account: Account, database: SyncDatabase) {
|
||||
self.account = account
|
||||
self.database = database
|
||||
}
|
||||
|
||||
func cloudKitDidChange(record: CKRecord) {
|
||||
|
@ -29,7 +32,28 @@ class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
|
|||
}
|
||||
|
||||
func cloudKitDidChange(records: [CKRecord]) {
|
||||
// TODO
|
||||
database.selectPendingReadStatusArticleIDs() { result in
|
||||
switch result {
|
||||
case .success(let pendingReadStatusArticleIDs):
|
||||
|
||||
self.database.selectPendingStarredStatusArticleIDs() { result in
|
||||
switch result {
|
||||
case .success(let pendingStarredStatusArticleIDs):
|
||||
|
||||
self.process(records: records,
|
||||
pendingReadStatusArticleIDs: pendingReadStatusArticleIDs,
|
||||
pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs)
|
||||
|
||||
case .failure(let error):
|
||||
os_log(.error, log: self.log, "Error occurred geting pending starred records: %@", error.localizedDescription)
|
||||
}
|
||||
}
|
||||
case .failure(let error):
|
||||
os_log(.error, log: self.log, "Error occurred getting pending read status records: %@", error.localizedDescription)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func cloudKitDidDelete(recordKeys: [CloudKitRecordKey]) {
|
||||
|
@ -37,3 +61,26 @@ class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
private extension CloudKitArticlesZoneDelegate {
|
||||
|
||||
func process(records: [CKRecord], pendingReadStatusArticleIDs: Set<String>, pendingStarredStatusArticleIDs: Set<String>) {
|
||||
|
||||
let receivedUnreadArticleIDs = Set(records.filter( { $0[CloudKitArticlesZone.CloudKitArticleStatus.Fields.read] == "0" }).map({ $0.externalID }))
|
||||
let receivedReadArticleIDs = Set(records.filter( { $0[CloudKitArticlesZone.CloudKitArticleStatus.Fields.read] == "1" }).map({ $0.externalID }))
|
||||
let receivedUnstarredArticleIDs = Set(records.filter( { $0[CloudKitArticlesZone.CloudKitArticleStatus.Fields.starred] == "0" }).map({ $0.externalID }))
|
||||
let receivedStarredArticleIDs = Set(records.filter( { $0[CloudKitArticlesZone.CloudKitArticleStatus.Fields.starred] == "1" }).map({ $0.externalID }))
|
||||
|
||||
let updateableUnreadArticleIDs = receivedUnreadArticleIDs.subtracting(pendingReadStatusArticleIDs)
|
||||
let updateableReadArticleIDs = receivedReadArticleIDs.subtracting(pendingReadStatusArticleIDs)
|
||||
let updateableUnstarredArticleIDs = receivedUnstarredArticleIDs.subtracting(pendingStarredStatusArticleIDs)
|
||||
let updateableStarredArticleIDs = receivedStarredArticleIDs.subtracting(pendingStarredStatusArticleIDs)
|
||||
|
||||
account?.markAsUnread(updateableUnreadArticleIDs)
|
||||
account?.markAsRead(updateableReadArticleIDs)
|
||||
account?.markAsUnstarred(updateableUnstarredArticleIDs)
|
||||
account?.markAsStarred(updateableStarredArticleIDs)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue