Tusker/Tusker/Screens/Main/AccountSwitchingContainerVi...

161 lines
5.9 KiB
Swift
Raw Normal View History

2020-11-11 20:26:25 +00:00
//
// AccountSwitchingContainerViewController.swift
// Tusker
//
// Created by Shadowfacts on 11/11/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
2022-08-02 01:29:24 +00:00
import ScreenCorners
2020-11-11 20:26:25 +00:00
class AccountSwitchingContainerViewController: UIViewController {
2023-02-25 22:59:35 +00:00
private var currentAccountID: String
2020-11-11 20:26:25 +00:00
private(set) var root: TuskerRootViewController
2023-02-25 22:59:35 +00:00
private var userActivities: [String: NSUserActivity] = [:]
init(root: TuskerRootViewController, for account: LocalData.UserAccountInfo) {
self.currentAccountID = account.id
2020-11-11 20:26:25 +00:00
self.root = root
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
embedChild(root)
}
2023-02-25 22:59:35 +00:00
func setRoot(_ newRoot: TuskerRootViewController, for account: LocalData.UserAccountInfo, animating direction: AnimationDirection) {
2020-11-11 20:26:25 +00:00
let oldRoot = self.root
if direction == .none {
oldRoot.removeViewAndController()
}
2023-02-25 22:59:35 +00:00
if let activity = oldRoot.stateRestorationActivity() {
stateRestorationLogger.debug("AccountSwitchingContainer: saving \(activity.activityType, privacy: .public) for \(self.currentAccountID, privacy: .public)")
userActivities[currentAccountID] = activity
}
self.currentAccountID = account.id
2020-11-11 20:26:25 +00:00
self.root = newRoot
embedChild(newRoot)
2023-02-25 22:59:35 +00:00
if let activity = userActivities.removeValue(forKey: account.id) {
stateRestorationLogger.debug("AccountSwitchingContainer: restoring \(activity.activityType, privacy: .public) for \(account.id, privacy: .public)")
let context = StateRestorationUserActivityHandlingContext(root: newRoot)
_ = activity.handleResume(manager: UserActivityManager(scene: view.window!.windowScene!, context: context))
context.finalize(activity: activity)
}
2020-11-11 20:26:25 +00:00
if direction != .none {
2021-02-06 18:47:45 +00:00
if UIAccessibility.prefersCrossFadeTransitions {
2020-11-11 20:26:25 +00:00
newRoot.view.alpha = 0
UIView.animate(withDuration: 0.4, delay: 0, options: .curveEaseInOut) {
newRoot.view.alpha = 1
oldRoot.view.alpha = 0
} completion: { (_) in
oldRoot.removeViewAndController()
}
} else {
let sign: CGFloat = direction == .downwards ? -1 : 1
let newInitialOffset = sign * view.bounds.height
2022-08-02 01:29:24 +00:00
let scale: CGFloat = 0.75
2020-11-11 20:26:25 +00:00
2022-08-02 01:29:24 +00:00
newRoot.view.transform = CGAffineTransform(translationX: 0, y: newInitialOffset).scaledBy(x: 0.9, y: 0.9)
newRoot.view.layer.masksToBounds = true
newRoot.view.layer.cornerCurve = .continuous
newRoot.view.layer.cornerRadius = view.window?.screen.displayCornerRadius ?? 0
2020-11-11 20:26:25 +00:00
2022-08-02 01:29:24 +00:00
oldRoot.view.layer.masksToBounds = true
oldRoot.view.layer.cornerCurve = .continuous
oldRoot.view.layer.cornerRadius = view.window?.screen.displayCornerRadius ?? 0
// only one edge is affected in each direction, i have no idea why
if direction == .upwards {
oldRoot.additionalSafeAreaInsets.bottom = view.safeAreaInsets.bottom
} else {
oldRoot.additionalSafeAreaInsets.top = view.safeAreaInsets.top
}
UIView.animate(withDuration: 0.4, delay: 0, options: .curveEaseInOut) {
oldRoot.view.transform = CGAffineTransform(translationX: 0, y: -newInitialOffset).scaledBy(x: scale, y: scale)
2020-11-11 20:26:25 +00:00
newRoot.view.transform = .identity
} completion: { (_) in
oldRoot.removeViewAndController()
2022-08-02 01:29:24 +00:00
newRoot.view.layer.masksToBounds = false
2020-11-11 20:26:25 +00:00
}
}
}
}
}
extension AccountSwitchingContainerViewController {
enum AnimationDirection {
case none, downwards, upwards
}
}
extension AccountSwitchingContainerViewController: TuskerRootViewController {
2022-11-23 16:35:25 +00:00
func stateRestorationActivity() -> NSUserActivity? {
loadViewIfNeeded()
return root.stateRestorationActivity()
}
func compose(editing draft: Draft?, animated: Bool, isDucked: Bool) {
2022-11-23 16:35:25 +00:00
loadViewIfNeeded()
root.compose(editing: draft, animated: animated, isDucked: isDucked)
2022-11-23 16:35:25 +00:00
}
func select(route: TuskerRoute, animated: Bool) {
2021-05-24 23:07:47 +00:00
loadViewIfNeeded()
root.select(route: route, animated: animated)
2020-11-11 20:26:25 +00:00
}
func getTabController(tab: MainTabBarViewController.Tab) -> UIViewController? {
2021-05-24 23:07:47 +00:00
loadViewIfNeeded()
return root.getTabController(tab: tab)
2020-11-11 20:26:25 +00:00
}
func getNavigationDelegate() -> TuskerNavigationDelegate? {
2021-05-24 23:07:47 +00:00
loadViewIfNeeded()
return root.getNavigationDelegate()
}
func getNavigationController() -> NavigationControllerProtocol {
loadViewIfNeeded()
return root.getNavigationController()
2021-05-24 23:07:47 +00:00
}
func performSearch(query: String) {
loadViewIfNeeded()
2021-05-24 23:07:47 +00:00
root.performSearch(query: query)
2020-11-11 20:26:25 +00:00
}
func presentPreferences(completion: (() -> Void)?) {
loadViewIfNeeded()
root.presentPreferences(completion: completion)
}
func handleStatusBarTapped(xPosition: CGFloat) -> StatusBarTapActionResult {
loadViewIfNeeded()
// TODO: check if fast account switcher is being presented?
return root.handleStatusBarTapped(xPosition: xPosition)
}
2020-11-11 20:26:25 +00:00
}
extension AccountSwitchingContainerViewController: BackgroundableViewController {
func sceneDidEnterBackground() {
if let backgroundable = root as? BackgroundableViewController {
backgroundable.sceneDidEnterBackground()
}
}
}