Files
NetNewsWire/Shared/Commands/MarkStatusCommand.swift
T

109 lines
3.5 KiB
Swift
Raw Normal View History

//
// MarkStatusCommand.swift
2018-08-28 22:18:24 -07:00
// 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.
2025-11-13 17:11:34 -08:00
@MainActor final class MarkStatusCommand: UndoableCommand {
2025-11-05 11:22:10 -08:00
let undoActionName: String
let redoActionName: String
let articleIDsByAccountID: [String: Set<String>]
let undoManager: UndoManager
let flag: Bool
let statusKey: ArticleStatus.Key
2026-01-01 15:59:20 -08:00
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) {
2025-11-05 11:22:10 -08:00
// 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()
}
2025-11-05 11:22:10 -08:00
func undo() {
mark(statusKey, !flag)
registerRedo()
}
}
2025-11-13 17:11:34 -08:00
@MainActor private extension MarkStatusCommand {
2025-11-05 11:22:10 -08:00
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] {
2026-01-01 16:17:54 -08:00
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
}
}
}