2018-02-11 21:59:35 +01:00
|
|
|
//
|
|
|
|
// MarkStatusCommand.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-02-11 21:59:35 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 10/26/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2018-02-11 21:59:35 +01:00
|
|
|
|
|
|
|
// Mark articles read/unread, starred/unstarred, deleted/undeleted.
|
|
|
|
|
|
|
|
final class MarkStatusCommand: UndoableCommand {
|
|
|
|
|
|
|
|
let undoActionName: String
|
|
|
|
let redoActionName: String
|
|
|
|
let articles: Set<Article>
|
|
|
|
let undoManager: UndoManager
|
|
|
|
let flag: Bool
|
|
|
|
let statusKey: ArticleStatus.Key
|
2021-04-13 02:41:01 +02:00
|
|
|
var completion: (() -> Void)? = nil
|
2018-02-11 21:59:35 +01:00
|
|
|
|
2021-04-13 02:41:01 +02:00
|
|
|
init?(initialArticles: [Article], statusKey: ArticleStatus.Key, flag: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil) {
|
2018-02-11 21:59:35 +01:00
|
|
|
|
2020-02-29 19:30:35 +01:00
|
|
|
// Filter out articles that already have the desired status or can't be marked.
|
2018-02-11 21:59:35 +01:00
|
|
|
let articlesToMark = MarkStatusCommand.filteredArticles(initialArticles, statusKey, flag)
|
|
|
|
if articlesToMark.isEmpty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
self.articles = Set(articlesToMark)
|
|
|
|
|
|
|
|
self.flag = flag
|
|
|
|
self.statusKey = statusKey
|
|
|
|
self.undoManager = undoManager
|
2021-04-13 02:41:01 +02:00
|
|
|
self.completion = completion
|
2018-02-11 21:59:35 +01:00
|
|
|
|
|
|
|
let actionName = MarkStatusCommand.actionName(statusKey, flag)
|
|
|
|
self.undoActionName = actionName
|
|
|
|
self.redoActionName = actionName
|
|
|
|
}
|
|
|
|
|
2021-04-15 21:29:49 +02:00
|
|
|
convenience init?(initialArticles: [Article], markingRead: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil) {
|
|
|
|
self.init(initialArticles: initialArticles, statusKey: .read, flag: markingRead, undoManager: undoManager, completion: completion)
|
2018-02-11 21:59:35 +01:00
|
|
|
}
|
|
|
|
|
2021-04-15 21:29:49 +02:00
|
|
|
convenience init?(initialArticles: [Article], markingStarred: Bool, undoManager: UndoManager, completion: (() -> Void)? = nil) {
|
|
|
|
self.init(initialArticles: initialArticles, statusKey: .starred, flag: markingStarred, undoManager: undoManager, completion: completion)
|
2018-02-11 21:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func perform() {
|
|
|
|
mark(statusKey, flag)
|
|
|
|
registerUndo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func undo() {
|
|
|
|
mark(statusKey, !flag)
|
|
|
|
registerRedo()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension MarkStatusCommand {
|
|
|
|
|
|
|
|
func mark(_ statusKey: ArticleStatus.Key, _ flag: Bool) {
|
2021-04-13 02:41:01 +02:00
|
|
|
markArticles(articles, statusKey: statusKey, flag: flag, completion: completion)
|
|
|
|
completion = nil
|
2018-02-11 21:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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] {
|
|
|
|
|
2020-02-29 19:30:35 +01:00
|
|
|
return 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
|
|
|
|
}
|
|
|
|
|
2018-02-11 21:59:35 +01:00
|
|
|
}
|
|
|
|
}
|