NetNewsWire/iOS/SceneDelegate.swift

127 lines
3.4 KiB
Swift
Raw Normal View History

2019-06-28 17:28:02 +02:00
//
// AppDelegate.swift
// NetNewsWire
//
// Created by Maurice Parker on 6/28/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2019-10-03 16:53:21 +02:00
import UserNotifications
2019-09-02 22:45:09 +02:00
import Account
2019-06-28 17:28:02 +02:00
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
2019-06-28 17:28:02 +02:00
var window: UIWindow?
var coordinator = SceneCoordinator()
2019-06-28 17:28:02 +02:00
// UIWindowScene delegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
window = UIWindow(windowScene: scene as! UIWindowScene)
window!.tintColor = AppAssets.primaryAccentColor
updateUserInterfaceStyle()
window!.rootViewController = coordinator.start(for: window!.frame.size)
2019-06-28 17:28:02 +02:00
coordinator.restoreWindowState(session.stateRestorationActivity)
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil)
2019-09-01 23:54:07 +02:00
if let shortcutItem = connectionOptions.shortcutItem {
window!.makeKeyAndVisible()
2019-09-01 23:54:07 +02:00
handleShortcutItem(shortcutItem)
return
}
2019-10-03 16:53:21 +02:00
if let notificationResponse = connectionOptions.notificationResponse {
window!.makeKeyAndVisible()
coordinator.handle(notificationResponse)
2019-10-03 16:53:21 +02:00
return
}
if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
coordinator.handle(userActivity)
}
window!.makeKeyAndVisible()
2019-06-28 17:28:02 +02:00
}
2019-09-01 23:54:07 +02:00
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
appDelegate.resumeDatabaseProcessingIfNecessary()
2019-09-01 23:54:07 +02:00
handleShortcutItem(shortcutItem)
completionHandler(true)
}
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
appDelegate.resumeDatabaseProcessingIfNecessary()
coordinator.handle(userActivity)
}
2019-06-28 17:28:02 +02:00
func sceneDidEnterBackground(_ scene: UIScene) {
ArticleStringFormatter.emptyCaches()
2019-06-28 17:28:02 +02:00
appDelegate.prepareAccountsForBackground()
}
func sceneWillEnterForeground(_ scene: UIScene) {
appDelegate.resumeDatabaseProcessingIfNecessary()
2019-06-28 17:28:02 +02:00
appDelegate.prepareAccountsForForeground()
coordinator.configurePanelMode(for: window!.frame.size)
coordinator.resetFocus()
2019-06-28 17:28:02 +02:00
}
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return coordinator.stateRestorationActivity
}
2019-10-03 16:53:21 +02:00
// API
func handle(_ response: UNNotificationResponse) {
appDelegate.resumeDatabaseProcessingIfNecessary()
coordinator.handle(response)
2019-10-03 16:53:21 +02:00
}
2019-06-28 17:28:02 +02:00
func suspend() {
coordinator.suspend()
}
func cleanUp(conditional: Bool) {
coordinator.cleanUp(conditional: conditional)
}
2019-06-28 17:28:02 +02:00
}
2019-09-01 23:54:07 +02:00
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":
2020-08-11 23:27:42 +02:00
coordinator.showAddWebFeed()
2019-09-01 23:54:07 +02:00
default:
break
}
}
@objc func userDefaultsDidChange() {
updateUserInterfaceStyle()
}
func updateUserInterfaceStyle() {
DispatchQueue.main.async {
switch AppDefaults.userInterfaceColorPalette {
case .automatic:
self.window!.overrideUserInterfaceStyle = .unspecified
case .light:
self.window!.overrideUserInterfaceStyle = .light
case .dark:
self.window!.overrideUserInterfaceStyle = .dark
}
}
}
2019-09-01 23:54:07 +02:00
}