ThimraTV/MoviaBox/AppDelegate/SceneDelegate.swift
2025-06-18 19:26:21 +08:00

172 lines
6.0 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.

//
// SceneDelegate.swift
// MoviaBox
//
// Created by on 2025/4/8.
//
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var timer: Timer?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
SPAPPTool.sceneDelegate = self
SPAPPTool.windowScene = windowScene
///
NotificationCenter.default.addObserver(self, selector: #selector(localizedDidChange), name: SPLocalizedManager.localizedDidChange, object: nil)
///
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityDidChangeNotification), name: SPNetworkReachabilityManager.reachabilityDidChangeNotification, object: nil)
///
// NotificationCenter.default.addObserver(self, selector: #selector(loginStateDidChangeNotification), name: SPLoginManager.loginStateDidChangeNotification, object: nil)
if let webpageURL = connectionOptions.userActivities.first?.webpageURL {
self.handleOpenAppMessage(webpageURL: webpageURL)
} else if let url = connectionOptions.urlContexts.first?.url {
self.handleOpenAppMessage(webpageURL: url)
}
window = UIWindow(windowScene: windowScene)
startApp()
//线
timer = Timer.scheduledTimer(timeInterval: 60 * 10, target: self, selector: #selector(handleOnLine), userInfo: nil, repeats: true)
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
SPStatAPI.requestEnterApp()
handleOnLine()
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
SPStatAPI.requestLeaveApp()
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.handleOpenAppMessage(webpageURL: nil)
}
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}
extension SceneDelegate {
private func startApp() {
let localizedManager = SPLocalizedManager.shared
///
if localizedManager.localizedDataLocalizedKey != localizedManager.currentLocalizedKey, let lanuchVC = SPAPPTool.getLanuchViewController() {
window?.rootViewController = lanuchVC
window?.makeKeyAndVisible()
SPLocalizedManager.shared.updateLocalizedData { [weak self] finish in
guard let self = self else { return }
self.setRootVC()
}
} else {
SPLocalizedManager.shared.updateLocalizedData(completer: nil)
setRootVC()
}
}
///线
@objc private func handleOnLine() {
SPStatAPI.requestStatOnLine()
}
private func setRootVC() {
let hasOpenApp = UserDefaults.standard.object(forKey: kSPHasBeenOpenedAPPDefaultsKey) as? Bool
///
let guideVc = SPGuideViewController()
if hasOpenApp != true && guideVc.lanuchVC != nil {
SPAPPTool.isAppOpen = false
guideVc.openAppBlock = {
self.handleOpenApp()
}
window?.rootViewController = guideVc
window?.makeKeyAndVisible()
} else {
SPAPPTool.isAppOpen = true
setTabBarController()
}
}
private func setTabBarController() {
let tabBarController = SPTabBarController()
SPAPPTool.mainTabBarController = tabBarController
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
}
///app
@objc private func handleOpenApp() {
setTabBarController()
retryHandleOpenAppMessage()
}
}
extension SceneDelegate {
///
@objc private func localizedDidChange() {
MJRefreshConfig.default.languageCode = SPLocalizedManager.shared.mjLocalizedKey
setTabBarController()
}
///
@objc private func reachabilityDidChangeNotification() {
retryHandleOpenAppMessage()
let localizedData = SPLocalizedManager.shared.localizedData ?? [:]
if SPNetworkReachabilityManager.manager.isReachable == true {
handleOnLine()
///
if localizedData.isEmpty {
self.startApp()
}
}
}
}