jsdw_ios/QuickLocation/Section/Launch/LaunchViewController.swift

308 lines
10 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 guideImgList: [String] = [
"guide_1",
"guide_2",
"guide_3",
"guide_4",
"guide_5"
]
private let showVersion = 1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = .white
setupLayout()
experienceBtn.rx.tap.subscribe(onNext: { _ in
Defaults[\.isShowGuide] = false
AppDelegate.shared.showMainViewController()
}).disposed(by: disposeBag)
getUserConfig()
}
private func navigateAfterDelay() {
Observable.just(())
.delay(.milliseconds(2500), scheduler: MainScheduler.instance)
.subscribe(onNext: { [weak self] in
guard let systemConfig = AppContextManager.shared.systemConfig else { return }
if systemConfig.guideDisplay, Defaults[\.isShowGuide] {
self?.collectionView.isHidden = false
self?.pageControl.isHidden = false
self?.experienceBtn.isHidden = false
} else {
AppDelegate.shared.showMainViewController()
}
})
.disposed(by: disposeBag)
}
///
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 = model.config?.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 }
#if DEBUG || AdHoc
this.showServerSwitchAlert()
#else
this.getUserConfig()
#endif
}
}).disposed(by: disposeBag)
}
private func showServerSwitchAlert() {
let current = URLManager.shared.apiServerURL
let alert = UIAlertController(
title: "切换接口",
message: "当前:\(current)",
preferredStyle: .alert
)
alert.addTextField { field in
field.placeholder = "自定义接口地址"
field.text = current
field.keyboardType = .URL
field.autocorrectionType = .no
field.autocapitalizationType = .none
}
for preset in URLManager.presetServers {
alert.addAction(UIAlertAction(title: preset.title, style: .default) { [weak self] _ in
self?.applyServer(url: preset.url, env: preset.env)
})
}
alert.addAction(UIAlertAction(title: "确定", style: .default) { [weak self] _ in
let typed = alert.textFields?.first?.text ?? ""
self?.applyServer(url: typed, env: nil)
})
alert.addAction(UIAlertAction(title: "取消", style: .cancel))
present(alert, animated: true)
}
private func applyServer(url: String, env: Int?) {
let normalized = URLManager.normalizedServerURL(url)
guard !normalized.isEmpty else {
DLToast.show(text: "请输入接口地址")
return
}
URLManager.shared.switchServer(url: normalized, env: env)
AppDelegate.shared.showMainViewController()
}
private func continueAfterConfig(model: UserConfigModel) {
self.navigateAfterDelay()
}
private func showVersionUpgrade(_ upgrade: VersionUpgradeModel, model: UserConfigModel) {
self.showConfirmPop(
showCloseBtn: !upgrade.force,
title: upgrade.title + upgrade.version,
message: upgrade.description,
confirmText: "立即更新",
confirmBlock: {
if let url = URL(string: upgrade.url) {
UIApplication.shared.open(url)
}
if !upgrade.force {
self.continueAfterConfig(model: model)
}
},
cancelText: upgrade.force ? nil : "稍后再说",
cancelBlock: upgrade.force ? nil : { [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
}()
///
lazy var collectionView: UICollectionView = {
let viewFlowLayout = UICollectionViewFlowLayout()
viewFlowLayout.scrollDirection = .horizontal
viewFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
viewFlowLayout.minimumLineSpacing = 0
viewFlowLayout.minimumInteritemSpacing = 0
viewFlowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight)
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: viewFlowLayout)
collectionView.backgroundColor = .white
collectionView.dataSource = self
collectionView.delegate = self
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.isPagingEnabled = true
collectionView.bounces = false
collectionView.register(GuideCell.self)
collectionView.isHidden = true
return collectionView
}()
lazy var experienceBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setTitle("立即体验", for: .normal)
btn.setTitleColor(UIColor(hexStr: "#0F2846"), for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
btn.setBackgroundImage(UIImage(named: "Guide/experience"), for: .normal)
btn.isHidden = true
return btn
}()
lazy var pageControl: JJPageControl = {
let pageControl = JJPageControl(frame: CGRectMake(0, 0, kScreenWidth, 10))
pageControl.currentColor = ThemeManager.shared.color.mainColor
pageControl.currentPointSize = CGSize(width: 20, height: 6)
pageControl.otherColor = ThemeManager.shared.color.mainColor.withAlphaComponent(0.5)
pageControl.otherPointSize = CGSize(width: 6, height: 6)
pageControl.pointCornerRadius = 3
pageControl.controlSpacing = 18
pageControl.pageAliment = .Center
pageControl.backgroundColor = .clear
pageControl.numberOfPages = guideImgList.count
pageControl.isHidden = true
return pageControl
}()
}
extension LaunchViewController {
private func setupLayout() {
view.addSubview(launchLogoView)
view.addSubview(tipsImgView)
view.addSubview(collectionView)
view.addSubview(experienceBtn)
view.addSubview(pageControl)
launchLogoView.layoutChain
.centerX()
.centerY()
tipsImgView.layoutChain
.centerX()
.bottom(kSafeBottomMargin + 50)
collectionView.layoutChain.edges()
experienceBtn.layoutChain
.bottom(kSafeBottomMargin + 23)
.width(245)
.height(50)
.centerX()
pageControl.layoutChain
.bottomToTopOfView(experienceBtn, offset: -25)
.edgesHorzontal(10)
.centerX()
.height(10)
}
}
// MARK: - UICollectionViewDataSource
extension LaunchViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guideImgList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: GuideCell = collectionView.dequeueReusableCell(for: indexPath)
cell.configure(imageName: guideImgList[indexPath.row])
return cell
}
}
// MARK: - UICollectionViewDelegate
extension LaunchViewController: UICollectionViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == collectionView {
let page = Int(scrollView.contentOffset.x / scrollView.dl.width)
pageControl.currentPage = page
}
}
}
class GuideCell: UICollectionViewCell {
func configure(imageName: String) {
imgView.image = UIImage(named: "Guide/" + imageName)
}
private func setupSubViews() {
addSubview(imgView)
setupLayout()
}
private func setupLayout() {
imgView.layoutChain.edges()
}
lazy var imgView: UIImageView = {
let icon = UIImageView()
icon.contentMode = .scaleAspectFill
return icon
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.backgroundColor = .clear
setupSubViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}