diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 9343ac704..e476add71 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -1117,7 +1117,7 @@ private extension AppDelegate { } private func handleStatusNotification(userInfo: [AnyHashable: Any], statusKey: ArticleStatus.Key) { - MainActor.assumeIsolated { + Task { @MainActor in guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable: Any], let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String, let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else { @@ -1132,15 +1132,7 @@ private extension AppDelegate { return } - let singleArticleSet = account.fetchArticles(.articleIDs([articleID])) - guard singleArticleSet.count > 0 else { - assertionFailure("Expected article with \(articleID)") - Self.logger.error("No article with articleID found \(articleID) from status notification") - return - } - - assert(singleArticleSet.count == 1) - account.markArticles(singleArticleSet, statusKey: statusKey, flag: true) { _ in } + try? await account.markArticles(articleIDs: [articleID], statusKey: statusKey, flag: true) } } } diff --git a/Modules/Account/Sources/Account/Account.swift b/Modules/Account/Sources/Account/Account.swift index 76b147f75..d09ab50ca 100644 --- a/Modules/Account/Sources/Account/Account.swift +++ b/Modules/Account/Sources/Account/Account.swift @@ -609,15 +609,8 @@ public enum FetchType { addOPMLItems(OPMLNormalizer.normalize(items)) } - public func markArticles(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { - Task { @MainActor in - do { - try await delegate.markArticles(articles: articles, statusKey: statusKey, flag: flag) - completion(.success(())) - } catch { - completion(.failure(error)) - } - } + public func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { + try await delegate.markArticles(articleIDs: articleIDs, statusKey: statusKey, flag: flag) } func existingContainer(withExternalID externalID: String) -> Container? { @@ -960,19 +953,20 @@ public enum FetchType { sendNotificationAbout(newAndUpdatedArticles) } - /// Returns set of Article whose statuses did change. + /// Mark statuses for articleIDs. Returns the articleIDs whose status actually changed. @discardableResult - func updateAsync(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async -> Set
{ - guard !articles.isEmpty else { - return Set
() + func updateStatusesAsync(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async -> Set { + guard !articleIDs.isEmpty else { + return Set() } - let updatedStatuses = await database.markAsync(articles: articles, statusKey: statusKey, flag: flag) - let updatedArticleIDs = updatedStatuses.articleIDs() - let updatedArticles = Set(articles.filter { updatedArticleIDs.contains($0.articleID) }) - noteStatusesForArticlesDidChange(updatedArticles) + let changedArticleIDs = await database.markAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + guard !changedArticleIDs.isEmpty else { + return Set() + } + noteStatusesForArticleIDsDidChange(articleIDs: changedArticleIDs, statusKey: statusKey, flag: flag) - return updatedArticles + return changedArticleIDs } // MARK: - Article Statuses @@ -1485,22 +1479,6 @@ private extension Account { unreadCount = updatedUnreadCount } - func noteStatusesForArticlesDidChange(_ articles: Set
) { - guard !articles.isEmpty else { - return - } - - let feeds = Set(articles.compactMap { $0.feed }) - let statuses = Set(articles.map { $0.status }) - let articleIDs = Set(articles.map { $0.articleID }) - - // .UnreadCountDidChange notification will get sent to Folder and Account objects, - // which will update their own unread counts. - updateUnreadCounts(feeds: feeds) - - NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.statuses: statuses, UserInfoKey.articles: articles, UserInfoKey.articleIDs: articleIDs, UserInfoKey.feeds: feeds]) - } - func noteStatusesForArticleIDsDidChange(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) { _fetchAllUnreadCounts() NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.articleIDs: articleIDs, UserInfoKey.statusKey: statusKey, UserInfoKey.statusFlag: flag]) diff --git a/Modules/Account/Sources/Account/AccountDelegate.swift b/Modules/Account/Sources/Account/AccountDelegate.swift index 81d7bc285..1af9e9816 100644 --- a/Modules/Account/Sources/Account/AccountDelegate.swift +++ b/Modules/Account/Sources/Account/AccountDelegate.swift @@ -51,7 +51,7 @@ import Secrets func restoreFeed(feed: Feed, container: Container) async throws func restoreFolder(folder: Folder) async throws - func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws + func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws // Called at the end of account’s init method. func accountDidInitialize() diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 133eb1bb0..1dc007899 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -574,15 +574,15 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable { } } - func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws { + func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { guard let account else { return } Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public)") - let updatedArticles = await account.updateAsync(articles: articles, statusKey: statusKey, flag: flag) - let syncStatuses = Set(updatedArticles.map { article in - SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) + let changedArticleIDs = await account.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + let syncStatuses = Set(changedArticleIDs.map { articleID in + SyncStatus(articleID: articleID, key: SyncStatus.Key(statusKey), flag: flag) }) await syncDatabase.insertStatuses(syncStatuses) diff --git a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 7bb91cdd0..c8e1bbeba 100644 --- a/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -427,13 +427,13 @@ public enum FeedbinAccountDelegateError: String, Error, Sendable { } } - func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws { + func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { guard let account else { return } - let updatedArticles = await account.updateAsync(articles: articles, statusKey: statusKey, flag: flag) - let syncStatuses = Set(updatedArticles.map { article in - SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) + let changedArticleIDs = await account.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + let syncStatuses = Set(changedArticleIDs.map { articleID in + SyncStatus(articleID: articleID, key: SyncStatus.Key(statusKey), flag: flag) }) await syncDatabase.insertStatuses(syncStatuses) diff --git a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 4e6a5c231..ce1cc1438 100644 --- a/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Modules/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -598,15 +598,15 @@ import Secrets } } - func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws { + func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { guard let account else { return } Self.logger.debug("FeedlyAccountDelegate: markArticles") - let updatedArticles = await account.updateAsync(articles: articles, statusKey: statusKey, flag: flag) - let syncStatuses = Set(updatedArticles.map { article in - SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) + let changedArticleIDs = await account.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + let syncStatuses = Set(changedArticleIDs.map { articleID in + SyncStatus(articleID: articleID, key: SyncStatus.Key(statusKey), flag: flag) }) await syncDatabase.insertStatuses(syncStatuses) diff --git a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index 4de2d5bfd..95f436920 100644 --- a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -143,8 +143,8 @@ import Secrets account?.addFolderToTree(folder) } - @MainActor func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws { - await account?.updateAsync(articles: articles, statusKey: statusKey, flag: flag) + @MainActor func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { + _ = await account?.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) } func accountDidInitialize() { diff --git a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 17afc1d45..b402a52fd 100644 --- a/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Modules/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -489,13 +489,13 @@ import Secrets } } - @MainActor func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws { + @MainActor func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { guard let account else { return } - await account.updateAsync(articles: articles, statusKey: statusKey, flag: flag) - let syncStatuses = Set(articles.map { article in - SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) + _ = await account.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + let syncStatuses = Set(articleIDs.map { articleID in + SyncStatus(articleID: articleID, key: SyncStatus.Key(statusKey), flag: flag) }) await syncDatabase.insertStatuses(syncStatuses) diff --git a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 360dd0e6f..dc054b240 100644 --- a/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Modules/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -569,15 +569,15 @@ final class ReaderAPIAccountDelegate: AccountDelegate { } } - @MainActor func markArticles(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws { + @MainActor func markArticles(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async throws { guard let account else { return } Self.logger.debug("ReaderAPIAccountDelegate: markArticles — statusKey \(statusKey.rawValue)") - let updatedArticles = await account.updateAsync(articles: articles, statusKey: statusKey, flag: flag) - let syncStatuses = Set(updatedArticles.map { article in - SyncStatus(articleID: article.articleID, key: SyncStatus.Key(statusKey), flag: flag) + let changedArticleIDs = await account.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag) + let syncStatuses = Set(changedArticleIDs.map { articleID in + SyncStatus(articleID: articleID, key: SyncStatus.Key(statusKey), flag: flag) }) await syncDatabase.insertStatuses(syncStatuses) diff --git a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift index 2c0a2017f..597dd7ebb 100644 --- a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift +++ b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesDatabase.swift @@ -364,10 +364,11 @@ public struct ArticleCounts: Sendable { // MARK: - Statuses - public func markAsync(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async -> Set { + /// Mark statuses for articleIDs. Returns the articleIDs whose status actually changed. + public func markAsync(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool) async -> Set { await withCheckedContinuation { continuation in - _mark(articles: articles, statusKey: statusKey, flag: flag) { statuses in - continuation.resume(returning: statuses) + _mark(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { changedArticleIDs in + continuation.resume(returning: changedArticleIDs) } } } @@ -456,7 +457,6 @@ typealias UpdateArticlesCompletionBlock = @Sendable (ArticleChanges) -> Void typealias SingleUnreadCountCompletionBlock = @Sendable (Int) -> Void typealias ArticleSetResultBlock = @Sendable (Set
) -> Void typealias ArticleIDsCompletionBlock = @Sendable (Set) -> Void -typealias ArticleStatusesResultBlock = @Sendable (Set) -> Void private extension ArticlesDatabase { @@ -490,9 +490,9 @@ private extension ArticlesDatabase { articlesTable.fetchStarredAndUnreadCount(feedIDs, completion) } - func _mark(articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleStatusesResultBlock) { + func _mark(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleIDsCompletionBlock) { Self.logger.debug("ArticlesDatabase: \(#function, privacy: .public) \(self.accountID, privacy: .public)") - return articlesTable.mark(articles, statusKey, flag, completion) + articlesTable.mark(articleIDs, statusKey, flag, completion) } func _markAndFetchNew(articleIDs: Set, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleIDsCompletionBlock) { diff --git a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift index b9f02ce4c..276acdc36 100644 --- a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift +++ b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/ArticlesTable.swift @@ -503,11 +503,11 @@ final class ArticlesTable: DatabaseTable, Sendable { statusesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThan(articleCutoffDate, completion) } - func mark(_ articles: Set
, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: @escaping ArticleStatusesResultBlock) { - self.queue.runInTransaction { database in - let statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database) + func mark(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ completion: @escaping ArticleIDsCompletionBlock) { + queue.runInTransaction { database in + let changedArticleIDs = self.statusesTable.mark(articleIDs, statusKey, flag, database) DispatchQueue.main.async { - completion(statuses ?? Set()) + completion(changedArticleIDs) } } } diff --git a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/Extensions/Article+Database.swift b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/Extensions/Article+Database.swift index 357b215d5..9ac7f8004 100644 --- a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/Extensions/Article+Database.swift +++ b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/Extensions/Article+Database.swift @@ -194,10 +194,6 @@ extension Article { extension Set where Element == Article { - func statuses() -> Set { - return Set(map { $0.status }) - } - func dictionary() -> [String: Article] { var d = [String: Article]() for article in self { diff --git a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift index ed67b13c4..02bf4eb67 100644 --- a/Modules/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift +++ b/Modules/ArticlesDatabase/Sources/ArticlesDatabase/StatusesTable.swift @@ -93,6 +93,13 @@ final class StatusesTable: DatabaseTable, Sendable { return newStatusIDs } + /// Mark statuses for articleIDs in both memory and database. Returns the articleIDs whose status actually changed. + func mark(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set { + let (statusesDictionary, _) = ensureStatusesForArticleIDs(articleIDs, flag, database) + let updatedStatuses = mark(Set(statusesDictionary.values), statusKey, flag, database) + return updatedStatuses?.articleIDs() ?? Set() + } + // MARK: - Fetching func fetchUnreadArticleIDs() -> Set { diff --git a/Shared/Commands/MarkStatusCommand.swift b/Shared/Commands/MarkStatusCommand.swift index 7f0258672..e3d349a1f 100644 --- a/Shared/Commands/MarkStatusCommand.swift +++ b/Shared/Commands/MarkStatusCommand.swift @@ -16,7 +16,7 @@ import Articles let undoActionName: String let redoActionName: String - let articles: Set
+ let articleIDsByAccountID: [String: Set] let undoManager: UndoManager let flag: Bool let statusKey: ArticleStatus.Key @@ -30,7 +30,7 @@ import Articles completion?() return nil } - self.articles = Set(articlesToMark) + self.articleIDsByAccountID = Dictionary(grouping: articlesToMark, by: { $0.accountID }).mapValues { Set($0.articleIDs()) } self.flag = flag self.statusKey = statusKey @@ -64,7 +64,7 @@ import Articles @MainActor private extension MarkStatusCommand { func mark(_ statusKey: ArticleStatus.Key, _ flag: Bool) { - markArticles(articles, statusKey: statusKey, flag: flag, completion: completion) + markArticleIDs(articleIDsByAccountID, statusKey: statusKey, flag: flag, completion: completion) completion = nil } diff --git a/Shared/Extensions/ArticleUtilities.swift b/Shared/Extensions/ArticleUtilities.swift index 7fc253187..aadb5f2d2 100644 --- a/Shared/Extensions/ArticleUtilities.swift +++ b/Shared/Extensions/ArticleUtilities.swift @@ -15,28 +15,24 @@ import Images // These handle multiple accounts. @MainActor func markArticles(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) { - let d: [String: Set
] = accountAndArticlesDictionary(articles) + markArticleIDs(articleIDsByAccountID(articles), statusKey: statusKey, flag: flag, completion: completion) +} - let group = DispatchGroup() - - for (accountID, accountArticles) in d { - guard let account = AccountManager.shared.existingAccount(accountID: accountID) else { - continue +@MainActor func markArticleIDs(_ articleIDsByAccountID: [String: Set], statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) { + Task { @MainActor in + for (accountID, articleIDs) in articleIDsByAccountID { + guard let account = AccountManager.shared.existingAccount(accountID: accountID) else { + continue + } + try? await account.markArticles(articleIDs: articleIDs, statusKey: statusKey, flag: flag) } - group.enter() - account.markArticles(accountArticles, statusKey: statusKey, flag: flag) { _ in - group.leave() - } - } - - group.notify(queue: .main) { completion?() } } -private func accountAndArticlesDictionary(_ articles: Set
) -> [String: Set
] { +private func articleIDsByAccountID(_ articles: Set
) -> [String: Set] { let d = Dictionary(grouping: articles, by: { $0.accountID }) - return d.mapValues { Set($0) } + return d.mapValues { Set($0.articleIDs()) } } extension Article { diff --git a/Shared/UserNotifications/UserNotificationManager.swift b/Shared/UserNotifications/UserNotificationManager.swift index 79ac7aafe..53f043c36 100644 --- a/Shared/UserNotifications/UserNotificationManager.swift +++ b/Shared/UserNotifications/UserNotificationManager.swift @@ -50,12 +50,6 @@ import UserNotifications } @objc func statusesDidChange(_ note: Notification) { - if let statuses = note.userInfo?[Account.UserInfoKey.statuses] as? Set, !statuses.isEmpty { - let identifiers = statuses.filter({ $0.read }).map { "articleID:\($0.articleID)" } - UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers) - return - } - if let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set, let statusKey = note.userInfo?[Account.UserInfoKey.statusKey] as? ArticleStatus.Key, let flag = note.userInfo?[Account.UserInfoKey.statusFlag] as? Bool, diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 20f7f775f..ef1d36398 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -457,11 +457,8 @@ private extension AppDelegate { return } - let singleArticleSet = account.fetchArticles(.articleIDs([articleID])) - assert(singleArticleSet.count == 1) - account.markArticles(singleArticleSet, statusKey: statusKey, flag: true) { _ in } - Task { @MainActor in + try? await account.markArticles(articleIDs: [articleID], statusKey: statusKey, flag: true) _ = try? await account.syncArticleStatus() prepareAccountsForBackground() suspendApplication()