Tusker/Tusker/ComposeSceneDelegate.swift

101 lines
3.6 KiB
Swift

//
// ComposeSceneDelegate.swift
// Tusker
//
// Created by Shadowfacts on 12/12/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
class ComposeSceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else {
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 {
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
}
session.mastodonController = controller
controller.getOwnAccount()
controller.getOwnInstance()
let composeVC = ComposeHostingController(draft: draft, mastodonController: controller)
composeVC.delegate = self
let nav = EnhancedNavigationViewController(rootViewController: composeVC)
window = UIWindow(windowScene: windowScene)
window!.rootViewController = nav
window!.makeKeyAndVisible()
NotificationCenter.default.addObserver(self, selector: #selector(themePrefChanged), name: .themePreferenceChanged, object: nil)
themePrefChanged()
}
func sceneWillResignActive(_ scene: UIScene) {
DraftsManager.save()
if let window = window,
let nav = window.rootViewController as? UINavigationController,
let compose = nav.topViewController as? ComposeHostingController {
scene.userActivity = UserActivityManager.editDraftActivity(id: compose.draft.id, accountID: scene.session.mastodonController!.accountInfo!.id)
}
}
func stateRestorationActivity(for scene: UIScene) -> NSUserActivity? {
return scene.userActivity
}
@objc private func themePrefChanged() {
window?.overrideUserInterfaceStyle = Preferences.shared.theme
}
}
extension ComposeSceneDelegate: ComposeHostingControllerDelegate {
func dismissCompose(mode: ComposeUIState.DismissMode) -> Bool {
let animation: UIWindowScene.DismissalAnimation
switch mode {
case .cancel:
animation = .decline
case .post:
animation = .commit
}
closeWindow(animation: animation)
return true
}
}