Rename MarkAllReadCommand to MarkReadOrUnreadCommand and make it handle both cases.

This commit is contained in:
Brent Simmons 2017-10-29 12:09:56 -07:00
parent 9fbce42959
commit 17b84ec57c
5 changed files with 90 additions and 71 deletions

View File

@ -1,54 +0,0 @@
//
// MarkAllReadCommand.swift
// Evergreen
//
// Created by Brent Simmons on 10/26/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import RSCore
import Data
final class MarkAllReadCommand: UndoableCommand {
static private let actionName = NSLocalizedString("Mark All as Read", comment: "command")
let undoActionName = actionName
let redoActionName = actionName
let articles: Set<Article>
let undoManager: UndoManager
init?(initialArticles: [Article], undoManager: UndoManager) {
// Filter out articles already read.
let unreadArticles = initialArticles.filter { !$0.status.read }
if unreadArticles.isEmpty {
return nil
}
self.articles = Set(unreadArticles)
self.undoManager = undoManager
}
func perform() {
mark(read: true)
registerUndo()
}
func undo() {
mark(read: false)
registerRedo()
}
func redo() {
perform()
}
}
private extension MarkAllReadCommand {
func mark(read: Bool) {
markArticles(articles, statusKey: .read, flag: read)
}
}

View File

@ -0,0 +1,63 @@
//
// MarkReadOrUnreadCommand.swift
// Evergreen
//
// Created by Brent Simmons on 10/26/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import RSCore
import Data
final class MarkReadOrUnreadCommand: UndoableCommand {
static private let markReadActionName = NSLocalizedString("Mark Read", comment: "command")
static private let markUnreadActionName = NSLocalizedString("Mark Unread", comment: "command")
let undoActionName: String
let redoActionName: String
let articles: Set<Article>
let undoManager: UndoManager
let markingRead: Bool
init?(initialArticles: [Article], markingRead: Bool, undoManager: UndoManager) {
// Filter out articles already read.
let articlesToMark = initialArticles.filter { markingRead ? !$0.status.read : $0.status.read }
if articlesToMark.isEmpty {
return nil
}
self.articles = Set(articlesToMark)
self.markingRead = markingRead
self.undoManager = undoManager
if markingRead {
self.undoActionName = MarkReadOrUnreadCommand.markReadActionName
self.redoActionName = MarkReadOrUnreadCommand.markReadActionName
}
else {
self.undoActionName = MarkReadOrUnreadCommand.markUnreadActionName
self.redoActionName = MarkReadOrUnreadCommand.markUnreadActionName
}
}
func perform() {
mark(read: markingRead)
registerUndo()
}
func undo() {
mark(read: !markingRead)
registerRedo()
}
}
private extension MarkReadOrUnreadCommand {
func mark(read: Bool) {
markArticles(articles, statusKey: .read, flag: read)
}
}

View File

@ -17,7 +17,7 @@
846E773E1F6EF67A00A165E2 /* Account.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 846E773A1F6EF5D700A165E2 /* Account.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
846E77411F6EF6A100A165E2 /* Database.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 846E77211F6EF5D100A165E2 /* Database.framework */; };
846E77421F6EF6A100A165E2 /* Database.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 846E77211F6EF5D100A165E2 /* Database.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
84702AA41FA27AC0006B8943 /* MarkAllReadCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84702AA31FA27AC0006B8943 /* MarkAllReadCommand.swift */; };
84702AA41FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84702AA31FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift */; };
849A97431ED9EAA9007D329B /* AddFolderWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849A97421ED9EAA9007D329B /* AddFolderWindowController.swift */; };
849A97531ED9EAC0007D329B /* AddFeedController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849A97511ED9EAC0007D329B /* AddFeedController.swift */; };
849A97541ED9EAC0007D329B /* AddFeedWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 849A97521ED9EAC0007D329B /* AddFeedWindowController.swift */; };
@ -383,7 +383,7 @@
846A7BEB1F872C5600FEFD30 /* BatchUpdate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BatchUpdate.swift; path = Evergreen/BatchUpdate.swift; sourceTree = "<group>"; };
846E77161F6EF5D000A165E2 /* Database.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Database.xcodeproj; path = Frameworks/Database/Database.xcodeproj; sourceTree = "<group>"; };
846E77301F6EF5D600A165E2 /* Account.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Account.xcodeproj; path = Frameworks/Account/Account.xcodeproj; sourceTree = "<group>"; };
84702AA31FA27AC0006B8943 /* MarkAllReadCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkAllReadCommand.swift; sourceTree = "<group>"; };
84702AA31FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkReadOrUnreadCommand.swift; sourceTree = "<group>"; };
849A97421ED9EAA9007D329B /* AddFolderWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AddFolderWindowController.swift; sourceTree = "<group>"; };
849A97511ED9EAC0007D329B /* AddFeedController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AddFeedController.swift; path = AddFeed/AddFeedController.swift; sourceTree = "<group>"; };
849A97521ED9EAC0007D329B /* AddFeedWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AddFeedWindowController.swift; path = AddFeed/AddFeedWindowController.swift; sourceTree = "<group>"; };
@ -525,7 +525,7 @@
84702AB31FA27AE8006B8943 /* Commands */ = {
isa = PBXGroup;
children = (
84702AA31FA27AC0006B8943 /* MarkAllReadCommand.swift */,
84702AA31FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift */,
);
path = Commands;
sourceTree = "<group>";
@ -1185,7 +1185,7 @@
842E45E51ED8C6B7000A8B52 /* MainWindowSplitView.swift in Sources */,
849A97661ED9EB96007D329B /* SidebarViewController.swift in Sources */,
849A97641ED9EB96007D329B /* SidebarOutlineView.swift in Sources */,
84702AA41FA27AC0006B8943 /* MarkAllReadCommand.swift in Sources */,
84702AA41FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift in Sources */,
849A979F1ED9F130007D329B /* SidebarCell.swift in Sources */,
849A97651ED9EB96007D329B /* SidebarTreeControllerDelegate.swift in Sources */,
849A97671ED9EB96007D329B /* UnreadCountView.swift in Sources */,

View File

@ -123,10 +123,10 @@ class TimelineViewController: NSViewController, NSTableViewDelegate, NSTableView
func markAllAsRead() {
guard let undoManager = undoManager, let markAllReadCommand = MarkAllReadCommand(initialArticles: articles, undoManager: undoManager) else {
guard let undoManager = undoManager, let markReadCommand = MarkReadOrUnreadCommand(initialArticles: articles, markingRead: true, undoManager: undoManager) else {
return
}
runCommand(markAllReadCommand)
runCommand(markReadCommand)
}
// MARK: - Undoable Commands
@ -144,9 +144,9 @@ class TimelineViewController: NSViewController, NSTableViewDelegate, NSTableView
private func clearUndoableCommands() {
// When the timeline is reloaded based on a different sidebar selection,
// When the timeline is reloaded and the list of articles changes,
// undoable commands should be dropped otherwise things like
// Redo Mark All as Read are ambiguous. (Do they apply to the previous articles
// Redo Mark Read are ambiguous. (Do they apply to the previous articles
// or to the current articles?)
guard let undoManager = undoManager else {
@ -173,18 +173,29 @@ class TimelineViewController: NSViewController, NSTableViewDelegate, NSTableView
let articles = selectedArticles
let status = articles.first!.status
let markAsRead = !status.read
markArticles(Set(articles), statusKey: .read, flag: markAsRead)
if markAsRead {
markSelectedArticlesAsRead(sender)
}
else {
markSelectedArticlesAsUnread(sender)
}
}
@IBAction func markSelectedArticlesAsRead(_ sender: AnyObject) {
markArticles(Set(selectedArticles), statusKey: .read, flag: true)
guard let undoManager = undoManager, let markReadCommand = MarkReadOrUnreadCommand(initialArticles: selectedArticles, markingRead: true, undoManager: undoManager) else {
return
}
runCommand(markReadCommand)
}
@IBAction func markSelectedArticlesAsUnread(_ sender: AnyObject) {
markArticles(Set(selectedArticles), statusKey: .read, flag: false)
guard let undoManager = undoManager, let markUnreadCommand = MarkReadOrUnreadCommand(initialArticles: selectedArticles, markingRead: false, undoManager: undoManager) else {
return
}
runCommand(markUnreadCommand)
}
// MARK: - Navigation

View File

@ -14,9 +14,8 @@ public protocol UndoableCommand: class {
var redoActionName: String { get }
var undoManager: UndoManager { get }
func perform()
func undo()
func redo()
func perform() // must call registerUndo()
func undo() // must call registerRedo()
}
extension UndoableCommand {
@ -33,7 +32,7 @@ extension UndoableCommand {
undoManager.setActionName(redoActionName)
undoManager.registerUndo(withTarget: self) { (target) in
self.redo()
self.perform()
}
}
}