BeeReel/BeeReel/Class/Delegate/SceneDelegate.swift
2025-08-07 18:28:21 +08:00

105 lines
3.3 KiB
Swift

import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var onLineTimer: Timer?
private(set) var isOpenApp = false
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
BRAppTool.sceneDelegate = self
BRAppTool.windowScene = windowScene
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusDidChangeNotification), name: BRNetworkStatusManager.networkStatusDidChangeNotification, object: nil)
onLineTimer = Timer.scheduledTimer(timeInterval: 60 * 10, target: YYTextWeakProxy(target: self), selector: #selector(handleOnLine), userInfo: nil, repeats: true)
if let webpageURL = connectionOptions.userActivities.first?.webpageURL {
self.br_handleOpenAppMessage(webpageURL: webpageURL)
} else if let url = connectionOptions.urlContexts.first?.url {
self.br_handleOpenAppMessage(webpageURL: url)
}
window = UIWindow(windowScene: windowScene)
startApp()
}
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) {
self.handleOnLine()
BRStatAPI.requestEnterApp()
}
func sceneWillResignActive(_ scene: UIScene) {
BRStatAPI.requestLeaveApp()
}
func sceneWillEnterForeground(_ scene: UIScene) {
BRStatAPI.uploadNoticeStatus()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.br_handleOpenAppMessage(webpageURL: SceneDelegate.webpageURL)
}
}
func sceneDidEnterBackground(_ scene: UIScene) {
SceneDelegate.allowOpenMessage = true
}
}
extension SceneDelegate {
private func startApp() {
let hasOpenApp = UserDefaults.standard.object(forKey: kBRHasBeenOpenedAPPDefaultsKey) as? Bool
if hasOpenApp != true {
let guideVc = BRGuideViewController()
guideVc.clickStartButton = { [weak self] in
guard let self = self else { return }
self.openApp()
self.br_retryHandleOpenAppMessage()
}
window?.rootViewController = guideVc
window?.makeKeyAndVisible()
} else {
openApp()
}
}
private func openApp() {
self.isOpenApp = true
BRAppTool.tabBarController = BRTabBarController()
window?.rootViewController = BRAppTool.tabBarController
window?.makeKeyAndVisible()
}
}
extension SceneDelegate {
@objc private func handleOnLine() {
BRStatAPI.requestStatOnLine()
}
@objc private func networkStatusDidChangeNotification() {
br_retryHandleOpenAppMessage()
}
}