Files
NetNewsWire/Shared/Commands/MarkStatusCommand.swift
T

110 lines
3.5 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// MarkStatusCommand.swift
// NetNewsWire
//
// Created by Brent Simmons on 10/26/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import RSCore
import Articles
// Mark articles read/unread, starred/unstarred, deleted/undeleted.
@MainActor final class MarkStatusCommand: UndoableCommand {
let undoActionName: String
let redoActionName: String
let articleIDsByAccountID: [String: Set<String>]
let undoManager: UndoManager
let flag: Bool
let statusKey: ArticleStatus.Key
var completion: (() -> Void)?
// Called after every mark, with the flag that was applied, so undo and redo can update
// the UI in the right direction. Unlike completion, it’s not one-shot.
let didMark: ((Bool) -> Void)?
init?(initialArticles: [Article], statusKey: ArticleStatus.Key, flag: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil, didMark: ((Bool) -> Void)? = nil) {
// Filter out articles that already have the desired status or can’t be marked.
let articlesToMark = MarkStatusCommand.filteredArticles(initialArticles, statusKey, flag)
if articlesToMark.isEmpty {
return nil
}
self.articleIDsByAccountID = Dictionary(grouping: articlesToMark, by: { $0.accountID }).mapValues { Set($0.articleIDs()) }
self.flag = flag
self.statusKey = statusKey
self.undoManager = undoManager
self.completion = completion
self.didMark = didMark
let actionName = MarkStatusCommand.actionName(statusKey, flag)
self.undoActionName = actionName
self.redoActionName = actionName
}
convenience init?(initialArticles: [Article], markingRead: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil) {
self.init(initialArticles: initialArticles, statusKey: .read, flag: markingRead, undoManager: undoManager, completion: completion)
}
convenience init?(initialArticles: [Article], markingStarred: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil) {
self.init(initialArticles: initialArticles, statusKey: .starred, flag: markingStarred, undoManager: undoManager, completion: completion)
}
func perform() {
mark(statusKey, flag)
registerUndo()
}
func undo() {
mark(statusKey, !flag)
registerRedo()
}
}
@MainActor private extension MarkStatusCommand {
func mark(_ statusKey: ArticleStatus.Key, _ flag: Bool) {
let completion = self.completion
let didMark = self.didMark
self.completion = nil
markArticleIDs(articleIDsByAccountID, statusKey: statusKey, flag: flag) {
completion?()
didMark?(flag)
}
}
static private let markReadActionName = NSLocalizedString("Mark Read", comment: "command")
static private let markUnreadActionName = NSLocalizedString("Mark Unread", comment: "command")
static private let markStarredActionName = NSLocalizedString("Mark Starred", comment: "command")
static private let markUnstarredActionName = NSLocalizedString("Mark Unstarred", comment: "command")
static func actionName(_ statusKey: ArticleStatus.Key, _ flag: Bool) -> String {
switch statusKey {
case .read:
return flag ? markReadActionName : markUnreadActionName
case .starred:
return flag ? markStarredActionName : markUnstarredActionName
}
}
static func filteredArticles(_ articles: [Article], _ statusKey: ArticleStatus.Key, _ flag: Bool) -> [Article] {
articles.filter { article in
guard article.status.boolStatus(forKey: statusKey) != flag else {
return false
}
guard statusKey == .read else {
return true
}
guard !article.status.read || article.isAvailableToMarkUnread else {
return false
}
return true
}
}
}