150 lines
4.5 KiB
Swift
150 lines
4.5 KiB
Swift
//
|
|
// LaunchViewController.swift
|
|
// SHECommunity
|
|
//
|
|
// Created by Lin on 2025/1/3.
|
|
//
|
|
|
|
import UIKit
|
|
import RxSwift
|
|
import SwiftyUserDefaults
|
|
import RxCocoa
|
|
|
|
class LaunchViewController: BaseViewController {
|
|
|
|
private let showVersion = 1
|
|
private var privacyConsentView: LaunchPrivacyConsentView?
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
view.backgroundColor = .white
|
|
setupLayout()
|
|
if Defaults[\.didAgreeLaunchPrivacy] {
|
|
proceedAfterPrivacyAgreed()
|
|
} else {
|
|
showPrivacyConsentIfNeeded()
|
|
}
|
|
}
|
|
|
|
private func proceedAfterPrivacyAgreed() {
|
|
AppDelegate.shared.setupPrivacyGrantedSDKs()
|
|
getUserConfig()
|
|
}
|
|
|
|
private func showPrivacyConsentIfNeeded() {
|
|
let consent = LaunchPrivacyConsentView()
|
|
privacyConsentView = consent
|
|
consent.onAgree = { [weak self] in
|
|
Defaults[\.didAgreeLaunchPrivacy] = true
|
|
self?.privacyConsentView?.removeFromSuperview()
|
|
self?.privacyConsentView = nil
|
|
self?.proceedAfterPrivacyAgreed()
|
|
}
|
|
consent.onDisagree = { [weak self] in
|
|
self?.handlePrivacyDisagree()
|
|
}
|
|
consent.onOpenURL = { [weak self] url in
|
|
self?.openPrivacyWeb(url)
|
|
}
|
|
consent.show(in: view)
|
|
}
|
|
|
|
private func handlePrivacyDisagree() {
|
|
let alert = UIAlertController(
|
|
title: "需同意协议后才能使用",
|
|
message: "若不同意服务协议及隐私政策,将无法进入极速定位。",
|
|
preferredStyle: .alert
|
|
)
|
|
alert.addAction(UIAlertAction(title: "退出应用", style: .destructive) { _ in
|
|
exit(0)
|
|
})
|
|
alert.addAction(UIAlertAction(title: "返回", style: .cancel))
|
|
present(alert, animated: true)
|
|
}
|
|
|
|
private func openPrivacyWeb(_ url: String) {
|
|
let vc = WebViewController(url: url, isShare: false, fullscreen: false, style: "default")
|
|
let nav = UINavigationController(rootViewController: vc)
|
|
nav.modalPresentationStyle = .fullScreen
|
|
present(nav, animated: true)
|
|
}
|
|
|
|
/// 获取用户配置
|
|
func getUserConfig() {
|
|
SystemService.userConfig().subscribe(onNext: { response in
|
|
guard let model = response.model else { return }
|
|
MQTTService.shared.connect()
|
|
Defaults[\.loginToken] = model.token
|
|
RelationStore.shared.preload()
|
|
if #available(iOS 16.0, *) {
|
|
AppRestrictManager.shared.checkScreenTimePermissionAfterLogin()
|
|
}
|
|
|
|
AppContextManager.shared.systemConfig = model.config
|
|
|
|
if let upgrade = AppContextManager.shared.systemConfig?.versionUpgradeModel {
|
|
self.showVersionUpgrade(upgrade, model: model)
|
|
return
|
|
}
|
|
self.continueAfterConfig(model: model)
|
|
}, onError: { [weak self] (error) in
|
|
DLAlert.show(title: error.localizedDescription,
|
|
defaultTitle: "重试") { [weak self] in
|
|
guard let this = self else { return }
|
|
this.getUserConfig()
|
|
}
|
|
}).disposed(by: disposeBag)
|
|
}
|
|
|
|
private func continueAfterConfig(model: UserConfigModel) {
|
|
AppDelegate.shared.showMainViewController()
|
|
}
|
|
|
|
private func showVersionUpgrade(_ upgrade: VersionUpgradeModel, model: UserConfigModel) {
|
|
showVersionUpgradePop(upgrade: upgrade) { [weak self] in
|
|
self?.continueAfterConfig(model: model)
|
|
}
|
|
}
|
|
|
|
// MARK: - Init
|
|
init() {
|
|
super.init(nibName: nil, bundle: nil)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
lazy var launchLogoView: UIImageView = {
|
|
let view = UIImageView()
|
|
view.image = UIImage(named: "Launch/logo")
|
|
view.backgroundColor = .clear
|
|
view.contentMode = .scaleAspectFill
|
|
return view
|
|
}()
|
|
|
|
lazy var tipsImgView: UIImageView = {
|
|
let view = UIImageView()
|
|
view.image = UIImage(named: "Launch/slogan")
|
|
view.backgroundColor = .clear
|
|
view.contentMode = .scaleAspectFill
|
|
return view
|
|
}()
|
|
}
|
|
|
|
extension LaunchViewController {
|
|
private func setupLayout() {
|
|
view.addSubview(launchLogoView)
|
|
view.addSubview(tipsImgView)
|
|
|
|
launchLogoView.layoutChain
|
|
.centerX()
|
|
.centerY()
|
|
|
|
tipsImgView.layoutChain
|
|
.centerX()
|
|
.bottom(kSafeBottomMargin + 50)
|
|
}
|
|
}
|