From 17b84ec57cdf0f05eb85d9e772bd1d6b8745f6e1 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 29 Oct 2017 12:09:56 -0700 Subject: [PATCH] Rename MarkAllReadCommand to MarkReadOrUnreadCommand and make it handle both cases. --- Commands/MarkAllReadCommand.swift | 54 ---------------- Commands/MarkReadOrUnreadCommand.swift | 63 +++++++++++++++++++ Evergreen.xcodeproj/project.pbxproj | 8 +-- .../Timeline/TimelineViewController.swift | 29 ++++++--- .../RSCore/RSCore/UndoableCommand.swift | 7 +-- 5 files changed, 90 insertions(+), 71 deletions(-) delete mode 100644 Commands/MarkAllReadCommand.swift create mode 100644 Commands/MarkReadOrUnreadCommand.swift diff --git a/Commands/MarkAllReadCommand.swift b/Commands/MarkAllReadCommand.swift deleted file mode 100644 index 0dc05e11d..000000000 --- a/Commands/MarkAllReadCommand.swift +++ /dev/null @@ -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
- 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) - } -} diff --git a/Commands/MarkReadOrUnreadCommand.swift b/Commands/MarkReadOrUnreadCommand.swift new file mode 100644 index 000000000..0fb4122ec --- /dev/null +++ b/Commands/MarkReadOrUnreadCommand.swift @@ -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
+ 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) + } +} diff --git a/Evergreen.xcodeproj/project.pbxproj b/Evergreen.xcodeproj/project.pbxproj index dc27fdfa8..2cfd59d74 100644 --- a/Evergreen.xcodeproj/project.pbxproj +++ b/Evergreen.xcodeproj/project.pbxproj @@ -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 = ""; }; 846E77161F6EF5D000A165E2 /* Database.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Database.xcodeproj; path = Frameworks/Database/Database.xcodeproj; sourceTree = ""; }; 846E77301F6EF5D600A165E2 /* Account.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Account.xcodeproj; path = Frameworks/Account/Account.xcodeproj; sourceTree = ""; }; - 84702AA31FA27AC0006B8943 /* MarkAllReadCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkAllReadCommand.swift; sourceTree = ""; }; + 84702AA31FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkReadOrUnreadCommand.swift; sourceTree = ""; }; 849A97421ED9EAA9007D329B /* AddFolderWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AddFolderWindowController.swift; sourceTree = ""; }; 849A97511ED9EAC0007D329B /* AddFeedController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AddFeedController.swift; path = AddFeed/AddFeedController.swift; sourceTree = ""; }; 849A97521ED9EAC0007D329B /* AddFeedWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AddFeedWindowController.swift; path = AddFeed/AddFeedWindowController.swift; sourceTree = ""; }; @@ -525,7 +525,7 @@ 84702AB31FA27AE8006B8943 /* Commands */ = { isa = PBXGroup; children = ( - 84702AA31FA27AC0006B8943 /* MarkAllReadCommand.swift */, + 84702AA31FA27AC0006B8943 /* MarkReadOrUnreadCommand.swift */, ); path = Commands; sourceTree = ""; @@ -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 */, diff --git a/Evergreen/MainWindow/Timeline/TimelineViewController.swift b/Evergreen/MainWindow/Timeline/TimelineViewController.swift index 18d9c705d..f37debf46 100644 --- a/Evergreen/MainWindow/Timeline/TimelineViewController.swift +++ b/Evergreen/MainWindow/Timeline/TimelineViewController.swift @@ -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 diff --git a/Frameworks/RSCore/RSCore/UndoableCommand.swift b/Frameworks/RSCore/RSCore/UndoableCommand.swift index e926c4a5e..c1caa2183 100644 --- a/Frameworks/RSCore/RSCore/UndoableCommand.swift +++ b/Frameworks/RSCore/RSCore/UndoableCommand.swift @@ -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() } } }