From 3413dff8f9e16044fd7b7b896e56f12b392ae47b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 11 Jun 2021 10:50:31 -0400 Subject: [PATCH] Present compose screen in new window on iOS 15 and iPad/Mac --- Tusker/ComposeSceneDelegate.swift | 30 +++++++++++++++---- .../Main/MainSplitViewController.swift | 15 +++++++--- .../Main/MainTabBarViewController.swift | 15 +++++++--- Tusker/Shortcuts/UserActivityManager.swift | 1 + Tusker/TuskerNavigationDelegate.swift | 17 +++++++---- 5 files changed, 59 insertions(+), 19 deletions(-) diff --git a/Tusker/ComposeSceneDelegate.swift b/Tusker/ComposeSceneDelegate.swift index 3b20561f..1c3b339d 100644 --- a/Tusker/ComposeSceneDelegate.swift +++ b/Tusker/ComposeSceneDelegate.swift @@ -17,21 +17,39 @@ class ComposeSceneDelegate: UIResponder, UIWindowSceneDelegate { return } + guard LocalData.shared.onboardingComplete else { + UIApplication.shared.requestSceneSessionDestruction(session, options: nil, errorHandler: nil) + return + } + let account: LocalData.UserAccountInfo + let controller: MastodonController let draft: Draft? - if let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity, - let activityAccount = UserActivityManager.getAccount(from: activity) { - account = activityAccount - draft = UserActivityManager.getDraft(from: activity) + if let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity { + if let activityAccount = UserActivityManager.getAccount(from: activity) { + account = activityAccount + } else { + // todo: this potentially changes the account for the draft, should show the same warning to user as in the drafts selection screen + account = LocalData.shared.getMostRecentAccount()! + } + + controller = MastodonController.getForAccount(account) + + if let activityDraft = UserActivityManager.getDraft(from: activity) { + draft = activityDraft + } else if let mentioning = activity.userInfo?["mentioning"] as? String { + draft = controller.createDraft(inReplyToID: nil, mentioningAcct: mentioning) + } else { + draft = nil + } } else { account = LocalData.shared.getMostRecentAccount()! + controller = MastodonController.getForAccount(account) draft = nil } - let controller = MastodonController.getForAccount(account) session.mastodonController = controller - controller.getOwnAccount() controller.getOwnInstance() diff --git a/Tusker/Screens/Main/MainSplitViewController.swift b/Tusker/Screens/Main/MainSplitViewController.swift index 57598583..c0261bd6 100644 --- a/Tusker/Screens/Main/MainSplitViewController.swift +++ b/Tusker/Screens/Main/MainSplitViewController.swift @@ -353,10 +353,17 @@ fileprivate extension MainSidebarViewController.Item { extension MainSplitViewController: TuskerRootViewController { @objc func presentCompose() { - let vc = ComposeHostingController(mastodonController: mastodonController) - let nav = EnhancedNavigationViewController(rootViewController: vc) - nav.presentationController?.delegate = vc - present(nav, animated: true) + if #available(iOS 15.0, *), UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { + let compose = UserActivityManager.newPostActivity(mentioning: nil, accountID: mastodonController.accountInfo!.id) + let options = UIWindowScene.ActivationRequestOptions() + options.preferredPresentationStyle = .prominent + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: compose, options: options, errorHandler: nil) + } else { + let vc = ComposeHostingController(mastodonController: mastodonController) + let nav = EnhancedNavigationViewController(rootViewController: vc) + nav.presentationController?.delegate = vc + present(nav, animated: true) + } } func select(tab: MainTabBarViewController.Tab) { diff --git a/Tusker/Screens/Main/MainTabBarViewController.swift b/Tusker/Screens/Main/MainTabBarViewController.swift index b0e276b0..0e95755e 100644 --- a/Tusker/Screens/Main/MainTabBarViewController.swift +++ b/Tusker/Screens/Main/MainTabBarViewController.swift @@ -212,10 +212,17 @@ extension MainTabBarViewController: FastAccountSwitcherViewControllerDelegate { extension MainTabBarViewController: TuskerRootViewController { @objc func presentCompose() { - let vc = ComposeHostingController(mastodonController: mastodonController) - let nav = EnhancedNavigationViewController(rootViewController: vc) - nav.presentationController?.delegate = vc - present(nav, animated: true) + if #available(iOS 15.0, *), UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { + let compose = UserActivityManager.newPostActivity(mentioning: nil, accountID: mastodonController.accountInfo!.id) + let options = UIWindowScene.ActivationRequestOptions() + options.preferredPresentationStyle = .prominent + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: compose, options: options, errorHandler: nil) + } else { + let vc = ComposeHostingController(mastodonController: mastodonController) + let nav = EnhancedNavigationViewController(rootViewController: vc) + nav.presentationController?.delegate = vc + present(nav, animated: true) + } } func select(tab: Tab) { diff --git a/Tusker/Shortcuts/UserActivityManager.swift b/Tusker/Shortcuts/UserActivityManager.swift index 6db9c8e9..cf90c3a0 100644 --- a/Tusker/Shortcuts/UserActivityManager.swift +++ b/Tusker/Shortcuts/UserActivityManager.swift @@ -70,6 +70,7 @@ class UserActivityManager { // TODO: check not currently showing compose screen let mentioning = activity.userInfo?["mentioning"] as? String let draft = mastodonController.createDraft(mentioningAcct: mentioning) + // todo: this shouldn't use self.mastodonController, it should get the right one based on the userInfo accountID let composeVC = ComposeHostingController(draft: draft, mastodonController: mastodonController) present(UINavigationController(rootViewController: composeVC)) } diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 7f2a1b8a..a65ffcb4 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -89,15 +89,22 @@ extension TuskerNavigationDelegate { } func compose(editing draft: Draft) { - let compose = ComposeHostingController(draft: draft, mastodonController: apiController) - - let vc = UINavigationController(rootViewController: compose) - vc.presentationController?.delegate = compose - present(vc, animated: true) + if #available(iOS 15.0, *), UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { + let compose = UserActivityManager.editDraftActivity(id: draft.id, accountID: apiController.accountInfo!.id) + let options = UIWindowScene.ActivationRequestOptions() + options.preferredPresentationStyle = .prominent + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: compose, options: options, errorHandler: nil) + } else { + let compose = ComposeHostingController(draft: draft, mastodonController: apiController) + let nav = UINavigationController(rootViewController: compose) + nav.presentationController?.delegate = compose + present(nav, animated: true) + } } func compose(inReplyToID: String? = nil, mentioningAcct: String? = nil) { let draft = apiController.createDraft(inReplyToID: inReplyToID, mentioningAcct: mentioningAcct) + DraftsManager.shared.add(draft) compose(editing: draft) }