diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index a6395a42f..870782fc5 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -19,8 +19,6 @@ import OSLog import CrashReporter import Sparkle -//var appDelegate: AppDelegate! - @NSApplicationMain final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, UNUserNotificationCenterDelegate, UnreadCountProvider, SPUStandardUserDriverDelegate, SPUUpdaterDelegate { @@ -32,8 +30,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat var extensionContainersFile: ExtensionContainersFile! var extensionFeedAddRequestFile: ExtensionFeedAddRequestFile! - var appName: String! - var refreshTimer: AccountRefreshTimer? var syncTimer: ArticleStatusSyncTimer? var lastRefreshInterval = AppDefaults.refreshInterval @@ -113,6 +109,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat NotificationCenter.default.addObserver(self, selector: #selector(importDownloadedTheme(_:)), name: .didEndDownloadingTheme, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(themeImportError(_:)), name: .didFailToImportThemeWithError, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(mainWindowWillClose(_:)), name: .mainWindowControllerWillClose, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(userDidDragFeedToSidebar(_:)), name: .userDidDragFeedToSidebar, object: nil) NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(didWakeNotification(_:)), name: NSWorkspace.didWakeNotification, object: nil) Self.shared = self @@ -148,8 +145,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat let imagesFolder = (cacheFolder as NSString).appendingPathComponent("Images") let imagesFolderURL = URL(fileURLWithPath: imagesFolder) try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil) - - appName = (Bundle.main.infoDictionary!["CFBundleExecutable"]! as! String) } func applicationDidFinishLaunching(_ note: Notification) { @@ -358,6 +353,15 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat removeMainWindowController(mainWindowController) } + @objc func userDidDragFeedToSidebar(_ note: Notification) { + guard let draggedFeed = note.userInfo?[UserInfoKey.draggedFeed] as? DraggedFeed else { + assertionFailure("Expected userInfo to contain a DraggedFeed.") + return + } + + addFeed(draggedFeed.url, name: draggedFeed.name, account: draggedFeed.account, folder: draggedFeed.folder) + } + // MARK: Main Window func createMainWindowController() -> MainWindowController { diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index 24505e3cb..6b060f84a 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -1207,8 +1207,8 @@ private extension MainWindowController { } guard let selectedObjects = selectedObjectsInSidebar(), selectedObjects.count > 0 else { - window?.title = appDelegate.appName! - setSubtitle(appDelegate.unreadCount) + window?.title = AppConfig.appName + setSubtitle(AccountManager.shared.unreadCount) return } diff --git a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift index 608fd39fd..38668bd96 100644 --- a/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift +++ b/Mac/MainWindow/Sidebar/SidebarOutlineDataSource.swift @@ -12,6 +12,17 @@ import Articles import RSCore import Account +extension Notification.Name { + static let userDidDragFeedToSidebar = Notification.Name("userDidDragFeedToSidebar") // UserInfoKey.draggedFeed +} + +struct DraggedFeed { // UserInfoKey.draggedFeed + let url: String + let name: String? + let account: Account? + let folder: Folder? +} + @objc final class SidebarOutlineDataSource: NSObject, NSOutlineViewDataSource { let treeController: TreeController @@ -473,19 +484,23 @@ private extension SidebarOutlineDataSource { } func acceptSingleNonLocalFeedDrop(_ outlineView: NSOutlineView, _ draggedFeed: PasteboardFeed, _ parentNode: Node, _ index: Int) -> Bool { + guard nodeIsDropTarget(parentNode), index == NSOutlineViewDropOnItemIndex else { return false } // Show the add-feed sheet. + let feedToAdd: DraggedFeed if let account = parentNode.representedObject as? Account { - appDelegate.addFeed(draggedFeed.url, name: draggedFeed.editedName ?? draggedFeed.name, account: account, folder: nil) + feedToAdd = DraggedFeed(url: draggedFeed.url, name: draggedFeed.editedName ?? draggedFeed.name, account: account, folder: nil) } else { let account = parentNode.parent?.representedObject as? Account let folder = parentNode.representedObject as? Folder - appDelegate.addFeed(draggedFeed.url, name: draggedFeed.editedName ?? draggedFeed.name, account: account, folder: folder) + feedToAdd = DraggedFeed(url: draggedFeed.url, name: draggedFeed.editedName ?? draggedFeed.name, account: account, folder: folder) } + NotificationCenter.default.post(name: .userDidDragFeedToSidebar, object: self, userInfo: [UserInfoKey.draggedFeed: feedToAdd]) + return true } diff --git a/Mac/Scriptability/NSApplication+Scriptability.swift b/Mac/Scriptability/NSApplication+Scriptability.swift index dd5e5a657..9d407ab09 100644 --- a/Mac/Scriptability/NSApplication+Scriptability.swift +++ b/Mac/Scriptability/NSApplication+Scriptability.swift @@ -26,10 +26,14 @@ extension NSApplication: ScriptingObjectContainer { return "application" } + private var scriptingAppDelegate: AppDelegate { + NSApplication.shared.delegate as! AppDelegate + } + @objc(currentArticle) func currentArticle() -> ScriptableArticle? { var scriptableArticle: ScriptableArticle? - if let currentArticle = appDelegate.scriptingCurrentArticle { + if let currentArticle = scriptingAppDelegate.scriptingCurrentArticle { if let feed = currentArticle.feed { let scriptableFeed = ScriptableFeed(feed, container: self) scriptableArticle = ScriptableArticle(currentArticle, container: scriptableFeed) @@ -40,7 +44,7 @@ extension NSApplication: ScriptingObjectContainer { @objc(selectedArticles) func selectedArticles() -> NSArray { - let articles = appDelegate.scriptingSelectedArticles + let articles = scriptingAppDelegate.scriptingSelectedArticles let scriptableArticles: [ScriptableArticle] = articles.compactMap { article in if let feed = article.feed, let account = feed.account { let scriptableFeed = ScriptableFeed(feed, container: ScriptableAccount(account)) diff --git a/Shared/SmartFeeds/UnreadFeed.swift b/Shared/SmartFeeds/UnreadFeed.swift index 6ad75b7b2..e569d9078 100644 --- a/Shared/SmartFeeds/UnreadFeed.swift +++ b/Shared/SmartFeeds/UnreadFeed.swift @@ -53,14 +53,14 @@ final class UnreadFeed: PseudoFeed { init() { - self.unreadCount = appDelegate.unreadCount - NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: appDelegate) + self.unreadCount = AccountManager.shared.unreadCount + NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: AccountManager.shared) } @objc func unreadCountDidChange(_ note: Notification) { - assert(note.object is AppDelegate) - unreadCount = appDelegate.unreadCount + assert(note.object is AccountManager) + unreadCount = AccountManager.shared.unreadCount } } diff --git a/Shared/UserInfoKey.swift b/Shared/UserInfoKey.swift index ce200fbd5..693a95bbf 100644 --- a/Shared/UserInfoKey.swift +++ b/Shared/UserInfoKey.swift @@ -14,6 +14,7 @@ struct UserInfoKey { static let url = "url" static let articlePath = "articlePath" static let feedIdentifier = "feedIdentifier" + static let draggedFeed = "draggedFeed" // DraggedFeed struct static let windowState = "windowState" static let windowFullScreenState = "windowFullScreenState"