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
|
2021-09-19 11:48:29 +02:00
|
|
|
import Zip
|
2019-06-28 17:28:02 +02:00
|
|
|
|
2021-09-20 03:36:09 +02:00
|
|
|
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
2021-09-19 11:48:29 +02:00
|
|
|
var window: UIWindow?
|
2019-09-01 19:43:07 +02:00
|
|
|
var coordinator = SceneCoordinator()
|
2019-07-19 18:59:08 +02:00
|
|
|
|
2021-09-19 11:48:29 +02:00
|
|
|
// UIWindowScene delegate
|
|
|
|
|
|
|
|
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
|
2019-09-06 17:29:00 +02:00
|
|
|
|
2019-07-26 16:58:46 +02:00
|
|
|
window = UIWindow(windowScene: scene as! UIWindowScene)
|
2019-09-18 09:49:57 +02:00
|
|
|
window!.tintColor = AppAssets.primaryAccentColor
|
2020-03-15 10:25:25 +01:00
|
|
|
updateUserInterfaceStyle()
|
2019-09-09 23:59:24 +02:00
|
|
|
window!.rootViewController = coordinator.start(for: window!.frame.size)
|
2020-11-19 08:52:43 +01:00
|
|
|
|
2019-11-27 03:23:12 +01:00
|
|
|
coordinator.restoreWindowState(session.stateRestorationActivity)
|
2019-11-26 23:33:11 +01:00
|
|
|
|
2020-03-15 10:25:25 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil)
|
|
|
|
|
2020-11-19 10:29:03 +01:00
|
|
|
if let _ = connectionOptions.urlContexts.first?.url {
|
|
|
|
window?.makeKeyAndVisible()
|
|
|
|
self.scene(scene, openURLContexts: connectionOptions.urlContexts)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-01 23:54:07 +02:00
|
|
|
if let shortcutItem = connectionOptions.shortcutItem {
|
2019-09-06 17:29:00 +02:00
|
|
|
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()
|
2020-02-18 02:40:40 +01:00
|
|
|
coordinator.handle(notificationResponse)
|
2019-10-03 16:53:21 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-19 11:48:29 +02:00
|
|
|
if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
|
2020-02-18 02:40:40 +01:00
|
|
|
coordinator.handle(userActivity)
|
2020-11-19 08:52:43 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:29:00 +02:00
|
|
|
window!.makeKeyAndVisible()
|
2021-09-19 11:48:29 +02:00
|
|
|
}
|
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) {
|
2020-01-11 00:32:06 +01:00
|
|
|
appDelegate.resumeDatabaseProcessingIfNecessary()
|
2019-09-01 23:54:07 +02:00
|
|
|
handleShortcutItem(shortcutItem)
|
|
|
|
completionHandler(true)
|
|
|
|
}
|
|
|
|
|
2019-08-25 02:31:29 +02:00
|
|
|
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
|
2020-01-11 00:32:06 +01:00
|
|
|
appDelegate.resumeDatabaseProcessingIfNecessary()
|
2020-02-18 02:40:40 +01:00
|
|
|
coordinator.handle(userActivity)
|
2019-08-25 02:31:29 +02:00
|
|
|
}
|
|
|
|
|
2019-06-28 17:28:02 +02:00
|
|
|
func sceneDidEnterBackground(_ scene: UIScene) {
|
2020-11-18 03:49:12 +01:00
|
|
|
if #available(iOS 14, *) {
|
2020-12-03 13:32:26 +01:00
|
|
|
try? WidgetDataEncoder.shared.encodeWidgetData()
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
2019-10-20 09:28:00 +02:00
|
|
|
ArticleStringFormatter.emptyCaches()
|
2019-06-28 17:28:02 +02:00
|
|
|
appDelegate.prepareAccountsForBackground()
|
|
|
|
}
|
|
|
|
|
|
|
|
func sceneWillEnterForeground(_ scene: UIScene) {
|
2020-01-11 00:32:06 +01:00
|
|
|
appDelegate.resumeDatabaseProcessingIfNecessary()
|
2019-06-28 17:28:02 +02:00
|
|
|
appDelegate.prepareAccountsForForeground()
|
2020-04-29 00:16:34 +02:00
|
|
|
coordinator.configurePanelMode(for: window!.frame.size)
|
|
|
|
coordinator.resetFocus()
|
2019-06-28 17:28:02 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 11:48:29 +02:00
|
|
|
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
|
2019-09-01 02:30:21 +02:00
|
|
|
return coordinator.stateRestorationActivity
|
2021-09-19 11:48:29 +02:00
|
|
|
}
|
2019-10-03 16:53:21 +02:00
|
|
|
|
|
|
|
// API
|
|
|
|
|
|
|
|
func handle(_ response: UNNotificationResponse) {
|
2020-01-11 00:32:06 +01:00
|
|
|
appDelegate.resumeDatabaseProcessingIfNecessary()
|
2020-02-18 02:40:40 +01:00
|
|
|
coordinator.handle(response)
|
2019-10-03 16:53:21 +02:00
|
|
|
}
|
2021-09-19 11:48:29 +02:00
|
|
|
|
2019-12-02 21:14:35 +01:00
|
|
|
func suspend() {
|
|
|
|
coordinator.suspend()
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:00:01 +01:00
|
|
|
func cleanUp(conditional: Bool) {
|
|
|
|
coordinator.cleanUp(conditional: conditional)
|
2020-03-11 21:47:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
// Handle Opening of URLs
|
|
|
|
|
2021-01-05 07:11:09 +01:00
|
|
|
func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
|
|
|
|
guard let context = urlContexts.first else { return }
|
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
DispatchQueue.main.async {
|
2021-01-05 07:11:09 +01:00
|
|
|
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.showAddWebFeed(initialFeed: normalizedURLString, initialFeedName: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
self.coordinator.selectArticleInCurrentFeed(id!)
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
2021-01-05 07:11:09 +01:00
|
|
|
} else {
|
|
|
|
self.coordinator.selectAllUnreadFeed()
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
2021-01-05 07:11:09 +01: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()
|
|
|
|
}
|
|
|
|
self.coordinator.selectTodayFeed() {
|
|
|
|
self.coordinator.selectArticleInCurrentFeed(id!)
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
2021-01-05 07:11:09 +01:00
|
|
|
} else {
|
|
|
|
self.coordinator.selectTodayFeed()
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
2021-01-05 07:11:09 +01: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()
|
|
|
|
}
|
|
|
|
self.coordinator.selectStarredFeed() {
|
|
|
|
self.coordinator.selectArticleInCurrentFeed(id!)
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
2021-01-05 07:11:09 +01:00
|
|
|
} else {
|
|
|
|
self.coordinator.selectStarredFeed()
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-05 07:11:09 +01:00
|
|
|
|
2021-09-12 21:46:15 +02:00
|
|
|
let filename = context.url.standardizedFileURL.path
|
|
|
|
if filename.hasSuffix(ArticleTheme.nnwThemeSuffix) {
|
|
|
|
self.coordinator.importTheme(filename: filename)
|
2021-09-19 11:48:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle theme URLs: netnewswire://theme/add?url={url}
|
|
|
|
guard let comps = URLComponents(url: context.url, resolvingAgainstBaseURL: false),
|
|
|
|
"theme" == comps.host,
|
|
|
|
let queryItems = comps.queryItems else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if let providedThemeURL = queryItems.first(where: { $0.name == "url" })?.value {
|
|
|
|
if let themeURL = URL(string: providedThemeURL) {
|
|
|
|
let request = URLRequest(url: themeURL)
|
2021-09-20 03:36:09 +02:00
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
NotificationCenter.default.post(name: .didBeginDownloadingTheme, object: nil)
|
|
|
|
}
|
|
|
|
let task = URLSession.shared.downloadTask(with: request) { [weak self] location, response, error in
|
|
|
|
guard let self = self, let location = location else { return }
|
|
|
|
self.createDownloadDirectoryIfRequired()
|
|
|
|
do {
|
|
|
|
let movedFileLocation = try self.moveTheme(from: location)
|
|
|
|
let unzippedFileLocation = try self.unzipFile(at: movedFileLocation)
|
|
|
|
let renamedFileLocation = try self.renameFileToThemeName(at: unzippedFileLocation)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
NotificationCenter.default.post(name: .didEndDownloadingTheme, object: nil)
|
|
|
|
self.coordinator.importTheme(filename: renamedFileLocation.path)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
NotificationCenter.default.post(name: .didEndDownloadingThemeWithError, object: nil, userInfo: ["error" : error])
|
|
|
|
self.showAlert(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
task.resume()
|
2021-09-19 11:48:29 +02:00
|
|
|
} else {
|
|
|
|
print("No theme URL")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
2021-09-12 21:46:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-18 03:49:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 03:36:09 +02:00
|
|
|
// MARK: - Theme Downloader
|
|
|
|
private func createDownloadDirectoryIfRequired() {
|
|
|
|
let downloadDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
|
|
|
|
try? FileManager.default.createDirectory(at: downloadDirectory, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
}
|
2021-09-19 11:48:29 +02:00
|
|
|
|
2021-09-20 03:36:09 +02:00
|
|
|
private func moveTheme(from location: URL) throws -> URL {
|
2021-09-19 11:48:29 +02:00
|
|
|
var downloadDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
|
|
|
|
let tmpFileName = UUID().uuidString + ".zip"
|
|
|
|
downloadDirectory.appendPathComponent("\(tmpFileName)")
|
2021-09-20 03:36:09 +02:00
|
|
|
try FileManager.default.moveItem(at: location, to: downloadDirectory)
|
|
|
|
return downloadDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
private func unzipFile(at location: URL) throws -> URL {
|
|
|
|
var unzippedDir = location.deletingLastPathComponent()
|
|
|
|
unzippedDir.appendPathComponent("newtheme.nnwtheme")
|
2021-09-20 03:48:31 +02:00
|
|
|
do {
|
|
|
|
try Zip.unzipFile(location, destination: unzippedDir, overwrite: true, password: nil, progress: nil, fileOutputHandler: nil)
|
|
|
|
try FileManager.default.removeItem(at: location)
|
|
|
|
return unzippedDir
|
|
|
|
} catch {
|
|
|
|
try? FileManager.default.removeItem(at: location)
|
|
|
|
throw error
|
|
|
|
}
|
2021-09-20 03:36:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private func renameFileToThemeName(at location: URL) throws -> URL {
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
let plistURL = URL(fileURLWithPath: location.appendingPathComponent("Info.plist").path)
|
|
|
|
let data = try Data(contentsOf: plistURL)
|
|
|
|
let plist = try decoder.decode(ArticleThemePlist.self, from: data)
|
|
|
|
var renamedUnzippedDir = location.deletingLastPathComponent()
|
|
|
|
renamedUnzippedDir.appendPathComponent(plist.name + ".nnwtheme")
|
|
|
|
if FileManager.default.fileExists(atPath: renamedUnzippedDir.path) {
|
|
|
|
try FileManager.default.removeItem(at: renamedUnzippedDir)
|
2021-09-19 11:48:29 +02:00
|
|
|
}
|
2021-09-20 03:36:09 +02:00
|
|
|
try FileManager.default.moveItem(at: location, to: renamedUnzippedDir)
|
|
|
|
return renamedUnzippedDir
|
2021-09-19 11:48:29 +02:00
|
|
|
}
|
|
|
|
|
2021-09-20 03:36:09 +02:00
|
|
|
private func showAlert(_ error: Error) {
|
|
|
|
let alert = UIAlertController(title: NSLocalizedString("Error", comment: "Error"),
|
|
|
|
message: error.localizedDescription,
|
|
|
|
preferredStyle: .alert)
|
|
|
|
alert.addAction(UIAlertAction(title: NSLocalizedString("Dismiss", comment: "Dismiss"), style: .cancel, handler: nil))
|
|
|
|
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
|
|
|
|
}
|
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()
|
2019-09-02 00:41:46 +02:00
|
|
|
case "com.ranchero.NetNewsWire.ShowSearch":
|
|
|
|
coordinator.showSearch()
|
2019-09-02 22:14:26 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 10:25:25 +01:00
|
|
|
@objc func userDefaultsDidChange() {
|
|
|
|
updateUserInterfaceStyle()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUserInterfaceStyle() {
|
2020-07-28 02:35:41 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
switch AppDefaults.userInterfaceColorPalette {
|
|
|
|
case .automatic:
|
2020-10-20 20:45:28 +02:00
|
|
|
self.window?.overrideUserInterfaceStyle = .unspecified
|
2020-07-28 02:35:41 +02:00
|
|
|
case .light:
|
2020-10-20 20:45:28 +02:00
|
|
|
self.window?.overrideUserInterfaceStyle = .light
|
2020-07-28 02:35:41 +02:00
|
|
|
case .dark:
|
2020-10-20 20:45:28 +02:00
|
|
|
self.window?.overrideUserInterfaceStyle = .dark
|
2020-07-28 02:35:41 +02:00
|
|
|
}
|
2020-03-15 10:25:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-01 23:54:07 +02:00
|
|
|
}
|