From 33882ab276fb7ca48018a1a99714f5001082bcec Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 1 Sep 2019 16:54:07 -0500 Subject: [PATCH] Add Home Page Quick Actions --- iOS/AppDelegate.swift | 62 ++++++++++++++++++++++++++------------ iOS/SceneCoordinator.swift | 5 +++ iOS/SceneDelegate.swift | 23 ++++++++++++++ 3 files changed, 70 insertions(+), 20 deletions(-) diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index cbf0c3d9a..46d947a7b 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -75,24 +75,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele let localAccount = AccountManager.shared.defaultAccount DefaultFeedsImporter.importIfNeeded(isFirstRun, account: localAccount) - let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! - let faviconsFolderURL = tempDir.appendingPathComponent("Favicons") - try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil) - let faviconsFolder = faviconsFolderURL.absoluteString - let faviconsFolderPath = faviconsFolder.suffix(from: faviconsFolder.index(faviconsFolder.startIndex, offsetBy: 7)) - faviconDownloader = FaviconDownloader(folder: String(faviconsFolderPath)) - - let imagesFolderURL = tempDir.appendingPathComponent("Images") - let imagesFolder = imagesFolderURL.absoluteString - let imagesFolderPath = imagesFolder.suffix(from: imagesFolder.index(imagesFolder.startIndex, offsetBy: 7)) - try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil) - imageDownloader = ImageDownloader(folder: String(imagesFolderPath)) - - authorAvatarDownloader = AuthorAvatarDownloader(imageDownloader: imageDownloader) - - let tempFolder = tempDir.absoluteString - let tempFolderPath = tempFolder.suffix(from: tempFolder.index(tempFolder.startIndex, offsetBy: 7)) - feedIconDownloader = FeedIconDownloader(imageDownloader: imageDownloader, folder: String(tempFolderPath)) + initializeDownloaders() + initializeHomeScreenQuickActions() DispatchQueue.main.async { self.unreadCount = AccountManager.shared.unreadCount @@ -188,8 +172,47 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele } +// MARK: App Initialization + +private extension AppDelegate { + + private func initializeDownloaders() { + let tempDir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! + let faviconsFolderURL = tempDir.appendingPathComponent("Favicons") + try! FileManager.default.createDirectory(at: faviconsFolderURL, withIntermediateDirectories: true, attributes: nil) + let faviconsFolder = faviconsFolderURL.absoluteString + let faviconsFolderPath = faviconsFolder.suffix(from: faviconsFolder.index(faviconsFolder.startIndex, offsetBy: 7)) + faviconDownloader = FaviconDownloader(folder: String(faviconsFolderPath)) + + let imagesFolderURL = tempDir.appendingPathComponent("Images") + let imagesFolder = imagesFolderURL.absoluteString + let imagesFolderPath = imagesFolder.suffix(from: imagesFolder.index(imagesFolder.startIndex, offsetBy: 7)) + try! FileManager.default.createDirectory(at: imagesFolderURL, withIntermediateDirectories: true, attributes: nil) + imageDownloader = ImageDownloader(folder: String(imagesFolderPath)) + + authorAvatarDownloader = AuthorAvatarDownloader(imageDownloader: imageDownloader) + + let tempFolder = tempDir.absoluteString + let tempFolderPath = tempFolder.suffix(from: tempFolder.index(tempFolder.startIndex, offsetBy: 7)) + feedIconDownloader = FeedIconDownloader(imageDownloader: imageDownloader, folder: String(tempFolderPath)) + } + + private func initializeHomeScreenQuickActions() { + let unreadTitle = NSLocalizedString("First Unread", comment: "First Unread") + let unreadIcon = UIApplicationShortcutIcon(systemImageName: "arrow.down.circle") + let unreadItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.FirstUnread", localizedTitle: unreadTitle, localizedSubtitle: nil, icon: unreadIcon, userInfo: nil) + + let searchTitle = NSLocalizedString("Search", comment: "Search") + let searchIcon = UIApplicationShortcutIcon(systemImageName: "magnifyingglass") + let searchItem = UIApplicationShortcutItem(type: "com.ranchero.NetNewsWire.ShowSearch", localizedTitle: searchTitle, localizedSubtitle: nil, icon: searchIcon, userInfo: nil) + + UIApplication.shared.shortcutItems = [searchItem, unreadItem] + } + +} + +// MARK: Background Tasks -// MARK: - Background Tasks private extension AppDelegate { /// Register all background tasks. @@ -264,7 +287,6 @@ private extension AppDelegate { private extension AppDelegate { - func sendReceivedArticlesUserNotification(newArticleCount: Int) { let content = UNMutableNotificationContent() diff --git a/iOS/SceneCoordinator.swift b/iOS/SceneCoordinator.swift index 4aa16a5a9..06680c802 100644 --- a/iOS/SceneCoordinator.swift +++ b/iOS/SceneCoordinator.swift @@ -584,6 +584,11 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider { } } + func selectFirstUnreadInAllUnread() { + selectFeed(IndexPath(row: 1, section: 0)) + selectFirstUnreadArticleInTimeline() + } + func selectFirstUnread() { selectFirstUnreadArticleInTimeline() } diff --git a/iOS/SceneDelegate.swift b/iOS/SceneDelegate.swift index baf0ecff9..70e88bd65 100644 --- a/iOS/SceneDelegate.swift +++ b/iOS/SceneDelegate.swift @@ -21,6 +21,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { window!.rootViewController = coordinator.start() window!.makeKeyAndVisible() + if let shortcutItem = connectionOptions.shortcutItem { + handleShortcutItem(shortcutItem) + return + } + if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity { DispatchQueue.main.asyncAfter(deadline: .now()) { self.coordinator.handle(userActivity) @@ -28,6 +33,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { } } + func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { + handleShortcutItem(shortcutItem) + completionHandler(true) + } + func scene(_ scene: UIScene, continue userActivity: NSUserActivity) { coordinator.handle(userActivity) } @@ -45,3 +55,16 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { } } + +private extension SceneDelegate { + + func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) { + switch shortcutItem.type { + case "com.ranchero.NetNewsWire.FirstUnread": + coordinator.selectFirstUnreadInAllUnread() + default: + break + } + } + +}