Add completion callbacks so that we can ensure that unreads have been marked before determining the next unread. Fixes #2993

This commit is contained in:
Maurice Parker
2021-04-12 19:41:01 -05:00
parent 3a1b3f96bb
commit c95daa208f
14 changed files with 60 additions and 41 deletions

View File

@@ -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")

View File

@@ -13,15 +13,24 @@ import Account
// These handle multiple accounts.
func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) {
func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) {
let d: [String: Set<Article>] = 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?()
}
}