forked from shadowfacts/Tusker
73 lines
2.4 KiB
Swift
73 lines
2.4 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
|
||
|
}
|
||
|
|
||
|
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
|
||
|
|
||
|
let composeVC = ComposeHostingController(draft: draft, mastodonController: controller)
|
||
|
composeVC.delegate = self
|
||
|
let nav = EnhancedNavigationViewController(rootViewController: composeVC)
|
||
|
|
||
|
window = UIWindow(windowScene: windowScene)
|
||
|
window!.rootViewController = nav
|
||
|
window!.makeKeyAndVisible()
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|