Create ArticleSupport struct for CloudKitArticlesZoneDelegate to use instead of it having access to Account. (This is so it can be moved from the Account module to the CloudKitSync module.)

This commit is contained in:
Brent Simmons 2024-06-02 21:02:13 -07:00
parent 58ba14cd78
commit 08a80cdfe9
2 changed files with 57 additions and 35 deletions

View File

@ -415,11 +415,45 @@ enum CloudKitAccountDelegateError: LocalizedError {
}
}
private func articleSupport() -> CloudKitArticlesZoneDelegate.ArticleSupport {
CloudKitArticlesZoneDelegate.ArticleSupport(
delete: { [weak self] articleIDs in
Task { @MainActor in
try? await self?.account?.delete(articleIDs: articleIDs)
}
},
markRead: { [weak self] articleIDs in
Task { @MainActor in
try? await self?.account?.markAsRead(articleIDs)
}
},
markUnread: { [weak self] articleIDs in
Task { @MainActor in
try? await self?.account?.markAsUnread(articleIDs)
}
},
markStarred: { [weak self] articleIDs in
Task { @MainActor in
try? await self?.account?.markAsStarred(articleIDs)
}
},
markUnstarred: { [weak self] articleIDs in
Task { @MainActor in
try? await self?.account?.markAsUnstarred(articleIDs)
}
},
update: { [weak self] (feedID, parsedItems, deleteOlder) async throws -> ArticleChanges? in
try await self?.account?.update(feedID: feedID, with: parsedItems, deleteOlder: deleteOlder)
}
)
}
func accountDidInitialize(_ account: Account) {
self.account = account
accountZone.delegate = CloudKitAcountZoneDelegate(account: account, refreshProgress: refreshProgress, articlesZone: articlesZone)
articlesZone.delegate = CloudKitArticlesZoneDelegate(account: account, database: database, articlesZone: articlesZone)
articlesZone.delegate = CloudKitArticlesZoneDelegate(articleSupport: articleSupport(), database: database, articlesZone: articlesZone)
articlesZone.feedInfoDelegate = self
Task {

View File

@ -19,14 +19,24 @@ import CloudKitSync
final class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
public struct ArticleSupport {
weak var account: Account?
var database: SyncDatabase
let delete: (Set<String>) -> Void
let markRead: (Set<String>) -> Void
let markUnread: (Set<String>) -> Void
let markStarred: (Set<String>) -> Void
let markUnstarred: (Set<String>) -> Void
let update: (String, Set<ParsedItem>, Bool) async throws -> ArticleChanges? // feedID, parsedItems, deleteOlder
}
let articleSupport: ArticleSupport
let database: SyncDatabase
weak var articlesZone: CloudKitArticlesZone?
init(account: Account, database: SyncDatabase, articlesZone: CloudKitArticlesZone) {
self.account = account
private var log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "CloudKit")
init(articleSupport: ArticleSupport, database: SyncDatabase, articlesZone: CloudKitArticlesZone) {
self.articleSupport = articleSupport
self.database = database
self.articlesZone = articlesZone
}
@ -67,7 +77,7 @@ private extension CloudKitArticlesZoneDelegate {
}
try? await database.deleteSelectedForProcessing(deletableArticleIDs)
try? await account?.delete(articleIDs: deletableArticleIDs)
articleSupport.delete(deletableArticleIDs)
}
@MainActor private func update(records: [CKRecord], pendingReadStatusArticleIDs: Set<String>, pendingStarredStatusArticleIDs: Set<String>) async throws {
@ -84,33 +94,11 @@ private extension CloudKitArticlesZoneDelegate {
var errorOccurred = false
do {
try await account?.markAsUnread(updateableUnreadArticleIDs)
} catch {
errorOccurred = true
os_log(.error, log: self.log, "Error occurred while storing unread statuses: %@", error.localizedDescription)
}
do {
try await account?.markAsRead(updateableReadArticleIDs)
} catch {
errorOccurred = true
os_log(.error, log: self.log, "Error occurred while storing read statuses: %@", error.localizedDescription)
}
do {
try await account?.markAsUnstarred(updateableUnstarredArticleIDs)
} catch {
errorOccurred = true
os_log(.error, log: self.log, "Error occurred while storing unstarred statuses: %@", error.localizedDescription)
}
do {
try await account?.markAsStarred(updateableStarredArticleIDs)
} catch {
errorOccurred = true
os_log(.error, log: self.log, "Error occurred while storing starred statuses: %@", error.localizedDescription)
}
articleSupport.markRead(updateableReadArticleIDs)
articleSupport.markUnread(updateableUnreadArticleIDs)
articleSupport.markStarred(updateableStarredArticleIDs)
articleSupport.markUnstarred(updateableUnstarredArticleIDs)
let parsedItems = await Self.makeParsedItems(records)
let feedIDsAndItems = Dictionary(grouping: parsedItems, by: { item in item.feedURL } ).mapValues { Set($0) }
@ -118,7 +106,7 @@ private extension CloudKitArticlesZoneDelegate {
for (feedID, parsedItems) in feedIDsAndItems {
do {
let articleChanges = try await self.account?.update(feedID: feedID, with: parsedItems, deleteOlder: false)
let articleChanges = try await articleSupport.update(feedID, parsedItems, false)
guard let deletes = articleChanges?.deletedArticles, !deletes.isEmpty else {
continue
}