Files
NetNewsWire/iOS/SceneDelegate.swift
T

247 lines
7.8 KiB
Swift

//
// AppDelegate.swift
// NetNewsWire
//
// Created by Maurice Parker on 6/28/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
import UserNotifications
import Account
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var coordinator: SceneCoordinator!
// UIWindowScene delegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
window!.tintColor = Assets.Colors.primaryAccent
let rootViewController = window!.rootViewController as! RootSplitViewController
rootViewController.presentsWithGesture = true
rootViewController.showsSecondaryOnlyButton = true
rootViewController.preferredDisplayMode = UISplitViewController.DisplayMode(rawValue: AppDefaults.splitViewPreferredDisplayMode) ?? .oneBesideSecondary
// On first run, show all three columns so the sidebar is visible.
if AppDefaults.isFirstRun {
rootViewController.preferredDisplayMode = .twoBesideSecondary
}
coordinator = SceneCoordinator(rootSplitViewController: rootViewController)
rootViewController.coordinator = coordinator
rootViewController.delegate = coordinator
// An external action (notification tap, URL, shortcut, or user activity)
// dictates navigation. Also doing selection restoration — previously selected feed
// and article — at the same time would start two competing navigations.
// <https://github.com/Ranchero-Software/NetNewsWire/issues/4638>
// Restore the window state, but skip restoring the selection.
let hasPendingExternalAction = connectionOptions.notificationResponse != nil ||
connectionOptions.urlContexts.first?.url != nil ||
connectionOptions.shortcutItem != nil ||
!connectionOptions.userActivities.isEmpty
coordinator.restoreWindowState(activity: session.stateRestorationActivity, restoreSelection: !hasPendingExternalAction)
updateUserInterfaceStyle()
NotificationCenter.default.addObserver(self, selector: #selector(handleUserInterfaceColorPaletteDidUpdate(_:)), name: .userInterfaceColorPaletteDidUpdate, object: nil)
if connectionOptions.urlContexts.first?.url != nil {
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
return
}
if let shortcutItem = connectionOptions.shortcutItem {
handleShortcutItem(shortcutItem)
return
}
if let notificationResponse = connectionOptions.notificationResponse {
coordinator.handle(notificationResponse)
return
}
// Handle activities from external sources (Handoff, Spotlight, Siri Shortcuts).
// Skip handling session.stateRestorationActivity since UserDefaults now handles state restoration.
if let userActivity = connectionOptions.userActivities.first {
coordinator.handle(userActivity)
}
}
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
appDelegate.resumeIfNecessary()
handleShortcutItem(shortcutItem)
completionHandler(true)
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
appDelegate.resumeIfNecessary()
coordinator.handle(userActivity)
}
func sceneDidEnterBackground(_ scene: UIScene) {
coordinator.didEnterBackground()
appDelegate.prepareAccountsForBackground()
}
func sceneWillEnterForeground(_ scene: UIScene) {
appDelegate.resumeIfNecessary()
appDelegate.prepareAccountsForForeground()
coordinator.resetFocus()
}
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return coordinator.stateRestorationActivity
}
// API
func handle(_ response: UNNotificationResponse) {
appDelegate.resumeIfNecessary()
coordinator.handle(response)
}
func suspend() {
coordinator.suspend()
}
func cleanUp(conditional: Bool) {
coordinator.cleanUp(conditional: conditional)
}
// Handle Opening of URLs
func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
guard let context = urlContexts.first else { return }
DispatchQueue.main.async {
// Theme URLs leave Settings open so the install prompt can appear over it.
let filename = context.url.standardizedFileURL.path
if filename.hasSuffix(ArticleTheme.nnwThemeSuffix) {
self.coordinator.importTheme(filename: filename)
return
}
// Handle theme URLs: netnewswire://theme/add?url={url}
if let urlComponents = URLComponents(url: context.url, resolvingAgainstBaseURL: false), urlComponents.scheme?.lowercased() == "netnewswire", urlComponents.host == "theme" {
if let providedThemeURL = urlComponents.queryItems?.first(where: { $0.name == "url" })?.value, let themeURL = URL(string: providedThemeURL) {
ArticleThemeDownloader.shared.downloadTheme(from: themeURL)
}
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.coordinator.dismissIfLaunchingFromExternalAction()
}
let urlString = context.url.absoluteString
// Handle the feed: and feeds: schemes
if urlString.starts(with: "feed:") || urlString.starts(with: "feeds:") {
let normalizedURLString = urlString.normalizedURL
if normalizedURLString.mayBeURL {
self.coordinator.showAddFeed(initialFeed: normalizedURLString, initialFeedName: nil)
}
return
}
// Show Unread View or Article
if urlString.contains(WidgetDeepLink.unread.url.absoluteString) {
guard let comps = URLComponents(string: urlString ) else { return }
let id = comps.queryItems?.first(where: { $0.name == "id" })?.value
if id != nil {
if AccountManager.shared.isSuspended {
AccountManager.shared.resumeAll()
}
self.coordinator.selectAllUnreadFeed {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
self.coordinator.selectArticleInCurrentFeed(id!)
}
}
} else {
self.coordinator.selectAllUnreadFeed()
}
return
}
// Show Today View or Article
if urlString.contains(WidgetDeepLink.today.url.absoluteString) {
guard let comps = URLComponents(string: urlString ) else { return }
let id = comps.queryItems?.first(where: { $0.name == "id" })?.value
if id != nil {
if AccountManager.shared.isSuspended {
AccountManager.shared.resumeAll()
}
self.coordinator.selectTodayFeed {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
self.coordinator.selectArticleInCurrentFeed(id!)
}
}
} else {
self.coordinator.selectTodayFeed()
}
return
}
// Show Starred View or Article
if urlString.contains(WidgetDeepLink.starred.url.absoluteString) {
guard let comps = URLComponents(string: urlString ) else { return }
let id = comps.queryItems?.first(where: { $0.name == "id" })?.value
if id != nil {
if AccountManager.shared.isSuspended {
AccountManager.shared.resumeAll()
}
self .coordinator.selectStarredFeed {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
self.coordinator.selectArticleInCurrentFeed(id!)
}
}
} else {
self.coordinator.selectStarredFeed()
}
return
}
}
}
}
private extension SceneDelegate {
func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
switch shortcutItem.type {
case "com.ranchero.NetNewsWire.FirstUnread":
coordinator.selectFirstUnreadInAllUnread()
case "com.ranchero.NetNewsWire.ShowSearch":
coordinator.showSearch()
case "com.ranchero.NetNewsWire.ShowAdd":
coordinator.showAddFeed()
default:
break
}
}
@objc func handleUserInterfaceColorPaletteDidUpdate(_ notification: Notification) {
assert(Thread.isMainThread)
Task {
updateUserInterfaceStyle()
}
}
@MainActor func updateUserInterfaceStyle() {
switch AppDefaults.userInterfaceColorPalette {
case .automatic:
self.window?.overrideUserInterfaceStyle = .unspecified
case .light:
self.window?.overrideUserInterfaceStyle = .light
case .dark:
self.window?.overrideUserInterfaceStyle = .dark
}
}
}