ReaderHive/ReaderHive/Delegate/SceneDelegate.swift
澜声世纪 e9e271e91a 1
2026-02-06 14:38:37 +08:00

143 lines
4.6 KiB
Swift

//
// SceneDelegate.swift
// ReaderHive
//
// Created by on 2025/11/21.
//
import UIKit
import YYText
import MJRefresh
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
private var onLineTimer: Timer?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusDidChangeNotification), name: NRNetworkReachableManager.networkStatusDidChangeNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(localizedDidChangeNotification), name: NRLocalizedManager.localizedDidChangeNotification, object: nil)
NRTool.sceneDelegate = self
NRTool.windowScene = windowScene
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
self.startApp()
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.handleOpenAppMessage(webpageURL: webpageURL)
} else if let url = connectionOptions.urlContexts.first?.url {
self.handleOpenAppMessage(webpageURL: url)
}
}
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()
NRStatAPI.nr_requestEnterApp()
NRTool.setBadgeCount(0)
}
func sceneWillResignActive(_ scene: UIScene) {
NRStatAPI.nr_requestLeaveApp()
}
func sceneWillEnterForeground(_ scene: UIScene) {
NRStatAPI.nr_uploadApnsAuthorizationStatus()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.handleOpenAppMessage(webpageURL: nil)
}
}
func sceneDidEnterBackground(_ scene: UIScene) {
NROpenAppManager.manager.allowOpenMessage = true
}
}
extension SceneDelegate {
private func startApp() {
let localizedManager = NRLocalizedManager.shared
if localizedManager.localizedDataLocalizedKey != localizedManager.currentLocalizedKey {
let vc = NRTool.lanuchViewController
window?.rootViewController = vc
Task {
let _ = await localizedManager.updateLocalizedData()
self.setRootVC()
}
} else {
Task {
let _ = await localizedManager.updateLocalizedData()
}
setRootVC()
}
}
private func setRootVC() {
///
let hasOpenApp = UserDefaults.standard.object(forKey: kNRHasFirstOpenedAPPDefaultsKey) as? Bool
if hasOpenApp != true {
let vc = NRAppStartViewController()
vc.openAppHandle = { [weak self] in
guard let self = self else { return }
UserDefaults.standard.set(true, forKey: kNRHasFirstOpenedAPPDefaultsKey)
self.setTabBarController()
}
window?.rootViewController = vc
} else {
self.setTabBarController()
}
}
private func setTabBarController() {
window?.rootViewController = NRTabBarController()
NROpenAppManager.manager.openAppFinish()
}
}
extension SceneDelegate {
@objc private func handleOnLine() {
NRStatAPI.nr_requestStatOnLine()
}
@objc private func networkStatusDidChangeNotification() {
guard NRNetworkReachableManager.manager.isReachable == true else { return }
self.handleOnLine()
self.retryHandleOpenAppMessage()
if NRLocalizedManager.shared.localizedData == nil, !NROpenAppManager.manager.isOpenApp {
self.startApp()
}
}
@objc private func localizedDidChangeNotification() {
MJRefreshConfig.default.languageCode = NRLocalizedManager.shared.mjLocalizedKey
self.setTabBarController()
}
}