From 6390bd4a5794fc8d368f4631d76aacdd85368c41 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 22 Sep 2018 12:46:24 -0700 Subject: [PATCH] Add the ability to specify a selected Folder when running the add-feed sheet. --- NetNewsWire/AppDelegate.swift | 10 +++++----- .../MainWindow/AddFeed/AddFeedController.swift | 4 ++-- .../MainWindow/AddFeed/AddFeedWindowController.swift | 8 +++++++- NetNewsWire/MainWindow/AddFeed/FolderTreeMenu.swift | 12 +++++++++++- NetNewsWire/MainWindow/MainWindowController.swift | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/NetNewsWire/AppDelegate.swift b/NetNewsWire/AppDelegate.swift index 8e38abf8a..b229d6ac5 100644 --- a/NetNewsWire/AppDelegate.swift +++ b/NetNewsWire/AppDelegate.swift @@ -97,10 +97,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, addFolderWindowController!.runSheetOnWindow(window) } - func showAddFeedSheetOnWindow(_ window: NSWindow, urlString: String?, name: String?) { + func showAddFeedSheetOnWindow(_ window: NSWindow, urlString: String?, name: String?, folder: Folder?) { addFeedController = AddFeedController(hostWindow: window) - addFeedController?.showAddFeedSheet(urlString, name) + addFeedController?.showAddFeedSheet(urlString, name, folder) } func showAddFeedFromListOnMainWindow(_ feedListFeeds: [FeedListFeed]) { @@ -256,14 +256,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, // MARK: Add Feed - func addFeed(_ urlString: String?, _ name: String? = nil) { + func addFeed(_ urlString: String?, name: String? = nil, folder: Folder? = nil) { createAndShowMainWindow() if mainWindowController!.isDisplayingSheet { return } - showAddFeedSheetOnWindow(mainWindowController!.window!, urlString: urlString, name: name) + showAddFeedSheetOnWindow(mainWindowController!.window!, urlString: urlString, name: name, folder: folder) } // MARK: - Actions @@ -416,7 +416,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, if AccountManager.shared.anyAccountHasFeedWithURL(appNewsURLString) { return } - addFeed(appNewsURLString, "NetNewsWire News") + addFeed(appNewsURLString, name: "NetNewsWire News") } @IBAction func openWebsite(_ sender: Any?) { diff --git a/NetNewsWire/MainWindow/AddFeed/AddFeedController.swift b/NetNewsWire/MainWindow/AddFeed/AddFeedController.swift index 0b8d9e975..a713e28f5 100644 --- a/NetNewsWire/MainWindow/AddFeed/AddFeedController.swift +++ b/NetNewsWire/MainWindow/AddFeed/AddFeedController.swift @@ -40,7 +40,7 @@ class AddFeedController: AddFeedWindowControllerDelegate, FeedFinderDelegate { self.hostWindow = hostWindow } - func showAddFeedSheet(_ urlString: String?, _ name: String?) { + func showAddFeedSheet(_ urlString: String?, _ name: String?, _ folder: Folder?) { let folderTreeControllerDelegate = FolderTreeControllerDelegate() @@ -48,7 +48,7 @@ class AddFeedController: AddFeedWindowControllerDelegate, FeedFinderDelegate { rootNode.canHaveChildNodes = true let folderTreeController = TreeController(delegate: folderTreeControllerDelegate, rootNode: rootNode) - addFeedWindowController = AddFeedWindowController(urlString: urlString ?? urlStringFromPasteboard, name: name, folderTreeController: folderTreeController, delegate: self) + addFeedWindowController = AddFeedWindowController(urlString: urlString ?? urlStringFromPasteboard, name: name, folder: folder, folderTreeController: folderTreeController, delegate: self) addFeedWindowController!.runSheetOnWindow(hostWindow) } diff --git a/NetNewsWire/MainWindow/AddFeed/AddFeedWindowController.swift b/NetNewsWire/MainWindow/AddFeed/AddFeedWindowController.swift index 451f801d7..59c728a67 100644 --- a/NetNewsWire/MainWindow/AddFeed/AddFeedWindowController.swift +++ b/NetNewsWire/MainWindow/AddFeed/AddFeedWindowController.swift @@ -29,6 +29,7 @@ class AddFeedWindowController : NSWindowController { private var urlString: String? private var initialName: String? + private var initialFolder: Folder? fileprivate weak var delegate: AddFeedWindowControllerDelegate? fileprivate var folderTreeController: TreeController! @@ -43,11 +44,12 @@ class AddFeedWindowController : NSWindowController { var hostWindow: NSWindow! - convenience init(urlString: String?, name: String?, folderTreeController: TreeController, delegate: AddFeedWindowControllerDelegate?) { + convenience init(urlString: String?, name: String?, folder: Folder?, folderTreeController: TreeController, delegate: AddFeedWindowControllerDelegate?) { self.init(windowNibName: NSNib.Name(rawValue: "AddFeedSheet")) self.urlString = urlString self.initialName = name + self.initialFolder = folder self.delegate = delegate self.folderTreeController = folderTreeController } @@ -69,6 +71,10 @@ class AddFeedWindowController : NSWindowController { } folderPopupButton.menu = FolderTreeMenu.createFolderPopupMenu(with: folderTreeController.rootNode) + if let folder = initialFolder { + FolderTreeMenu.select(folder, in: folderPopupButton) + } + updateUI() } diff --git a/NetNewsWire/MainWindow/AddFeed/FolderTreeMenu.swift b/NetNewsWire/MainWindow/AddFeed/FolderTreeMenu.swift index 5b32307d3..c24203581 100644 --- a/NetNewsWire/MainWindow/AddFeed/FolderTreeMenu.swift +++ b/NetNewsWire/MainWindow/AddFeed/FolderTreeMenu.swift @@ -9,6 +9,7 @@ import AppKit import RSCore import RSTree +import Account class FolderTreeMenu { @@ -25,7 +26,16 @@ class FolderTreeMenu { return menu } - + + static func select(_ folder: Folder, in popupButton: NSPopUpButton) { + for menuItem in popupButton.itemArray { + if let oneFolder = menuItem.representedObject as? Folder, oneFolder == folder { + popupButton.select(menuItem) + return + } + } + } + private static func addFolderItemsToMenuWithNodes(menu: NSMenu, nodes: [Node], indentationLevel: Int) { nodes.forEach { (oneNode) in diff --git a/NetNewsWire/MainWindow/MainWindowController.swift b/NetNewsWire/MainWindow/MainWindowController.swift index 42bef3427..6a3753347 100644 --- a/NetNewsWire/MainWindow/MainWindowController.swift +++ b/NetNewsWire/MainWindow/MainWindowController.swift @@ -231,7 +231,7 @@ class MainWindowController : NSWindowController, NSUserInterfaceValidations { @IBAction func showAddFeedWindow(_ sender: Any?) { - appDelegate.showAddFeedSheetOnWindow(window!, urlString: nil, name: nil) + appDelegate.showAddFeedSheetOnWindow(window!, urlString: nil, name: nil, folder: nil) } @IBAction func openArticleInBrowser(_ sender: Any?) {