Files
NetNewsWire/iOS/SceneDelegate.swift
T

250 lines
7.8 KiB
Swift
Raw Normal View History

2019-06-28 10:28:02 -05:00
//
// AppDelegate.swift
// NetNewsWire
//
// Created by Maurice Parker on 6/28/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2019-10-03 09:53:21 -05:00
import UserNotifications
2019-09-02 15:45:09 -05:00
import Account
2019-06-28 10:28:02 -05:00
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
2025-11-05 11:22:10 -08:00
2021-09-19 17:48:29 +08:00
var window: UIWindow?
var coordinator: SceneCoordinator!
2025-11-05 11:22:10 -08:00
2021-09-19 17:48:29 +08:00
// UIWindowScene delegate
2025-11-05 11:22:10 -08:00
2021-09-19 17:48:29 +08:00
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
2025-11-05 11:22:10 -08:00
2025-11-19 10:38:18 -08:00
window!.tintColor = Assets.Colors.primaryAccent
2025-11-05 11:22:10 -08:00
let rootViewController = window!.rootViewController as! RootSplitViewController
rootViewController.presentsWithGesture = true
rootViewController.showsSecondaryOnlyButton = true
rootViewController.preferredDisplayMode = UISplitViewController.DisplayMode(rawValue: AppDefaults.splitViewPreferredDisplayMode) ?? .oneBesideSecondary
2026-01-31 20:59:07 -08:00
// On first run on iPad, show all three columns so the sidebar is visible
if AppDefaults.isFirstRun && UIDevice.current.userInterfaceIdiom == .pad {
2026-01-31 20:59:07 -08:00
rootViewController.preferredDisplayMode = .twoBesideSecondary
}
coordinator = SceneCoordinator(rootSplitViewController: rootViewController)
rootViewController.coordinator = coordinator
rootViewController.delegate = coordinator
2025-11-05 11:22:10 -08:00
// 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)
2026-01-02 17:50:35 -08:00
if connectionOptions.urlContexts.first?.url != nil {
2020-11-19 17:29:03 +08:00
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
return
}
2025-11-05 11:22:10 -08:00
2019-09-01 16:54:07 -05:00
if let shortcutItem = connectionOptions.shortcutItem {
handleShortcutItem(shortcutItem)
return
}
2025-11-05 11:22:10 -08:00
2019-10-03 09:53:21 -05:00
if let notificationResponse = connectionOptions.notificationResponse {
coordinator.handle(notificationResponse)
2019-10-03 09:53:21 -05:00
return
}
2025-11-05 11:22:10 -08:00
2025-12-04 21:03:36 -08:00
// 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)
}
2021-09-19 17:48:29 +08:00
}
2025-11-05 11:22:10 -08:00
2019-09-01 16:54:07 -05:00
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
appDelegate.resumeIfNecessary()
2019-09-01 16:54:07 -05:00
handleShortcutItem(shortcutItem)
completionHandler(true)
}
2025-11-05 11:22:10 -08:00
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
appDelegate.resumeIfNecessary()
coordinator.handle(userActivity)
}
2025-11-05 11:22:10 -08:00
2019-06-28 10:28:02 -05:00
func sceneDidEnterBackground(_ scene: UIScene) {
coordinator.didEnterBackground()
2019-06-28 10:28:02 -05:00
appDelegate.prepareAccountsForBackground()
}
2024-12-04 18:39:16 -08:00
2019-06-28 10:28:02 -05:00
func sceneWillEnterForeground(_ scene: UIScene) {
appDelegate.resumeIfNecessary()
2019-06-28 10:28:02 -05:00
appDelegate.prepareAccountsForForeground()
coordinator.resetFocus()
2019-06-28 10:28:02 -05:00
}
2025-11-05 11:22:10 -08:00
2021-09-19 17:48:29 +08:00
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return coordinator.stateRestorationActivity
2021-09-19 17:48:29 +08:00
}
2025-11-05 11:22:10 -08:00
2019-10-03 09:53:21 -05:00
// API
2025-11-05 11:22:10 -08:00
2019-10-03 09:53:21 -05:00
func handle(_ response: UNNotificationResponse) {
appDelegate.resumeIfNecessary()
coordinator.handle(response)
2019-10-03 09:53:21 -05:00
}
2025-11-05 11:22:10 -08:00
func suspend() {
coordinator.suspend()
}
2025-11-05 11:22:10 -08:00
func cleanUp(conditional: Bool) {
coordinator.cleanUp(conditional: conditional)
}
2025-11-05 11:22:10 -08:00
2020-11-18 10:49:12 +08:00
// Handle Opening of URLs
2025-11-05 11:22:10 -08:00
func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
guard let context = urlContexts.first else { return }
2025-11-05 11:22:10 -08:00
2020-11-18 10:49:12 +08:00
DispatchQueue.main.async {
2025-11-05 11:22:10 -08:00
2021-11-06 22:43:50 +08:00
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
2021-11-08 09:52:12 +08:00
self.coordinator.dismissIfLaunchingFromExternalAction()
2021-11-06 22:43:50 +08:00
}
2025-11-05 11:22:10 -08:00
let urlString = context.url.absoluteString
2025-11-05 11:22:10 -08:00
// Handle the feed: and feeds: schemes
if urlString.starts(with: "feed:") || urlString.starts(with: "feeds:") {
let normalizedURLString = urlString.normalizedURL
if normalizedURLString.mayBeURL {
2025-11-03 09:32:50 -08:00
self.coordinator.showAddFeed(initialFeed: normalizedURLString, initialFeedName: nil)
2020-11-18 10:49:12 +08:00
}
2026-08-31 20:06:15 -07:00
return
2020-11-18 10:49:12 +08:00
}
2025-11-05 11:22:10 -08:00
// 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()
}
2026-01-02 17:50:35 -08:00
self.coordinator.selectAllUnreadFeed {
2026-01-06 14:02:18 +08:00
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
2026-01-08 16:31:58 +08:00
self.coordinator.selectArticleInCurrentFeed(id!)
2026-01-06 14:02:18 +08:00
}
}
} else {
self.coordinator.selectAllUnreadFeed()
}
2026-08-31 20:06:15 -07:00
return
}
2025-11-05 11:22:10 -08:00
// 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()
}
2026-01-08 16:31:58 +08:00
self.coordinator.selectTodayFeed {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
2026-01-08 12:38:34 +08:00
self.coordinator.selectArticleInCurrentFeed(id!)
}
}
} else {
self.coordinator.selectTodayFeed()
}
2026-08-31 20:06:15 -07:00
return
}
2025-11-05 11:22:10 -08:00
// 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()
}
2026-01-08 16:31:58 +08:00
self .coordinator.selectStarredFeed {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
2026-01-08 12:38:34 +08:00
self.coordinator.selectArticleInCurrentFeed(id!)
}
}
} else {
self.coordinator.selectStarredFeed()
}
2026-08-31 20:06:15 -07:00
return
}
2025-11-05 11:22:10 -08:00
2021-09-12 14:46:15 -05:00
let filename = context.url.standardizedFileURL.path
if filename.hasSuffix(ArticleTheme.nnwThemeSuffix) {
self.coordinator.importTheme(filename: filename)
2021-09-19 17:48:29 +08:00
return
2021-09-12 14:46:15 -05:00
}
2025-11-05 11:22:10 -08:00
2021-09-19 17:48:29 +08:00
// Handle theme URLs: netnewswire://theme/add?url={url}
guard let comps = URLComponents(url: context.url, resolvingAgainstBaseURL: false),
2026-08-31 20:06:15 -07:00
comps.scheme?.lowercased() == "netnewswire",
2021-09-19 17:48:29 +08:00
"theme" == comps.host,
let queryItems = comps.queryItems else {
return
}
2025-11-05 11:22:10 -08:00
if let providedThemeURL = queryItems.first(where: { $0.name == "url" })?.value,
let themeURL = URL(string: providedThemeURL) {
ArticleThemeDownloader.shared.downloadTheme(from: themeURL)
2021-09-19 17:48:29 +08:00
}
}
}
2019-06-28 10:28:02 -05:00
}
2019-09-01 16:54:07 -05:00
private extension SceneDelegate {
2025-11-05 11:22:10 -08:00
2019-09-01 16:54:07 -05:00
func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
switch shortcutItem.type {
case "com.ranchero.NetNewsWire.FirstUnread":
coordinator.selectFirstUnreadInAllUnread()
2019-09-01 17:41:46 -05:00
case "com.ranchero.NetNewsWire.ShowSearch":
coordinator.showSearch()
case "com.ranchero.NetNewsWire.ShowAdd":
2025-11-03 09:32:50 -08:00
coordinator.showAddFeed()
2019-09-01 16:54:07 -05:00
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
}
}
2019-09-01 16:54:07 -05:00
}