From 5c6e5807d9088f1a1f53cc096426b36de07a106b Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 18 Mar 2024 21:39:13 -0700 Subject: [PATCH] =?UTF-8?q?Change=20markAndFetchNew=20to=20just=20mark=20a?= =?UTF-8?q?nd=20get=20rid=20of=20the=20return=20value=20which=20wasn?= =?UTF-8?q?=E2=80=99t=20used=20anywhere.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Account/Sources/Account/Account.swift | 31 +++++++++---------- .../CloudKitArticlesZoneDelegate.swift | 16 +++++----- ...edlyIngestStarredArticleIdsOperation.swift | 8 ++--- ...eedlyIngestUnreadArticleIdsOperation.swift | 8 ++--- .../ArticlesDatabase/ArticlesDatabase.swift | 4 +-- .../ArticlesDatabaseCompatibility.swift | 8 ++--- .../ArticlesDatabase/ArticlesTable.swift | 5 ++- .../ArticlesDatabase/StatusesTable.swift | 5 ++- 8 files changed, 41 insertions(+), 44 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 1dd5ff8e1..37c3b6cb5 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -868,44 +868,43 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, /// Mark articleIDs statuses based on statusKey and flag. /// Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. /// Returns a set of new article statuses. - func markAndFetchNew(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: ArticleIDsCompletionBlock? = nil) { + func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock? = nil) { guard !articleIDs.isEmpty else { - completion?(.success(Set())) + completion?(nil) return } - database.markAndFetchNew(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { result in - switch result { - case .success(let newArticleStatusIDs): + database.mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { error in + if let error { + completion?(error) + } else { self.noteStatusesForArticleIDsDidChange(articleIDs: articleIDs, statusKey: statusKey, flag: flag) - completion?(.success(newArticleStatusIDs)) - case .failure(let databaseError): - completion?(.failure(databaseError)) + completion?(nil) } } } /// Mark articleIDs as read. Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. /// Returns a set of new article statuses. - func markAsRead(_ articleIDs: Set, completion: ArticleIDsCompletionBlock? = nil) { - markAndFetchNew(articleIDs: articleIDs, statusKey: .read, flag: true, completion: completion) + func markAsRead(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { + mark(articleIDs: articleIDs, statusKey: .read, flag: true, completion: completion) } /// Mark articleIDs as unread. Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. /// Returns a set of new article statuses. - func markAsUnread(_ articleIDs: Set, completion: ArticleIDsCompletionBlock? = nil) { - markAndFetchNew(articleIDs: articleIDs, statusKey: .read, flag: false, completion: completion) + func markAsUnread(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { + mark(articleIDs: articleIDs, statusKey: .read, flag: false, completion: completion) } /// Mark articleIDs as starred. Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. /// Returns a set of new article statuses. - func markAsStarred(_ articleIDs: Set, completion: ArticleIDsCompletionBlock? = nil) { - markAndFetchNew(articleIDs: articleIDs, statusKey: .starred, flag: true, completion: completion) + func markAsStarred(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { + mark(articleIDs: articleIDs, statusKey: .starred, flag: true, completion: completion) } /// Mark articleIDs as unstarred. Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. /// Returns a set of new article statuses. - func markAsUnstarred(_ articleIDs: Set, completion: ArticleIDsCompletionBlock? = nil) { - markAndFetchNew(articleIDs: articleIDs, statusKey: .starred, flag: false, completion: completion) + func markAsUnstarred(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { + mark(articleIDs: articleIDs, statusKey: .starred, flag: false, completion: completion) } // Delete the articles associated with the given set of articleIDs diff --git a/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift index 0f5daf86e..a94d69b35 100644 --- a/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift @@ -99,9 +99,9 @@ private extension CloudKitArticlesZoneDelegate { let group = DispatchGroup() group.enter() - account?.markAsUnread(updateableUnreadArticleIDs) { result in + account?.markAsUnread(updateableUnreadArticleIDs) { databaseError in MainActor.assumeIsolated { - if case .failure(let databaseError) = result { + if let databaseError { errorOccurred = true os_log(.error, log: self.log, "Error occurred while storing unread statuses: %@", databaseError.localizedDescription) } @@ -110,9 +110,9 @@ private extension CloudKitArticlesZoneDelegate { } group.enter() - account?.markAsRead(updateableReadArticleIDs) { result in + account?.markAsRead(updateableReadArticleIDs) { databaseError in MainActor.assumeIsolated { - if case .failure(let databaseError) = result { + if let databaseError { errorOccurred = true os_log(.error, log: self.log, "Error occurred while storing read statuses: %@", databaseError.localizedDescription) } @@ -121,9 +121,9 @@ private extension CloudKitArticlesZoneDelegate { } group.enter() - account?.markAsUnstarred(updateableUnstarredArticleIDs) { result in + account?.markAsUnstarred(updateableUnstarredArticleIDs) { databaseError in MainActor.assumeIsolated { - if case .failure(let databaseError) = result { + if let databaseError { errorOccurred = true os_log(.error, log: self.log, "Error occurred while storing unstarred statuses: %@", databaseError.localizedDescription) } @@ -132,9 +132,9 @@ private extension CloudKitArticlesZoneDelegate { } group.enter() - account?.markAsStarred(updateableStarredArticleIDs) { result in + account?.markAsStarred(updateableStarredArticleIDs) { databaseError in MainActor.assumeIsolated { - if case .failure(let databaseError) = result { + if let databaseError { errorOccurred = true os_log(.error, log: self.log, "Error occurred while storing starred statuses: %@", databaseError.localizedDescription) } diff --git a/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift b/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift index 8c0ba5b03..3b6d940b5 100644 --- a/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift +++ b/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift @@ -125,8 +125,8 @@ final class FeedlyIngestStarredArticleIdsOperation: FeedlyOperation { let results = StarredStatusResults() group.enter() - account.markAsStarred(remoteStarredArticleIDs) { result in - if case .failure(let error) = result { + account.markAsStarred(remoteStarredArticleIDs) { error in + if let error { results.markAsStarredError = error } group.leave() @@ -134,8 +134,8 @@ final class FeedlyIngestStarredArticleIdsOperation: FeedlyOperation { let deltaUnstarredArticleIDs = localStarredArticleIDs.subtracting(remoteStarredArticleIDs) group.enter() - account.markAsUnstarred(deltaUnstarredArticleIDs) { result in - if case .failure(let error) = result { + account.markAsUnstarred(deltaUnstarredArticleIDs) { error in + if let error { results.markAsUnstarredError = error } group.leave() diff --git a/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift b/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift index a06ebe0f5..38d970ee7 100644 --- a/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift +++ b/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift @@ -125,8 +125,8 @@ final class FeedlyIngestUnreadArticleIdsOperation: FeedlyOperation { let results = ReadStatusResults() group.enter() - account.markAsUnread(remoteUnreadArticleIDs) { result in - if case .failure(let error) = result { + account.markAsUnread(remoteUnreadArticleIDs) { error in + if let error { results.markAsUnreadError = error } group.leave() @@ -134,8 +134,8 @@ final class FeedlyIngestUnreadArticleIdsOperation: FeedlyOperation { let articleIDsToMarkRead = localUnreadArticleIDs.subtracting(remoteUnreadArticleIDs) group.enter() - account.markAsRead(articleIDsToMarkRead) { result in - if case .failure(let error) = result { + account.markAsRead(articleIDsToMarkRead) { error in + if let error { results.markAsReadError = error } group.leave() diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index d47a4ff13..c8b92da35 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -255,12 +255,12 @@ public actor ArticlesDatabase { return articlesTable.mark(articles: articles, statusKey: statusKey, flag: flag, database: database) } - public func markAndFetchNew(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) throws -> Set { + public func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) throws { guard let database else { throw DatabaseError.suspended } - return articlesTable.markAndFetchNew(articleIDs: articleIDs, statusKey: statusKey, flag: flag, database: database) + articlesTable.mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag, database: database) } /// Create statuses for specified articleIDs. For existing statuses, don’t do anything. diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabaseCompatibility.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabaseCompatibility.swift index 8aad09da9..08fe0452a 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabaseCompatibility.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabaseCompatibility.swift @@ -304,14 +304,14 @@ public extension ArticlesDatabase { } } - nonisolated func markAndFetchNew(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleIDsCompletionBlock) { + nonisolated func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping DatabaseCompletionBlock) { Task { do { - let statuses = try await markAndFetchNew(articleIDs: articleIDs, statusKey: statusKey, flag: flag) - callArticleIDsCompletion(completion, .success(statuses)) + try await mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + callDatabaseCompletion(completion) } catch { - callArticleIDsCompletion(completion, .failure(.suspended)) + callDatabaseCompletion(completion, .suspended) } } } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index 69bb1cfbb..92362227a 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -368,10 +368,9 @@ final class ArticlesTable { return statuses } - func markAndFetchNew(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, database: FMDatabase) -> Set { + func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, database: FMDatabase) { - let newStatusIDs = statusesTable.markAndFetchNew(articleIDs, statusKey, flag, database) - return newStatusIDs + statusesTable.mark(articleIDs, statusKey, flag, database) } /// Create statuses for specified articleIDs. For existing statuses, don’t do anything. diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift index 2893552e4..069a20c7a 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift @@ -81,11 +81,10 @@ final class StatusesTable { return updatedStatuses } - func markAndFetchNew(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set { - let (statusesDictionary, newStatusIDs) = ensureStatusesForArticleIDs(articleIDs, flag, database) + func mark(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) { + let (statusesDictionary, _) = ensureStatusesForArticleIDs(articleIDs, flag, database) let statuses = Set(statusesDictionary.values) mark(statuses, statusKey, flag, database) - return newStatusIDs } // MARK: - Fetching