From 2a18cd8930d50b28645f99c558140aa1920c3f1f Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 11 Jul 2023 22:18:22 -0700 Subject: [PATCH] Continue adopting async/await. --- Account/Sources/Account/Account.swift | 15 +++++++++++++++ .../Feedbin/FeedbinAccountDelegate.swift | 2 +- .../NewsBlurAccountDelegate+Internal.swift | 2 +- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 2 +- .../ArticlesDatabase/ArticlesDatabase.swift | 4 ++++ .../ArticlesDatabase/ArticlesTable.swift | 17 +++++++++++++++++ 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index dbf53474e..2db078b8b 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -887,6 +887,21 @@ public enum FetchType { } } + /// Mark articleIDs statuses based on statusKey and flag. + /// Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification. + func markArticleIDs(_ articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { + guard !articleIDs.isEmpty else { + return + } + + try await database.markArticleIDs(articleIDs, statusKey: statusKey, flag: flag) + noteStatusesForArticleIDsDidChange(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + } + + func markArticleIDsAsRead(_ articleIDs: Set) async throws { + try await markArticleIDs(articleIDs, statusKey: .read, flag: true) + } + /// 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: DatabaseCompletionBlock? = nil) { diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index de674b1b0..904af359b 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -1290,7 +1290,7 @@ private extension FeedbinAccountDelegate { // Mark articles as read let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs) - account.markAsRead(deltaReadArticleIDs) + try await account.markArticleIDsAsRead(deltaReadArticleIDs) } catch let error { self.logger.error("Sync Articles Read Status failed: \(error.localizedDescription, privacy: .public)") } diff --git a/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift b/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift index cf982ba8c..4d6a6173c 100644 --- a/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift +++ b/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift @@ -338,7 +338,7 @@ extension NewsBlurAccountDelegate { // Mark articles as read let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableNewsBlurUnreadStoryHashes) - account.markAsRead(deltaReadArticleIDs) + try await account.markArticleIDsAsRead(deltaReadArticleIDs) } catch let error { self.logger.error("Sync story read status failed: \(error.localizedDescription, privacy: .public)") } diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 6a64d2d08..86c8e19d4 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -1099,7 +1099,7 @@ private extension ReaderAPIAccountDelegate { // Mark articles as read let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableReaderUnreadArticleIDs) - account.markAsRead(deltaReadArticleIDs) + try await account.markArticleIDsAsRead(deltaReadArticleIDs) } catch let error { self.logger.error("Sync Article Read Status failed: \(error.localizedDescription, privacy: .public)") } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index 1ca10cb9f..04c936afc 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -257,6 +257,10 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void return articlesTable.mark(articles, statusKey, flag, completion) } + public func markArticleIDs(_ articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { + try await articlesTable.markArticleIDs(articleIDs, statusKey, flag) + } + public func mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock?) { articlesTable.mark(articleIDs, statusKey, flag, completion) } diff --git a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index aba8807da..e9a333424 100644 --- a/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -493,6 +493,23 @@ final class ArticlesTable: DatabaseTable { } } + func markArticleIDs(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool) async throws { + + try await withCheckedThrowingContinuation { continuation in + Task { @MainActor in + queue.runInTransaction { databaseResult in + switch databaseResult { + case .success(let database): + self.statusesTable.mark(articleIDs, statusKey, flag, database) + continuation.resume() + case .failure(let databaseError): + continuation.resume(throwing: databaseError) + } + } + } + } + } + func mark(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: DatabaseCompletionBlock?) { queue.runInTransaction { databaseResult in switch databaseResult {