From 8dba15ca179b10602ce1cc38afaadfe6f52ba27c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 7 Jan 2020 18:39:19 -0500 Subject: [PATCH] Switch to scene-based lifecycle events See #16 --- Tusker.xcodeproj/project.pbxproj | 4 + Tusker/AppDelegate.swift | 110 ---------------- Tusker/Info.plist | 61 +++++---- Tusker/SceneDelegate.swift | 138 +++++++++++++++++++++ Tusker/Shortcuts/AppShortcutItems.swift | 4 +- Tusker/Shortcuts/UserActivityManager.swift | 7 +- Tusker/XCallbackURL/XCBActions.swift | 5 +- 7 files changed, 195 insertions(+), 134 deletions(-) create mode 100644 Tusker/SceneDelegate.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 44d85d1e..adf1defc 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -174,6 +174,7 @@ D6A5BB2F23BBAC97003BF21D /* DelegatingResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A5BB2E23BBAC97003BF21D /* DelegatingResponse.swift */; }; D6A5BB3123BBAD87003BF21D /* JSONResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A5BB3023BBAD87003BF21D /* JSONResponse.swift */; }; D6A5FAF1217B7E05003DB2D9 /* ComposeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D6A5FAF0217B7E05003DB2D9 /* ComposeViewController.xib */; }; + D6AC956723C4347E008C9946 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AC956623C4347E008C9946 /* SceneDelegate.swift */; }; D6AEBB3E2321638100E5038B /* UIActivity+Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AEBB3D2321638100E5038B /* UIActivity+Types.swift */; }; D6AEBB412321642700E5038B /* SendMesasgeActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AEBB402321642700E5038B /* SendMesasgeActivity.swift */; }; D6AEBB432321685E00E5038B /* OpenInSafariActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6AEBB422321685E00E5038B /* OpenInSafariActivity.swift */; }; @@ -446,6 +447,7 @@ D6A5BB2E23BBAC97003BF21D /* DelegatingResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DelegatingResponse.swift; sourceTree = ""; }; D6A5BB3023BBAD87003BF21D /* JSONResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONResponse.swift; sourceTree = ""; }; D6A5FAF0217B7E05003DB2D9 /* ComposeViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ComposeViewController.xib; sourceTree = ""; }; + D6AC956623C4347E008C9946 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; D6AEBB3D2321638100E5038B /* UIActivity+Types.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIActivity+Types.swift"; sourceTree = ""; }; D6AEBB402321642700E5038B /* SendMesasgeActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendMesasgeActivity.swift; sourceTree = ""; }; D6AEBB422321685E00E5038B /* OpenInSafariActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenInSafariActivity.swift; sourceTree = ""; }; @@ -1179,6 +1181,7 @@ isa = PBXGroup; children = ( D6D4DDCF212518A000E1C4BB /* AppDelegate.swift */, + D6AC956623C4347E008C9946 /* SceneDelegate.swift */, D64D0AAC2128D88B005A6F37 /* LocalData.swift */, D627FF75217E923E00CC0648 /* DraftsManager.swift */, D6945C2E23AC47C3005C403C /* SavedHashtagsManager.swift */, @@ -1640,6 +1643,7 @@ D627943723A552C200D38C68 /* BookmarkStatusActivity.swift in Sources */, D62D2426217ABF63005076CC /* UserActivityType.swift in Sources */, D66362712136338600C9CBA2 /* ComposeViewController.swift in Sources */, + D6AC956723C4347E008C9946 /* SceneDelegate.swift in Sources */, D6BC9DD7232D7811002CA326 /* TimelinesPageViewController.swift in Sources */, D6028B9B2150811100F223B9 /* MastodonCache.swift in Sources */, D6A3BC802321B7E600FD64D5 /* FollowNotificationGroupTableViewCell.swift in Sources */, diff --git a/Tusker/AppDelegate.swift b/Tusker/AppDelegate.swift index 6f7521aa..1854af62 100644 --- a/Tusker/AppDelegate.swift +++ b/Tusker/AppDelegate.swift @@ -11,119 +11,9 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - - let mastodonController = MastodonController.shared - func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { AppShortcutItem.createItems(for: application) - - window = UIWindow(frame: UIScreen.main.bounds) - - if LocalData.shared.onboardingComplete { - showAppUI() - } else { - showOnboardingUI() - } - - NotificationCenter.default.addObserver(self, selector: #selector(onUserLoggedOut), name: .userLoggedOut, object: nil) - - NotificationCenter.default.addObserver(self, selector: #selector(themePrefChanged), name: .themePreferenceChanged, object: nil) - themePrefChanged() - - window!.makeKeyAndVisible() - - if let shortcutItem = launchOptions?[.shortcutItem] as? UIApplicationShortcutItem { - _ = AppShortcutItem.handle(shortcutItem) - } - return true } - func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { - if url.host == "x-callback-url" { - return XCBManager.handle(url: url) - } else if var components = URLComponents(url: url, resolvingAgainstBaseURL: false), - let tabBarController = window!.rootViewController as? MainTabBarViewController, - let exploreNavController = tabBarController.getTabController(tab: .explore) as? UINavigationController, - let exploreController = exploreNavController.viewControllers.first as? ExploreViewController { - - tabBarController.select(tab: .explore) - exploreNavController.popToRootViewController(animated: false) - - exploreController.loadViewIfNeeded() - exploreController.searchController.isActive = true - - components.scheme = "https" - let query = components.url!.absoluteString - exploreController.searchController.searchBar.text = query - exploreController.resultsController.performSearch(query: query) - - return true - } - return false - } - - func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { - return userActivity.handleResume() - } - - func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { - completionHandler(AppShortcutItem.handle(shortcutItem)) - } - - func applicationWillResignActive(_ application: UIApplication) { - // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. - // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. - } - - func applicationDidEnterBackground(_ application: UIApplication) { - // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. - // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. - Preferences.save() - DraftsManager.save() - } - - func applicationWillEnterForeground(_ application: UIApplication) { - // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. - } - - func applicationDidBecomeActive(_ application: UIApplication) { - // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. - } - - func applicationWillTerminate(_ application: UIApplication) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - } - - func showAppUI() { - mastodonController.createClient() - mastodonController.getOwnAccount() - mastodonController.getOwnInstance() - - let tabBarController = MainTabBarViewController(mastodonController: mastodonController) - window!.rootViewController = tabBarController - } - - func showOnboardingUI() { - let onboarding = OnboardingViewController() - onboarding.onboardingDelegate = self - window!.rootViewController = onboarding - } - - @objc func onUserLoggedOut() { - showOnboardingUI() - } - - @objc func themePrefChanged() { - window?.overrideUserInterfaceStyle = Preferences.shared.theme - } - -} - -extension AppDelegate: OnboardingViewControllerDelegate { - func didFinishOnboarding() { - LocalData.shared.onboardingComplete = true - showAppUI() - } } diff --git a/Tusker/Info.plist b/Tusker/Info.plist index 937a979e..48f051d6 100644 --- a/Tusker/Info.plist +++ b/Tusker/Info.plist @@ -2,25 +2,6 @@ - NSAppTransportSecurity - - NSExceptionDomains - - localhost - - NSExceptionAllowsInsecureHTTPLoads - - - - - NSUserActivityTypes - - $(PRODUCT_BUNDLE_IDENTIFIER).activity.show-timeline - $(PRODUCT_BUNDLE_IDENTIFIER).activity.check-notifications - $(PRODUCT_BUNDLE_IDENTIFIER).activity.check-mentions - $(PRODUCT_BUNDLE_IDENTIFIER).activity.new-post - $(PRODUCT_BUNDLE_IDENTIFIER).activity.search - CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -52,14 +33,52 @@ 1 LSRequiresIPhoneOS - NSMicrophoneUsageDescription - Post videos from the camera. + NSAppTransportSecurity + + NSExceptionDomains + + localhost + + NSExceptionAllowsInsecureHTTPLoads + + + + NSCameraUsageDescription Post photos and videos from the camera. + NSMicrophoneUsageDescription + Post videos from the camera. NSPhotoLibraryAddUsageDescription Save photos directly from other people's posts. NSPhotoLibraryUsageDescription Post photos from the photo library. + NSUserActivityTypes + + $(PRODUCT_BUNDLE_IDENTIFIER).activity.show-timeline + $(PRODUCT_BUNDLE_IDENTIFIER).activity.check-notifications + $(PRODUCT_BUNDLE_IDENTIFIER).activity.check-mentions + $(PRODUCT_BUNDLE_IDENTIFIER).activity.new-post + $(PRODUCT_BUNDLE_IDENTIFIER).activity.search + + UIApplicationSceneManifest + + UISceneConfigurations + + UIWindowSceneSessionRoleApplication + + + UISceneClassName + UIWindowScene + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).SceneDelegate + UISceneConfigurationName + main-scene + + + + UIApplicationSupportsMultipleScenes + + UILaunchStoryboardName LaunchScreen UIRequiredDeviceCapabilities diff --git a/Tusker/SceneDelegate.swift b/Tusker/SceneDelegate.swift new file mode 100644 index 00000000..5e3d7ac2 --- /dev/null +++ b/Tusker/SceneDelegate.swift @@ -0,0 +1,138 @@ +// +// SceneDelegate.swift +// Tusker +// +// Created by Shadowfacts on 1/6/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import UIKit + +class SceneDelegate: UIResponder, UIWindowSceneDelegate { + + var window: UIWindow? + + let mastodonController = MastodonController.shared + + func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { + // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. + // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. + // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). + guard let windowScene = scene as? UIWindowScene else { return } + + window = UIWindow(windowScene: windowScene) + + if LocalData.shared.onboardingComplete { + showAppUI() + } else { + showOnboardingUI() + } + + window!.makeKeyAndVisible() + + NotificationCenter.default.addObserver(self, selector: #selector(onUserLoggedOut), name: .userLoggedOut, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(themePrefChanged), name: .themePreferenceChanged, object: nil) + themePrefChanged() + + if let shortcutItem = connectionOptions.shortcutItem { + _ = AppShortcutItem.handle(shortcutItem) + } + } + + func scene(_ scene: UIScene, openURLContexts URLContexts: Set) { + if URLContexts.count > 1 { + fatalError("Cannot open more than 1 URL") + } + + let url = URLContexts.first!.url + + if url.host == "x-callback-url" { + _ = XCBManager.handle(url: url) + } else if var components = URLComponents(url: url, resolvingAgainstBaseURL: false), + let tabBarController = window!.rootViewController as? MainTabBarViewController, + let exploreNavController = tabBarController.getTabController(tab: .explore) as? UINavigationController, + let exploreController = exploreNavController.viewControllers.first as? ExploreViewController { + + tabBarController.select(tab: .explore) + exploreNavController.popToRootViewController(animated: false) + + exploreController.loadViewIfNeeded() + exploreController.searchController.isActive = true + + components.scheme = "https" + let query = url.absoluteString + exploreController.searchController.searchBar.text = query + exploreController.resultsController.performSearch(query: query) + } + } + + func scene(_ scene: UIScene, continue userActivity: NSUserActivity) { + _ = userActivity.handleResume() + } + + func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { + completionHandler(AppShortcutItem.handle(shortcutItem)) + } + + func sceneDidDisconnect(_ scene: UIScene) { + // Called as the scene is being released by the system. + // This occurs shortly after the scene enters the background, or when its session is discarded. + // Release any resources associated with this scene that can be re-created the next time the scene connects. + // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). + } + + func sceneDidBecomeActive(_ scene: UIScene) { + // Called when the scene has moved from an inactive state to an active state. + // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. + } + + func sceneWillResignActive(_ scene: UIScene) { + // Called when the scene will move from an active state to an inactive state. + // This may occur due to temporary interruptions (ex. an incoming phone call). + } + + func sceneWillEnterForeground(_ scene: UIScene) { + // Called as the scene transitions from the background to the foreground. + // Use this method to undo the changes made on entering the background. + } + + func sceneDidEnterBackground(_ scene: UIScene) { + // Called as the scene transitions from the foreground to the background. + // Use this method to save data, release shared resources, and store enough scene-specific state information + // to restore the scene back to its current state. + + Preferences.save() + DraftsManager.save() + } + + func showAppUI() { + mastodonController.createClient() + mastodonController.getOwnAccount() + mastodonController.getOwnInstance() + + let tabBarController = MainTabBarViewController(mastodonController: mastodonController) + window!.rootViewController = tabBarController + } + + func showOnboardingUI() { + let onboarding = OnboardingViewController() + onboarding.onboardingDelegate = self + window!.rootViewController = onboarding + } + + @objc func onUserLoggedOut() { + showOnboardingUI() + } + + @objc func themePrefChanged() { + window?.overrideUserInterfaceStyle = Preferences.shared.theme + } + +} + +extension SceneDelegate: OnboardingViewControllerDelegate { + func didFinishOnboarding() { + LocalData.shared.onboardingComplete = true + showAppUI() + } +} diff --git a/Tusker/Shortcuts/AppShortcutItems.swift b/Tusker/Shortcuts/AppShortcutItems.swift index 0e9dea80..d0efdffa 100644 --- a/Tusker/Shortcuts/AppShortcutItems.swift +++ b/Tusker/Shortcuts/AppShortcutItems.swift @@ -45,7 +45,9 @@ enum AppShortcutItem: String, CaseIterable { case .composePost: tab = .compose } - let controller = (UIApplication.shared.delegate!.window!!.rootViewController as! MainTabBarViewController) + let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first! + let window = scene.windows.first { $0.isKeyWindow }! + let controller = window.rootViewController as! MainTabBarViewController controller.select(tab: tab) } } diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index 5701e1f9..4b745f40 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -18,7 +18,10 @@ class UserActivityManager { private static var mastodonController: MastodonController { .shared } private static func getMainTabBarController() -> MainTabBarViewController { - return (UIApplication.shared.delegate! as! AppDelegate).window!.rootViewController as! MainTabBarViewController + let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first! + let window = scene.windows.first { $0.isKeyWindow }! + return window.rootViewController as! MainTabBarViewController +// return (UIApplication.shared.delegate! as! AppDelegate).window!.rootViewController as! MainTabBarViewController } private static func present(_ vc: UIViewController, animated: Bool = true) { @@ -63,6 +66,7 @@ class UserActivityManager { if let navigationController = tabBarController.getTabController(tab: .notifications) as? UINavigationController, let notificationsPageController = navigationController.viewControllers.first as? NotificationsPageViewController { navigationController.popToRootViewController(animated: false) + notificationsPageController.loadViewIfNeeded() notificationsPageController.selectMode(.allNotifications) } } @@ -82,6 +86,7 @@ class UserActivityManager { if let navController = tabBarController.getTabController(tab: .notifications) as? UINavigationController, let notificationsPageController = navController.viewControllers.first as? NotificationsPageViewController { navController.popToRootViewController(animated: false) + notificationsPageController.loadViewIfNeeded() notificationsPageController.selectMode(.mentionsOnly) } } diff --git a/Tusker/XCallbackURL/XCBActions.swift b/Tusker/XCallbackURL/XCBActions.swift index 426c4f55..ad2feb5f 100644 --- a/Tusker/XCallbackURL/XCBActions.swift +++ b/Tusker/XCallbackURL/XCBActions.swift @@ -16,7 +16,10 @@ struct XCBActions { private static var mastodonController: MastodonController { .shared } private static func getMainTabBarController() -> MainTabBarViewController { - return (UIApplication.shared.delegate as! AppDelegate).window!.rootViewController as! MainTabBarViewController + let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first! + let window = scene.windows.first { $0.isKeyWindow }! + return window.rootViewController as! MainTabBarViewController +// return (UIApplication.shared.delegate as! AppDelegate).window!.rootViewController as! MainTabBarViewController } private static func show(_ vc: UIViewController) {