From c95daa208ffb69a473492ec6ea600f963edd1cc9 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Mon, 12 Apr 2021 19:41:01 -0500 Subject: [PATCH] Add completion callbacks so that we can ensure that unreads have been marked before determining the next unread. Fixes #2993 --- Account/Sources/Account/Account.swift | 4 ++-- Account/Sources/Account/AccountDelegate.swift | 2 +- .../CloudKit/CloudKitAccountDelegate.swift | 6 +++--- .../FeedWranglerAccountDelegate.swift | 6 +++--- .../Feedbin/FeedbinAccountDelegate.swift | 6 +++--- .../Account/Feedly/FeedlyAccountDelegate.swift | 6 +++--- .../LocalAccount/LocalAccountDelegate.swift | 10 ++++++++-- .../NewsBlur/NewsBlurAccountDelegate.swift | 6 +++--- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 6 +++--- Shared/Commands/MarkStatusCommand.swift | 10 +++++----- Shared/Extensions/ArticleUtilities.swift | 13 +++++++++++-- iOS/AppDelegate.swift | 4 ++-- iOS/RootSplitViewController.swift | 5 +++-- iOS/SceneCoordinator.swift | 17 ++++++++++------- 14 files changed, 60 insertions(+), 41 deletions(-) diff --git a/Account/Sources/Account/Account.swift b/Account/Sources/Account/Account.swift index fb57fdd1c..81bbe56d6 100644 --- a/Account/Sources/Account/Account.swift +++ b/Account/Sources/Account/Account.swift @@ -536,8 +536,8 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, addOPMLItems(OPMLNormalizer.normalize(items)) } - public func markArticles(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { - delegate.markArticles(for: self, articles: articles, statusKey: statusKey, flag: flag) + public func markArticles(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { + delegate.markArticles(for: self, articles: articles, statusKey: statusKey, flag: flag, completion: completion) } func existingContainer(withExternalID externalID: String) -> Container? { diff --git a/Account/Sources/Account/AccountDelegate.swift b/Account/Sources/Account/AccountDelegate.swift index f86368053..1f8584406 100644 --- a/Account/Sources/Account/AccountDelegate.swift +++ b/Account/Sources/Account/AccountDelegate.swift @@ -44,7 +44,7 @@ protocol AccountDelegate { func restoreWebFeed(for account: Account, feed: WebFeed, container: Container, completion: @escaping (Result) -> Void) func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result) -> Void) - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) // Called at the end of account’s init method. func accountDidInitialize(_ account: Account) diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index 143b90e6c..c7371079d 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -387,7 +387,7 @@ final class CloudKitAccountDelegate: AccountDelegate { } } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { account.update(articles, statusKey: statusKey, flag: flag) { result in switch result { case .success(let articles): @@ -398,12 +398,12 @@ final class CloudKitAccountDelegate: AccountDelegate { self.database.insertStatuses(syncStatuses) { _ in self.database.selectPendingCount { result in if let count = try? result.get(), count > 100 { - self.sendArticleStatus(for: account, showProgress: false) { _ in } + self.sendArticleStatus(for: account, showProgress: false, completion: completion) } } } case .failure(let error): - os_log(.error, log: self.log, "Error marking article status: %@", error.localizedDescription) + completion(.failure(error)) } } } diff --git a/Account/Sources/Account/FeedWrangler/FeedWranglerAccountDelegate.swift b/Account/Sources/Account/FeedWrangler/FeedWranglerAccountDelegate.swift index 06edc663f..057585745 100644 --- a/Account/Sources/Account/FeedWrangler/FeedWranglerAccountDelegate.swift +++ b/Account/Sources/Account/FeedWrangler/FeedWranglerAccountDelegate.swift @@ -454,7 +454,7 @@ final class FeedWranglerAccountDelegate: AccountDelegate { fatalError() } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { account.update(articles, statusKey: statusKey, flag: flag) { result in switch result { case .success(let articles): @@ -465,12 +465,12 @@ final class FeedWranglerAccountDelegate: AccountDelegate { self.database.insertStatuses(syncStatuses) { _ in self.database.selectPendingCount { result in if let count = try? result.get(), count > 100 { - self.sendArticleStatus(for: account) { _ in } + self.sendArticleStatus(for: account, completion: completion) } } } case .failure(let error): - os_log(.error, log: self.log, "Error marking article status: %@", error.localizedDescription) + completion(.failure(error)) } } } diff --git a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift index 406a34218..678c73a2e 100644 --- a/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Account/Sources/Account/Feedbin/FeedbinAccountDelegate.swift @@ -536,7 +536,7 @@ final class FeedbinAccountDelegate: AccountDelegate { } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { account.update(articles, statusKey: statusKey, flag: flag) { result in switch result { case .success(let articles): @@ -547,12 +547,12 @@ final class FeedbinAccountDelegate: AccountDelegate { self.database.insertStatuses(syncStatuses) { _ in self.database.selectPendingCount { result in if let count = try? result.get(), count > 100 { - self.sendArticleStatus(for: account) { _ in } + self.sendArticleStatus(for: account, completion: completion) } } } case .failure(let error): - os_log(.error, log: self.log, "Error marking article status: %@", error.localizedDescription) + completion(.failure(error)) } } } diff --git a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift index 56b7533d1..e6672472b 100644 --- a/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Account/Sources/Account/Feedly/FeedlyAccountDelegate.swift @@ -487,7 +487,7 @@ final class FeedlyAccountDelegate: AccountDelegate { } } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { account.update(articles, statusKey: statusKey, flag: flag) { result in switch result { case .success(let articles): @@ -498,12 +498,12 @@ final class FeedlyAccountDelegate: AccountDelegate { self.database.insertStatuses(syncStatuses) { _ in self.database.selectPendingCount { result in if let count = try? result.get(), count > 100 { - self.sendArticleStatus(for: account) { _ in } + self.sendArticleStatus(for: account, completion: completion) } } } case .failure(let error): - os_log(.error, log: self.log, "Error marking article status: %@", error.localizedDescription) + completion(.failure(error)) } } } diff --git a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index 2ae29f7f7..2006f2973 100644 --- a/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -209,8 +209,14 @@ final class LocalAccountDelegate: AccountDelegate { completion(.success(())) } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { - account.update(articles, statusKey: statusKey, flag: flag) { _ in } + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { + account.update(articles, statusKey: statusKey, flag: flag) { result in + if case .failure(let error) = result { + completion(.failure(error)) + } else { + completion(.success(())) + } + } } func accountDidInitialize(_ account: Account) { diff --git a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift index 71f4f22b3..85f41a2ab 100644 --- a/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift +++ b/Account/Sources/Account/NewsBlur/NewsBlurAccountDelegate.swift @@ -564,7 +564,7 @@ final class NewsBlurAccountDelegate: AccountDelegate { } } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { account.update(articles, statusKey: statusKey, flag: flag) { result in switch result { case .success(let articles): @@ -575,12 +575,12 @@ final class NewsBlurAccountDelegate: AccountDelegate { self.database.insertStatuses(syncStatuses) { _ in self.database.selectPendingCount { result in if let count = try? result.get(), count > 100 { - self.sendArticleStatus(for: account) { _ in } + self.sendArticleStatus(for: account, completion: completion) } } } case .failure(let error): - os_log(.error, log: self.log, "Error marking article status: %@", error.localizedDescription) + completion(.failure(error)) } } } diff --git a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index 182db4203..060ba2bbb 100644 --- a/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Account/Sources/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -529,7 +529,7 @@ final class ReaderAPIAccountDelegate: AccountDelegate { } - func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { + func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping (Result) -> Void) { account.update(articles, statusKey: statusKey, flag: flag) { result in switch result { case .success(let articles): @@ -540,12 +540,12 @@ final class ReaderAPIAccountDelegate: AccountDelegate { self.database.insertStatuses(syncStatuses) { _ in self.database.selectPendingCount { result in if let count = try? result.get(), count > 100 { - self.sendArticleStatus(for: account) { _ in } + self.sendArticleStatus(for: account, completion: completion) } } } case .failure(let error): - os_log(.error, log: self.log, "Error marking article status: %@", error.localizedDescription) + completion(.failure(error)) } } } diff --git a/Shared/Commands/MarkStatusCommand.swift b/Shared/Commands/MarkStatusCommand.swift index c513413fd..b1e5f4c5c 100644 --- a/Shared/Commands/MarkStatusCommand.swift +++ b/Shared/Commands/MarkStatusCommand.swift @@ -20,8 +20,9 @@ final class MarkStatusCommand: UndoableCommand { let undoManager: UndoManager let flag: Bool let statusKey: ArticleStatus.Key + var completion: (() -> Void)? = nil - init?(initialArticles: [Article], statusKey: ArticleStatus.Key, flag: Bool, undoManager: UndoManager) { + init?(initialArticles: [Article], statusKey: ArticleStatus.Key, flag: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil) { // Filter out articles that already have the desired status or can't be marked. let articlesToMark = MarkStatusCommand.filteredArticles(initialArticles, statusKey, flag) @@ -33,6 +34,7 @@ final class MarkStatusCommand: UndoableCommand { self.flag = flag self.statusKey = statusKey self.undoManager = undoManager + self.completion = completion let actionName = MarkStatusCommand.actionName(statusKey, flag) self.undoActionName = actionName @@ -40,12 +42,10 @@ final class MarkStatusCommand: UndoableCommand { } convenience init?(initialArticles: [Article], markingRead: Bool, undoManager: UndoManager) { - self.init(initialArticles: initialArticles, statusKey: .read, flag: markingRead, undoManager: undoManager) } convenience init?(initialArticles: [Article], markingStarred: Bool, undoManager: UndoManager) { - self.init(initialArticles: initialArticles, statusKey: .starred, flag: markingStarred, undoManager: undoManager) } @@ -63,8 +63,8 @@ final class MarkStatusCommand: UndoableCommand { private extension MarkStatusCommand { func mark(_ statusKey: ArticleStatus.Key, _ flag: Bool) { - - markArticles(articles, statusKey: statusKey, flag: flag) + markArticles(articles, statusKey: statusKey, flag: flag, completion: completion) + completion = nil } static private let markReadActionName = NSLocalizedString("Mark Read", comment: "command") diff --git a/Shared/Extensions/ArticleUtilities.swift b/Shared/Extensions/ArticleUtilities.swift index 6d9f23963..5e1822cba 100644 --- a/Shared/Extensions/ArticleUtilities.swift +++ b/Shared/Extensions/ArticleUtilities.swift @@ -13,15 +13,24 @@ import Account // These handle multiple accounts. -func markArticles(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) { +func markArticles(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) { let d: [String: Set
] = accountAndArticlesDictionary(articles) + let group = DispatchGroup() + for (accountID, accountArticles) in d { guard let account = AccountManager.shared.existingAccount(with: accountID) else { continue } - account.markArticles(accountArticles, statusKey: statusKey, flag: flag) + group.enter() + account.markArticles(accountArticles, statusKey: statusKey, flag: flag) { _ in + group.leave() + } + } + + group.notify(queue: .main) { + completion?() } } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index da8c4dafd..6e1bab493 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -428,7 +428,7 @@ private extension AppDelegate { os_log(.debug, "No article found from search using %@", articleID) return } - account!.markArticles(article!, statusKey: .read, flag: true) + account!.markArticles(article!, statusKey: .read, flag: true) { _ in } self.prepareAccountsForBackground() account!.syncArticleStatus(completion: { [weak self] _ in if !AccountManager.shared.isSuspended { @@ -458,7 +458,7 @@ private extension AppDelegate { os_log(.debug, "No article found from search using %@", articleID) return } - account!.markArticles(article!, statusKey: .starred, flag: true) + account!.markArticles(article!, statusKey: .starred, flag: true) { _ in } account!.syncArticleStatus(completion: { [weak self] _ in if !AccountManager.shared.isSuspended { if #available(iOS 14, *) { diff --git a/iOS/RootSplitViewController.swift b/iOS/RootSplitViewController.swift index e84a8241f..90b20f3c4 100644 --- a/iOS/RootSplitViewController.swift +++ b/iOS/RootSplitViewController.swift @@ -58,8 +58,9 @@ class RootSplitViewController: UISplitViewController { } @objc func markAllAsReadAndGoToNextUnread(_ sender: Any?) { - coordinator.markAllAsReadInTimeline() - coordinator.selectNextUnread() + coordinator.markAllAsReadInTimeline() { + self.coordinator.selectNextUnread() + } } @objc func markAboveAsRead(_ sender: Any?) { diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 62361afd7..02346c5f7 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -997,13 +997,15 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } } - func markAllAsRead(_ articles: [Article]) { - markArticlesWithUndo(articles, statusKey: .read, flag: true) + func markAllAsRead(_ articles: [Article], completion: (() -> Void)? = nil) { + markArticlesWithUndo(articles, statusKey: .read, flag: true, completion: completion) } - func markAllAsReadInTimeline() { - markAllAsRead(articles) - masterNavigationController.popViewController(animated: true) + func markAllAsReadInTimeline(completion: (() -> Void)? = nil) { + markAllAsRead(articles) { + self.masterNavigationController.popViewController(animated: true) + completion?() + } } func canMarkAboveAsRead(for article: Article) -> Bool { @@ -1372,8 +1374,9 @@ extension SceneCoordinator: UINavigationControllerDelegate { private extension SceneCoordinator { - func markArticlesWithUndo(_ articles: [Article], statusKey: ArticleStatus.Key, flag: Bool) { - guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articles, statusKey: statusKey, flag: flag, undoManager: undoManager) else { + func markArticlesWithUndo(_ articles: [Article], statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) { + guard let undoManager = undoManager, + let markReadCommand = MarkStatusCommand(initialArticles: articles, statusKey: statusKey, flag: flag, undoManager: undoManager, completion: completion) else { return } runCommand(markReadCommand)