72 lines
2.0 KiB
Swift
72 lines
2.0 KiB
Swift
//
|
|
// SceneDelegate.swift
|
|
// suixinkan
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|
|
|
var window: UIWindow?
|
|
|
|
func scene(
|
|
_ scene: UIScene,
|
|
willConnectTo session: UISceneSession,
|
|
options connectionOptions: UIScene.ConnectionOptions
|
|
) {
|
|
guard let windowScene = scene as? UIWindowScene else { return }
|
|
|
|
AppNavigationBarAppearance.applyGlobalAppearance()
|
|
|
|
let window = UIWindow(windowScene: windowScene)
|
|
window.rootViewController = AppRouter.makeRootViewController()
|
|
window.makeKeyAndVisible()
|
|
self.window = window
|
|
(UIApplication.shared.delegate as? AppDelegate)?.window = window
|
|
|
|
registerNotifications()
|
|
}
|
|
|
|
func sceneDidDisconnect(_ scene: UIScene) {
|
|
NotificationCenter.default.removeObserver(self)
|
|
if (UIApplication.shared.delegate as? AppDelegate)?.window === window {
|
|
(UIApplication.shared.delegate as? AppDelegate)?.window = nil
|
|
}
|
|
}
|
|
|
|
private func registerNotifications() {
|
|
NotificationCenter.default.addObserver(
|
|
self,
|
|
selector: #selector(handleSessionDidExpire),
|
|
name: NotificationName.sessionDidExpire,
|
|
object: nil
|
|
)
|
|
NotificationCenter.default.addObserver(
|
|
self,
|
|
selector: #selector(handleUserDidLogout),
|
|
name: NotificationName.userDidLogout,
|
|
object: nil
|
|
)
|
|
NotificationCenter.default.addObserver(
|
|
self,
|
|
selector: #selector(handleUserDidLogin),
|
|
name: NotificationName.userDidLogin,
|
|
object: nil
|
|
)
|
|
}
|
|
|
|
@objc private func handleSessionDidExpire() {
|
|
AppStore.shared.logout()
|
|
AppRouter.setRoot(.login, on: window)
|
|
}
|
|
|
|
@objc private func handleUserDidLogout() {
|
|
AppStore.shared.logout()
|
|
AppRouter.setRoot(.login, on: window)
|
|
}
|
|
|
|
@objc private func handleUserDidLogin() {
|
|
AppRouter.setRoot(.mainTab, on: window)
|
|
}
|
|
}
|