// // CompositionRoot.swift // JSJP_Student_sw // // Created by ningye on 2023/5/22. // import UIKit import CGFloatLiteral import Kingfisher import RxGesture import RxOptional import RxViewController import SnapKit import SwiftyColor import SwiftyImage import SwiftyUserDefaults import Then import URLNavigator import NSObject_Rx import ManualLayout import GDPerformanceView_Swift import Toaster import Dollar import WebKit import SwiftDate struct NYAppDependency { typealias OpenURLHandler = (_ url: URL, _ options: [UIApplication.OpenURLOptionsKey: Any]) -> Bool let window: UIWindow let configureSDKs: () -> Void let configureAppearance: () -> Void let configureUserAgent: () -> Void let congigurePerformance: () -> Void let openURL: OpenURLHandler } let navigator = Navigator() final class CompositionRoot { /// Builds a dependency graph and returns an entry view controller. static func resolve() -> NYAppDependency { let window = UIWindow(frame: UIScreen.main.bounds) window.backgroundColor = .white window.makeKeyAndVisible() URLNavigationMap.initialize(navigator: navigator) var presentMainScreen: (() -> Void)! presentMainScreen = { //首页 let homeViewController = HomeViewController(nibName: "HomeViewController") let navigationController = MainNavigationController(rootViewController: homeViewController) navigationController.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3) window.rootViewController = navigationController } let splashViewController = SplashViewController(presentMainScreen: presentMainScreen) window.rootViewController = splashViewController return NYAppDependency( window: window, configureSDKs: self.configureSDKs, configureAppearance: self.configureAppearance, configureUserAgent: self.configureUserAgent, congigurePerformance: self.congigurePerformance, openURL: self.openURLFactory(navigator: navigator) ) } static func configureSDKs() { //友盟 UM_APPKEY UMConfigure.setLogEnabled(true) UMConfigure.initWithAppkey(NYMacros.appMenKey, channel: "App Store") } static func configureAppearance() { //设置时区 let _ = Region(calendar: Calendars.gregorian, zone: Zones.asiaShanghai, locale: Locales.chinese) //打开app次数 LocalManager.userInfo.openTimes += 1 //设置环境 --默认是线上环境 Defaults.currentEnvironment = .res #if DEBUG || _DEBUG //设置环境 Defaults.currentEnvironment = .dev #endif //ToastView ToastView.appearance().font = NYFont.SysFont.sys_15 //配置全局GJRatioAutoLayout GJRatioAutoLayout.start(with: GJScreenSizeType.typeiPhone6) } static func configureUserAgent() { let webView = WKWebView(frame: .zero) webView.evaluateJavaScript("navigator.userAgent") { (oldAgent, error) in guard let oldAgent = oldAgent as? String else { return } let newAgent = "\(oldAgent) BiliApp/StudioApp/6560" let newAgentDic = ["UserAgent":newAgent] UserDefaults.standard.register(defaults: newAgentDic) if #available(iOS 9.0, *) { //局部更新,即可以在其他用到的webView页面重新修改userAgent webView.customUserAgent = newAgent } } webView.load(URLRequest(url: URL(string:"http//www.baidu.com")!)) } static func congigurePerformance() { //#if DEBUG // PerformanceMonitor.sharedInstance.startMonitoring() // PerformanceMonitor.sharedInstance.appVersionHidden = true // PerformanceMonitor.sharedInstance.deviceVersionHidden = true //#endif } static func openURLFactory(navigator: NavigatorType) -> NYAppDependency.OpenURLHandler { return { url, options -> Bool in if navigator.open(url) { return true } if navigator.present(url, wrap: MainNavigationController.self) != nil { return true } return false } } }