2020-12-14 03:37:37 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
let account: LocalData.UserAccountInfo
|
|
|
|
let draft: Draft?
|
|
|
|
|
|
|
|
if let activity = connectionOptions.userActivities.first ?? session.stateRestorationActivity,
|
|
|
|
let activityAccount = UserActivityManager.getAccount(from: activity) {
|
|
|
|
account = activityAccount
|
|
|
|
draft = UserActivityManager.getDraft(from: activity)
|
|
|
|
} else {
|
|
|
|
account = LocalData.shared.getMostRecentAccount()!
|
|
|
|
draft = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let controller = MastodonController.getForAccount(account)
|
|
|
|
session.mastodonController = controller
|
|
|
|
|
2020-12-15 03:35:34 +00:00
|
|
|
controller.getOwnAccount()
|
|
|
|
controller.getOwnInstance()
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
let composeVC = ComposeHostingController(draft: draft, mastodonController: controller)
|
|
|
|
composeVC.delegate = self
|
|
|
|
let nav = EnhancedNavigationViewController(rootViewController: composeVC)
|
|
|
|
|
|
|
|
window = UIWindow(windowScene: windowScene)
|
|
|
|
window!.rootViewController = nav
|
|
|
|
window!.makeKeyAndVisible()
|
2021-01-07 00:20:14 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(themePrefChanged), name: .themePreferenceChanged, object: nil)
|
|
|
|
themePrefChanged()
|
2020-12-14 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-07 00:20:14 +00:00
|
|
|
@objc private func themePrefChanged() {
|
|
|
|
window?.overrideUserInterfaceStyle = Preferences.shared.theme
|
|
|
|
}
|
|
|
|
|
2020-12-14 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|