From 741333e99e6fe582b09675fae9b6fc57e79a5d11 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 27 Feb 2022 09:55:59 -0800 Subject: [PATCH] Enhance StatusDidChange message to pass status and flag when not passing full articles. Fixes #3481 --- Account/Sources/Account/Account.swift | 9 ++++++++- .../UserNotificationManager.swift | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index b2f0c2076..db7f23fd3 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -72,6 +72,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, public static let statuses = "statuses" // StatusesDidChange public static let articles = "articles" // StatusesDidChange public static let articleIDs = "articleIDs" // StatusesDidChange + public static let statusKey = "statusKey" // StatusesDidChange + public static let statusFlag = "statusFlag" // StatusesDidChange public static let webFeeds = "webFeeds" // AccountDidDownloadArticles, StatusesDidChange public static let syncErrors = "syncErrors" // AccountsDidFailToSyncWithErrors } @@ -845,7 +847,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, database.markAndFetchNew(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { result in switch result { case .success(let newArticleStatusIDs): - self.noteStatusesForArticleIDsDidChange(articleIDs) + self.noteStatusesForArticleIDsDidChange(articleIDs: articleIDs, statusKey: statusKey, flag: flag) completion?(.success(newArticleStatusIDs)) case .failure(let databaseError): completion?(.failure(databaseError)) @@ -1268,6 +1270,11 @@ private extension Account { NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.statuses: statuses, UserInfoKey.articles: articles, UserInfoKey.articleIDs: articleIDs, UserInfoKey.webFeeds: 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]) + } + func noteStatusesForArticleIDsDidChange(_ articleIDs: Set) { fetchAllUnreadCounts() NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.articleIDs: articleIDs]) diff --git a/Shared/UserNotifications/UserNotificationManager.swift b/Shared/UserNotifications/UserNotificationManager.swift index d8ed11eeb..e41165405 100644 --- a/Shared/UserNotifications/UserNotificationManager.swift +++ b/Shared/UserNotifications/UserNotificationManager.swift @@ -33,11 +33,20 @@ final class UserNotificationManager: NSObject { } @objc func statusesDidChange(_ note: Notification) { - guard let statuses = note.userInfo?[Account.UserInfoKey.statuses] as? Set, !statuses.isEmpty else { + 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 } - let identifiers = statuses.filter({ $0.read }).map { "articleID:\($0.articleID)" } - UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers) + + 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, + statusKey == .read, + flag == true { + let identifiers = articleIDs.map { "articleID:\($0)" } + UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers) + } } }