diff --git a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift index 276ac113d..c0488a0eb 100644 --- a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift @@ -594,18 +594,7 @@ private extension CloudKitAccountDelegate { self.refreshProgress.completeTask() self.refresher.refreshFeeds(webFeeds) { - account.metadata.lastArticleFetchEndTime = Date() - - self.sendArticleStatus(for: account) { result in - switch result { - case .success: - completion(.success(())) - case .failure(let error): - fail(error) - } - } - } case .failure(let error): @@ -638,12 +627,13 @@ private extension CloudKitAccountDelegate { extension CloudKitAccountDelegate: LocalAccountRefresherDelegate { - func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles) { + func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles, completion: @escaping () -> Void) { if let newArticles = newAndUpdatedArticles.newArticles { - let syncStatuses = newArticles.map { article in - return SyncStatus(articleID: article.articleID, key: .read, flag: false) + articlesZone.sendNewArticles(newArticles) { _ in + completion() } - database.insertStatuses(syncStatuses) + } else { + completion() } } diff --git a/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift b/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift index 02c84d78f..f78d1218d 100644 --- a/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift +++ b/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift @@ -51,7 +51,6 @@ final class CloudKitArticlesZone: CloudKitZone { static let webFeedExternalID = "webFeedExternalID" static let read = "read" static let starred = "starred" - static let userDeleted = "userDeleted" } } @@ -82,6 +81,11 @@ final class CloudKitArticlesZone: CloudKitZone { } } + func sendNewArticles(_ articles: Set
, completion: @escaping ((Result) -> Void)) { + let records = makeNewStatusRecords(articles) + saveIfNew(records, completion: completion) + } + func sendArticleStatus(_ syncStatuses: [SyncStatus], articles: Set
, completion: @escaping ((Result) -> Void)) { var records = makeStatusRecords(syncStatuses, articles) @@ -124,6 +128,23 @@ final class CloudKitArticlesZone: CloudKitZone { private extension CloudKitArticlesZone { + func makeNewStatusRecords(_ articles: Set
) -> [CKRecord] { + + var records = [CKRecord]() + + for article in articles { + let recordID = CKRecord.ID(recordName: article.articleID, zoneID: Self.zoneID) + let record = CKRecord(recordType: CloudKitArticleStatus.recordType, recordID: recordID) + if let webFeedExternalID = article.webFeed?.externalID { + record[CloudKitArticleStatus.Fields.webFeedExternalID] = webFeedExternalID + } + record[CloudKitArticleStatus.Fields.read] = "0" + records.append(record) + } + + return records + } + func makeStatusRecords(_ syncStatuses: [SyncStatus], _ articles: Set
) -> [CKRecord] { var articleDict = [String: Article]() @@ -151,8 +172,8 @@ private extension CloudKitArticlesZone { record![CloudKitArticleStatus.Fields.read] = status.flag ? "1" : "0" case .starred: record![CloudKitArticleStatus.Fields.starred] = status.flag ? "1" : "0" - case .userDeleted: - record![CloudKitArticleStatus.Fields.userDeleted] = status.flag ? "1" : "0" + default: + break } } diff --git a/Frameworks/Account/CloudKit/CloudKitArticlesZoneDelegate.swift b/Frameworks/Account/CloudKit/CloudKitArticlesZoneDelegate.swift index a5c1baf3e..3cc535d18 100644 --- a/Frameworks/Account/CloudKit/CloudKitArticlesZoneDelegate.swift +++ b/Frameworks/Account/CloudKit/CloudKitArticlesZoneDelegate.swift @@ -202,7 +202,8 @@ private extension CloudKitArticlesZoneDelegate { extension CloudKitArticlesZoneDelegate: LocalAccountRefresherDelegate { - func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles) { + func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles, completion: @escaping () -> Void) { + completion() } func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) { diff --git a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift index 113841b64..70941c911 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountDelegate.swift @@ -234,7 +234,8 @@ final class LocalAccountDelegate: AccountDelegate { extension LocalAccountDelegate: LocalAccountRefresherDelegate { - func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles) { + func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles, completion: @escaping () -> Void) { + completion() } func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) { diff --git a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift index f109c7b59..7a5873f07 100644 --- a/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift +++ b/Frameworks/Account/LocalAccount/LocalAccountRefresher.swift @@ -14,7 +14,7 @@ import Articles import ArticlesDatabase protocol LocalAccountRefresherDelegate { - func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: NewAndUpdatedArticles) + func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: NewAndUpdatedArticles, completion: @escaping () -> Void) func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) } @@ -101,14 +101,18 @@ extension LocalAccountRefresher: DownloadSessionDelegate { account.update(feed, with: parsedFeed) { result in if case .success(let newAndUpdatedArticles) = result { - self.delegate?.localAccountRefresher(self, didProcess: newAndUpdatedArticles) - if let httpResponse = response as? HTTPURLResponse { - feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) + self.delegate?.localAccountRefresher(self, didProcess: newAndUpdatedArticles) { + if let httpResponse = response as? HTTPURLResponse { + feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) + } + feed.contentHash = dataHash + completion() + self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) } - feed.contentHash = dataHash + } else { + completion() + self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) } - completion() - self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) } }