251 lines
8.0 KiB
Swift
251 lines
8.0 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 = UIColor(hexStr: "#E0F2FF")
|
||
setupLayout()
|
||
|
||
experienceBtn.rx.tap.subscribe(onNext: { _ in
|
||
// Defaults[\.guideShowVersion] = self.showVersion
|
||
AppDelegate.shared.showMainViewController()
|
||
}).disposed(by: disposeBag)
|
||
|
||
getUserConfig()
|
||
|
||
navigateAfterDelay()
|
||
}
|
||
|
||
private func navigateAfterDelay() {
|
||
Observable.just(())
|
||
.delay(.milliseconds(2500), scheduler: MainScheduler.instance)
|
||
.subscribe(onNext: { [weak self] in
|
||
self?.navigateToNext()
|
||
})
|
||
.disposed(by: disposeBag)
|
||
}
|
||
|
||
private func navigateToNext() {
|
||
let hasShownGuide = UserDefaults.standard.bool(forKey: "hasShownGuide")
|
||
if hasShownGuide {
|
||
let loginVC = LoginViewController()
|
||
UIView.transition(with: view.window!, duration: 0.4, options: .transitionCrossDissolve) {
|
||
self.view.window?.rootViewController = UINavigationController(rootViewController: loginVC)
|
||
}
|
||
} else {
|
||
collectionView.isHidden = false
|
||
pageControl.isHidden = false
|
||
experienceBtn.isHidden = false
|
||
}
|
||
}
|
||
|
||
/// 获取用户配置
|
||
func getUserConfig() {
|
||
SystemService.userConfig().subscribe(onNext: { response in
|
||
self.getUserIMToken()
|
||
guard let model = response.model else { return }
|
||
AppContextManager.shared.systemConfig = model.config
|
||
// 保存用户数据
|
||
AppContextManager.shared.saveAccount(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)
|
||
}
|
||
|
||
/// 获取用户IM Token
|
||
func getUserIMToken() {
|
||
UserService.imToken().subscribe(onNext: { response in
|
||
guard let data = response.data, let token = data["token"] as? String else { return }
|
||
AppContextManager.shared.imToken = token
|
||
// 提前初始化 SDK,减少圈子页等待时间
|
||
GroupIMService.shared.initSDK()
|
||
}).disposed(by: disposeBag)
|
||
}
|
||
|
||
// 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")
|
||
}
|
||
}
|