diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index e7d721700..ab8d39f15 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -836,43 +836,35 @@ 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 markAndFetchNew(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): - self.noteStatusesForArticleIDsDidChange(articleIDs: articleIDs, statusKey: statusKey, flag: flag) - completion?(.success(newArticleStatusIDs)) - case .failure(let databaseError): - completion?(.failure(databaseError)) - } - } + database.mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag, completion: completion) } /// 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) { + func markAsRead(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { markAndFetchNew(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) { + func markAsUnread(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { markAndFetchNew(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) { + func markAsStarred(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { markAndFetchNew(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) { + func markAsUnstarred(_ articleIDs: Set, completion: DatabaseCompletionBlock? = nil) { markAndFetchNew(articleIDs: articleIDs, statusKey: .starred, flag: false, completion: completion) } diff --git a/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift index 9a8cc6b55..8cc5acdb7 100644 --- a/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitArticlesZoneDelegate.swift @@ -96,8 +96,8 @@ private extension CloudKitArticlesZoneDelegate { let group = DispatchGroup() group.enter() - account?.markAsUnread(updateableUnreadArticleIDs) { result in - if case .failure(let databaseError) = result { + account?.markAsUnread(updateableUnreadArticleIDs) { databaseError in + if let databaseError = databaseError { errorOccurred = true self.logger.error("Error occurred while storing unread statuses: \(databaseError.localizedDescription, privacy: .public)") } @@ -105,8 +105,8 @@ private extension CloudKitArticlesZoneDelegate { } group.enter() - account?.markAsRead(updateableReadArticleIDs) { result in - if case .failure(let databaseError) = result { + account?.markAsRead(updateableReadArticleIDs) { databaseError in + if let databaseError = databaseError { errorOccurred = true self.logger.error("Error occurred while storing read statuses: \(databaseError.localizedDescription, privacy: .public)") } @@ -114,8 +114,8 @@ private extension CloudKitArticlesZoneDelegate { } group.enter() - account?.markAsUnstarred(updateableUnstarredArticleIDs) { result in - if case .failure(let databaseError) = result { + account?.markAsUnstarred(updateableUnstarredArticleIDs) { databaseError in + if let databaseError = databaseError { errorOccurred = true self.logger.error("Error occurred while storing unstarred statuses: \(databaseError.localizedDescription, privacy: .public)") } @@ -123,8 +123,8 @@ private extension CloudKitArticlesZoneDelegate { } group.enter() - account?.markAsStarred(updateableStarredArticleIDs) { result in - if case .failure(let databaseError) = result { + account?.markAsStarred(updateableStarredArticleIDs) { databaseError in + if let databaseError = databaseError { errorOccurred = true self.logger.error("Error occurred while stroing starred records: \(databaseError.localizedDescription, privacy: .public)") } diff --git a/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift b/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift index 29022c3fa..a38ee238f 100644 --- a/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift +++ b/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift @@ -123,18 +123,18 @@ final class FeedlyIngestStarredArticleIdsOperation: FeedlyOperation, Logging { let results = StarredStatusResults() group.enter() - account.markAsStarred(remoteStarredArticleIDs) { result in - if case .failure(let error) = result { - results.markAsStarredError = error + account.markAsStarred(remoteStarredArticleIDs) { databaseError in + if let databaseError = databaseError { + results.markAsStarredError = databaseError } group.leave() } let deltaUnstarredArticleIDs = localStarredArticleIDs.subtracting(remoteStarredArticleIDs) group.enter() - account.markAsUnstarred(deltaUnstarredArticleIDs) { result in - if case .failure(let error) = result { - results.markAsUnstarredError = error + account.markAsUnstarred(deltaUnstarredArticleIDs) { databaseError in + if let databaseError = databaseError { + results.markAsUnstarredError = databaseError } group.leave() } diff --git a/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift b/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift index 72ec9f075..fa618fe70 100644 --- a/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift +++ b/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift @@ -123,18 +123,18 @@ final class FeedlyIngestUnreadArticleIdsOperation: FeedlyOperation, Logging { let results = ReadStatusResults() group.enter() - account.markAsUnread(remoteUnreadArticleIDs) { result in - if case .failure(let error) = result { - results.markAsUnreadError = error + account.markAsUnread(remoteUnreadArticleIDs) { databaseError in + if let databaseError = databaseError { + results.markAsUnreadError = databaseError } group.leave() } let articleIDsToMarkRead = localUnreadArticleIDs.subtracting(remoteUnreadArticleIDs) group.enter() - account.markAsRead(articleIDsToMarkRead) { result in - if case .failure(let error) = result { - results.markAsReadError = error + account.markAsRead(articleIDsToMarkRead) { databaseError in + if let databaseError = databaseError { + results.markAsReadError = databaseError } group.leave() } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index fae1df9b2..e32a83198 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -245,8 +245,8 @@ public final class ArticlesDatabase { return articlesTable.mark(articles, statusKey, flag, completion) } - public func markAndFetchNew(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleIDsCompletionBlock) { - articlesTable.markAndFetchNew(articleIDs, statusKey, flag, completion) + public func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock?) { + articlesTable.mark(articleIDs, statusKey, flag, completion) } /// Create statuses for specified articleIDs. For existing statuses, don’t do anything. diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index a64be83dc..94d75d94a 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -217,7 +217,7 @@ final class ArticlesTable: DatabaseTable { func makeDatabaseCalls(_ database: FMDatabase) { let articleIDs = parsedItems.articleIDs() - let (statusesDictionary, _) = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, false, database) //1 + let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, false, database) //1 assert(statusesDictionary.count == articleIDs.count) let incomingArticles = Article.articlesWithParsedItems(parsedItems, webFeedID, self.accountID, statusesDictionary) //2 @@ -299,7 +299,7 @@ final class ArticlesTable: DatabaseTable { articleIDs.formUnion(parsedItems.articleIDs()) } - let (statusesDictionary, _) = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1 + let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1 assert(statusesDictionary.count == articleIDs.count) let allIncomingArticles = Article.articlesWithWebFeedIDsAndItems(webFeedIDsAndItems, self.accountID, statusesDictionary) //2 @@ -472,17 +472,17 @@ final class ArticlesTable: DatabaseTable { } } - func markAndFetchNew(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: @escaping ArticleIDsCompletionBlock) { + func mark(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: DatabaseCompletionBlock?) { queue.runInTransaction { databaseResult in switch databaseResult { case .success(let database): - let newStatusIDs = self.statusesTable.markAndFetchNew(articleIDs, statusKey, flag, database) + self.statusesTable.mark(articleIDs, statusKey, flag, database) DispatchQueue.main.async { - completion(.success(newStatusIDs)) + completion?(nil) } case .failure(let databaseError): DispatchQueue.main.async { - completion(.failure(databaseError)) + completion?(databaseError) } } } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift index bb53a36a2..c09ae1ed4 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift @@ -28,7 +28,7 @@ final class StatusesTable: DatabaseTable { // MARK: - Creating/Updating - func ensureStatusesForArticleIDs(_ articleIDs: Set, _ read: Bool, _ database: FMDatabase) -> ([String: ArticleStatus], Set) { + func ensureStatusesForArticleIDs(_ articleIDs: Set, _ read: Bool, _ database: FMDatabase) -> [String: ArticleStatus] { #if DEBUG // Check for missing statuses — this asserts that all the passed-in articleIDs exist in the statuses table. @@ -44,7 +44,7 @@ final class StatusesTable: DatabaseTable { // Check cache. let articleIDsMissingCachedStatus = articleIDsWithNoCachedStatus(articleIDs) if articleIDsMissingCachedStatus.isEmpty { - return (statusesDictionary(articleIDs), Set()) + return statusesDictionary(articleIDs) } // Check database. @@ -56,7 +56,7 @@ final class StatusesTable: DatabaseTable { self.createAndSaveStatusesForArticleIDs(articleIDsNeedingStatus, read, database) } - return (statusesDictionary(articleIDs), articleIDsNeedingStatus) + return statusesDictionary(articleIDs) } // MARK: - Marking @@ -85,11 +85,10 @@ final class StatusesTable: DatabaseTable { 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