This commit is contained in:
Brent Simmons committed 2026-06-16 07:54:23 -07:00
commit 0a94ccb776
17 files changed
+69 -109

No files matched your search

+2 -10
View File
@@ -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)
}
}
}
+12 -34
View File
@@ -609,15 +609,8 @@ public enum FetchType {
addOPMLItems(OPMLNormalizer.normalize(items))
}
public func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result<Void, Error>) -> 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<String>, 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<Article>, statusKey: ArticleStatus.Key, flag: Bool) async -> Set<Article> {
guard !articles.isEmpty else {
return Set<Article>()
func updateStatusesAsync(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool) async -> Set<String> {
guard !articleIDs.isEmpty else {
return Set<String>()
}
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<String>()
}
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<Article>) {
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<String>, statusKey: ArticleStatus.Key, flag: Bool) {
_fetchAllUnreadCounts()
NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.articleIDs: articleIDs, UserInfoKey.statusKey: statusKey, UserInfoKey.statusFlag: flag])
@@ -51,7 +51,7 @@ import Secrets
func restoreFeed(feed: Feed, container: Container) async throws
func restoreFolder(folder: Folder) async throws
func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws
func markArticles(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool) async throws
// Called at the end of account’s init method.
func accountDidInitialize()
@@ -574,15 +574,15 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable {
}
}
func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
func markArticles(articleIDs: Set<String>, 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)
@@ -427,13 +427,13 @@ public enum FeedbinAccountDelegateError: String, Error, Sendable {
}
}
func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
func markArticles(articleIDs: Set<String>, 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)
@@ -598,15 +598,15 @@ import Secrets
}
}
func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
func markArticles(articleIDs: Set<String>, 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)
@@ -143,8 +143,8 @@ import Secrets
account?.addFolderToTree(folder)
}
@MainActor func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
await account?.updateAsync(articles: articles, statusKey: statusKey, flag: flag)
@MainActor func markArticles(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
_ = await account?.updateStatusesAsync(articleIDs: articleIDs, statusKey: statusKey, flag: flag)
}
func accountDidInitialize() {
@@ -489,13 +489,13 @@ import Secrets
}
}
@MainActor func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
@MainActor func markArticles(articleIDs: Set<String>, 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)
@@ -569,15 +569,15 @@ final class ReaderAPIAccountDelegate: AccountDelegate {
}
}
@MainActor func markArticles(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
@MainActor func markArticles(articleIDs: Set<String>, 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)
@@ -364,10 +364,11 @@ public struct ArticleCounts: Sendable {
// MARK: - Statuses
public func markAsync(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async -> Set<ArticleStatus> {
/// Mark statuses for articleIDs. Returns the articleIDs whose status actually changed.
public func markAsync(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool) async -> Set<String> {
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<Article>) -> Void
typealias ArticleIDsCompletionBlock = @Sendable (Set<String>) -> Void
typealias ArticleStatusesResultBlock = @Sendable (Set<ArticleStatus>) -> Void
private extension ArticlesDatabase {
@@ -490,9 +490,9 @@ private extension ArticlesDatabase {
articlesTable.fetchStarredAndUnreadCount(feedIDs, completion)
}
func _mark(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleStatusesResultBlock) {
func _mark(articleIDs: Set<String>, 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<String>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping ArticleIDsCompletionBlock) {
@@ -503,11 +503,11 @@ final class ArticlesTable: DatabaseTable, Sendable {
statusesTable.fetchArticleIDsForStatusesWithoutArticlesNewerThan(articleCutoffDate, completion)
}
func mark(_ articles: Set<Article>, _ 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<String>, _ 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<ArticleStatus>())
completion(changedArticleIDs)
}
}
}
@@ -194,10 +194,6 @@ extension Article {
extension Set where Element == Article {
func statuses() -> Set<ArticleStatus> {
return Set<ArticleStatus>(map { $0.status })
}
func dictionary() -> [String: Article] {
var d = [String: Article]()
for article in self {
@@ -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<String>, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set<String> {
let (statusesDictionary, _) = ensureStatusesForArticleIDs(articleIDs, flag, database)
let updatedStatuses = mark(Set(statusesDictionary.values), statusKey, flag, database)
return updatedStatuses?.articleIDs() ?? Set<String>()
}
// MARK: - Fetching
func fetchUnreadArticleIDs() -> Set<String> {
+3 -3
View File
@@ -16,7 +16,7 @@ import Articles
let undoActionName: String
let redoActionName: String
let articles: Set<Article>
let articleIDsByAccountID: [String: Set<String>]
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
}
+11 -15
View File
@@ -15,28 +15,24 @@ import Images
// These handle multiple accounts.
@MainActor func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) {
let d: [String: Set<Article>] = 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<String>], 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<Article>) -> [String: Set<Article>] {
private func articleIDsByAccountID(_ articles: Set<Article>) -> [String: Set<String>] {
let d = Dictionary(grouping: articles, by: { $0.accountID })
return d.mapValues { Set($0) }
return d.mapValues { Set($0.articleIDs()) }
}
extension Article {
@@ -50,12 +50,6 @@ import UserNotifications
}
@objc func statusesDidChange(_ note: Notification) {
if let statuses = note.userInfo?[Account.UserInfoKey.statuses] as? Set<ArticleStatus>, !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<String>,
let statusKey = note.userInfo?[Account.UserInfoKey.statusKey] as? ArticleStatus.Key,
let flag = note.userInfo?[Account.UserInfoKey.statusFlag] as? Bool,
+1 -4
View File
@@ -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()