From ba5fc89cbca2f9480d161a06a7db6309a2715165 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 3 Apr 2024 22:23:38 -0700 Subject: [PATCH] Convert fetchUnreadArticleIDs to async await. --- Account/Sources/Account/Account.swift | 5 +- .../Feedbin/FeedbinAccountDelegate.swift | 56 +++++++--------- ...eedlyIngestUnreadArticleIdsOperation.swift | 17 +++-- .../NewsBlurAccountDelegate+Internal.swift | 58 +++++++--------- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 67 ++++++++----------- 5 files changed, 89 insertions(+), 114 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index 48040be69..5ca4a615a 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -749,8 +749,9 @@ public enum FetchType { try await database.starredAndUnreadCount(feedIDs: allFeedIDs()) ?? 0 } - public func fetchUnreadArticleIDs(_ completion: @escaping ArticleIDsCompletionBlock) { - database.fetchUnreadArticleIDsAsync(completion: completion) + public func fetchUnreadArticleIDs() async throws -> Set? { + + try await database.unreadArticleIDs() } public func fetchStarredArticleIDs(_ completion: @escaping ArticleIDsCompletionBlock) { diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 2e7585606..5cf373ec1 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -1505,41 +1505,33 @@ private extension FeedbinAccountDelegate { let pendingArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set() - @MainActor func process(_ pendingArticleIDs: Set) { + let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } ) + let updatableFeedbinUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(pendingArticleIDs) - let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } ) - let updatableFeedbinUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(pendingArticleIDs) - - account.fetchUnreadArticleIDs { articleIDsResult in - MainActor.assumeIsolated { - guard let currentUnreadArticleIDs = try? articleIDsResult.get() else { - return - } - - let group = DispatchGroup() - - // Mark articles as unread - let deltaUnreadArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs) - group.enter() - account.markAsUnread(deltaUnreadArticleIDs) { _ in - group.leave() - } - - // Mark articles as read - let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs) - group.enter() - account.markAsRead(deltaReadArticleIDs) { _ in - group.leave() - } - - group.notify(queue: DispatchQueue.main) { - completion() - } - } - } + guard let currentUnreadArticleIDs = try await account.fetchUnreadArticleIDs() else { + completion() + return } - process(pendingArticleIDs) + let group = DispatchGroup() + + // Mark articles as unread + let deltaUnreadArticleIDs = updatableFeedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs) + group.enter() + account.markAsUnread(deltaUnreadArticleIDs) { _ in + group.leave() + } + + // Mark articles as read + let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableFeedbinUnreadArticleIDs) + group.enter() + account.markAsRead(deltaReadArticleIDs) { _ in + group.leave() + } + + group.notify(queue: DispatchQueue.main) { + completion() + } } catch { os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription) diff --git a/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift b/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift index d70bfe327..f4bef00e2 100644 --- a/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift +++ b/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift @@ -96,20 +96,19 @@ final class FeedlyIngestUnreadArticleIdsOperation: FeedlyOperation { didFinish() return } - - account.fetchUnreadArticleIDs { result in - MainActor.assumeIsolated { - switch result { - case .success(let localUnreadArticleIDs): - self.processUnreadArticleIDs(localUnreadArticleIDs) - case .failure(let error): - self.didFinish(with: error) + Task { @MainActor in + + do { + if let localUnreadArticleIDs = try await account.fetchUnreadArticleIDs() { + self.processUnreadArticleIDs(localUnreadArticleIDs) } + } catch { + self.didFinish(with: error) } } } - + private func processUnreadArticleIDs(_ localUnreadArticleIDs: Set) { guard !isCanceled else { didFinish() diff --git a/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift b/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift index 778f1fc76..12c524635 100644 --- a/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift +++ b/Account/Sources/Account/NewsBlur/Internals/NewsBlurAccountDelegate+Internal.swift @@ -325,7 +325,7 @@ extension NewsBlurAccountDelegate { func syncStoryReadState(account: Account, hashes: [NewsBlurStoryHash]?, completion: @escaping (() -> Void)) { - guard let hashes = hashes else { + guard let hashes else { completion() return } @@ -334,41 +334,33 @@ extension NewsBlurAccountDelegate { do { let pendingArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set() - @MainActor func process(_ pendingStoryHashes: Set) { + let newsBlurUnreadStoryHashes = Set(hashes.map { $0.hash } ) + let updatableNewsBlurUnreadStoryHashes = newsBlurUnreadStoryHashes.subtracting(pendingArticleIDs) - let newsBlurUnreadStoryHashes = Set(hashes.map { $0.hash } ) - let updatableNewsBlurUnreadStoryHashes = newsBlurUnreadStoryHashes.subtracting(pendingStoryHashes) - - account.fetchUnreadArticleIDs { articleIDsResult in - MainActor.assumeIsolated { - guard let currentUnreadArticleIDs = try? articleIDsResult.get() else { - return - } - - let group = DispatchGroup() - - // Mark articles as unread - let deltaUnreadArticleIDs = updatableNewsBlurUnreadStoryHashes.subtracting(currentUnreadArticleIDs) - group.enter() - account.markAsUnread(deltaUnreadArticleIDs) { _ in - group.leave() - } - - // Mark articles as read - let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableNewsBlurUnreadStoryHashes) - group.enter() - account.markAsRead(deltaReadArticleIDs) { _ in - group.leave() - } - - group.notify(queue: DispatchQueue.main) { - completion() - } - } - } + guard let currentUnreadArticleIDs = try await account.fetchUnreadArticleIDs() else { + completion() + return } - process(pendingArticleIDs) + let group = DispatchGroup() + + // Mark articles as unread + let deltaUnreadArticleIDs = updatableNewsBlurUnreadStoryHashes.subtracting(currentUnreadArticleIDs) + group.enter() + account.markAsUnread(deltaUnreadArticleIDs) { _ in + group.leave() + } + + // Mark articles as read + let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableNewsBlurUnreadStoryHashes) + group.enter() + account.markAsRead(deltaReadArticleIDs) { _ in + group.leave() + } + + group.notify(queue: DispatchQueue.main) { + completion() + } } catch { os_log(.error, log: self.log, "Sync Story Read Status failed: %@.", error.localizedDescription) } diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 672f40f0f..a785e55ad 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -1302,53 +1302,44 @@ private extension ReaderAPIAccountDelegate { completion() return } - + Task { @MainActor in do { - + let pendingArticleIDs = (try await self.database.selectPendingReadStatusArticleIDs()) ?? Set() - - @MainActor func process(_ pendingArticleIDs: Set) { - let updatableReaderUnreadArticleIDs = Set(articleIDs).subtracting(pendingArticleIDs) - - account.fetchUnreadArticleIDs { articleIDsResult in - - MainActor.assumeIsolated { - guard let currentUnreadArticleIDs = try? articleIDsResult.get() else { - return - } - - let group = DispatchGroup() - - // Mark articles as unread - let deltaUnreadArticleIDs = updatableReaderUnreadArticleIDs.subtracting(currentUnreadArticleIDs) - group.enter() - account.markAsUnread(deltaUnreadArticleIDs) { _ in - group.leave() - } - - // Mark articles as read - let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableReaderUnreadArticleIDs) - group.enter() - account.markAsRead(deltaReadArticleIDs) { _ in - group.leave() - } - - group.notify(queue: DispatchQueue.main) { - completion() - } - } - } + + let updatableReaderUnreadArticleIDs = Set(articleIDs).subtracting(pendingArticleIDs) + + guard let currentUnreadArticleIDs = try await account.fetchUnreadArticleIDs() else { + completion() + return + } + + let group = DispatchGroup() + + // Mark articles as unread + let deltaUnreadArticleIDs = updatableReaderUnreadArticleIDs.subtracting(currentUnreadArticleIDs) + group.enter() + account.markAsUnread(deltaUnreadArticleIDs) { _ in + group.leave() + } + + // Mark articles as read + let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(updatableReaderUnreadArticleIDs) + group.enter() + account.markAsRead(deltaReadArticleIDs) { _ in + group.leave() + } + + group.notify(queue: DispatchQueue.main) { + completion() } - - process(pendingArticleIDs) - } catch { os_log(.error, log: self.log, "Sync Article Read Status failed: %@.", error.localizedDescription) } } } - + func syncArticleStarredState(account: Account, articleIDs: [String]?, completion: @escaping (() -> Void)) { guard let articleIDs = articleIDs else { completion()