Files
suixinkan_ios_uikit/suixinkan_ios/App/ViewControllers/RootViewController.swift
汉秋 a1c031c9b7 Integrate CYLTabBar center scan button and unify UIKit navigation.
Add CYLTabBarController with a global scan entry, promote MainTabBarController to the window root after login, replace RouterPath-based navigation with AppNavigator, and fix Profile diffable cell reconfiguration crashes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 16:36:18 +08:00

181 lines
6.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// RootViewController.swift
// suixinkan
//
import SnapKit
import UIKit
@MainActor
/// window `MainTabBarController`
final class RootViewController: UIViewController {
private let services: AppServices
private var currentAuthChild: UIViewController?
///
init(services: AppServices = .shared) {
self.services = services
super.init(nibName: nil, bundle: nil)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
///
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
services.appSession.onChange = { [weak self] in
self?.handleSessionPhaseChange()
}
applyRoot(for: services.appSession.phase)
bootstrapSession()
services.configurePushNotifications()
}
///
private func bootstrapSession() {
Task {
await services.globalLoading.withLoading {
await services.sessionBootstrapper.restore(
appSession: services.appSession,
accountContext: services.accountContext,
permissionContext: services.permissionContext,
profileAPI: services.profileAPI,
accountContextAPI: services.accountContextAPI,
toastCenter: services.toastCenter
)
}
if services.appSession.isLoggedIn, !AppUITestLaunchState.isRunningUITests {
PushNotificationManager.shared.requestAuthorizationAndRegister()
}
}
}
/// window
private func handleSessionPhaseChange() {
applyRoot(for: services.appSession.phase)
switch services.appSession.phase {
case .loggedIn:
if !AppUITestLaunchState.isRunningUITests {
PushNotificationManager.shared.requestAuthorizationAndRegister()
}
Task { await reloadScenicSpotContextIfNeeded() }
case .loggedOut:
//
services.accountContext.reset()
services.permissionContext.reset()
services.scenicSpotContext.reset()
services.appNavigator.resetAllStacks()
services.appNavigator.detach()
services.toastCenter.dismiss()
services.scenicQueueRuntime.stop()
case .restoring:
break
}
}
/// ID
private func reloadScenicSpotContextIfNeeded() async {
guard services.appSession.isLoggedIn else {
services.scenicSpotContext.reset()
return
}
await services.scenicSpotContext.reload(
scenicId: services.accountContext.currentScenic?.id,
api: services.accountContextAPI
)
}
/// window
private func applyRoot(for phase: AuthPhase) {
switch phase {
case .loggedIn:
showMainTabBarAsWindowRoot()
case .loggedOut:
showAuthRoot(with: LoginViewController(services: services))
case .restoring:
showAuthRoot(with: restoringViewController())
}
}
/// `MainTabBarController` window CYLTabBarController
private func showMainTabBarAsWindowRoot() {
removeCurrentAuthChild()
guard let window = activeWindow else { return }
let mainTabBarController = MainTabBarController(services: services)
window.rootViewController = mainTabBarController
attachGlobalOverlays(to: window)
}
/// /
private func showAuthRoot(with child: UIViewController) {
guard let window = activeWindow else {
mountAuthChild(child)
return
}
if window.rootViewController !== self {
window.rootViewController = self
}
mountAuthChild(child)
attachGlobalOverlays(to: window)
}
///
private func mountAuthChild(_ child: UIViewController) {
guard currentAuthChild !== child else { return }
currentAuthChild?.willMove(toParent: nil)
currentAuthChild?.view.removeFromSuperview()
currentAuthChild?.removeFromParent()
addChild(child)
view.addSubview(child.view)
child.view.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
child.didMove(toParent: self)
currentAuthChild = child
}
///
private func removeCurrentAuthChild() {
currentAuthChild?.willMove(toParent: nil)
currentAuthChild?.view.removeFromSuperview()
currentAuthChild?.removeFromParent()
currentAuthChild = nil
}
///
private func restoringViewController() -> UIViewController {
let controller = UIViewController()
controller.view.backgroundColor = .systemBackground
return controller
}
/// Toast Loading window
private func attachGlobalOverlays(to window: UIWindow) {
services.toastCenter.attachToWindow(window)
services.globalLoading.attachToWindow(window)
}
/// key window
private var activeWindow: UIWindow? {
view.window ?? UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap(\.windows)
.first(where: \.isKeyWindow)
}
}