//
|
// HomeVC.swift
|
// WanPai
|
//
|
// Created by 杨锴 on 2023/6/8.
|
//
|
|
import UIKit
|
import JQTools
|
import RxSwift
|
import RxCocoa
|
import SDWebImage
|
|
//http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
|
//http://vjs.zencdn.net/v/oceans.mp4
|
//https://media.w3.org/2010/05/sintel/trailer.mp4
|
|
class HomeViewModel:RefreshModel<HomeStoreConfigModel>{
|
let storeId = BehaviorRelay<Int>(value: 0)
|
|
override func api() -> (Observable<BaseResponse<[HomeStoreConfigModel]>>)? {
|
return Services.homeStoreConfig(storeId: storeId.value)
|
}
|
}
|
|
class HomeVC: BaseVC{
|
@IBOutlet weak var collectionView: BaseCollectionView!
|
@IBOutlet weak var label_vipInfo: UILabel!
|
|
private var layout:WaterFallFlowLayout!
|
@IBOutlet weak var label_store: UILabel!
|
private var items = Array<HomeStoreConfigModel>()
|
private var storeModel:HomeStoreModel?
|
var viewModel = HomeViewModel()
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
if storeModel == nil{
|
startLocation()
|
}
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
viewModel.configure(collectionView,needMore: false)
|
|
startLocation()
|
|
viewModel.dataSource.subscribe(onNext: {[weak self] data in
|
guard let weakSelf = self else { return }
|
guard data.count != 0 else {return}
|
weakSelf.items = data
|
let group = DispatchGroup()
|
|
for m in weakSelf.items{
|
let queue = DispatchQueue(label: "1")
|
queue.async(group: group) {
|
group.enter()
|
m.radio = m.type.defaultImg.size.width / m.type.defaultImg.size.height //默认比例
|
SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage),options: .scaleDownLargeImages,progress: nil) { image, data, error, status in
|
if let i = image{
|
m.radio = i.size.width / i.size.height
|
}
|
group.leave()
|
}
|
}
|
}
|
|
group.notify(queue: .main){
|
//重置Layout,不然不更新
|
self!.layout = WaterFallFlowLayout()
|
self!.layout.cols = 2
|
self!.layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34)
|
self!.layout.delegate = self
|
self!.collectionView.collectionViewLayout = self!.layout
|
self!.collectionView.reloadData()
|
}
|
|
}).disposed(by: disposeBag)
|
|
Services.startCourseHome().subscribe(onNext: {[weak self] data in
|
if let model = data.data{
|
if model.isThere == 1{
|
let courseInfoVC = CourseInfoVC()
|
courseInfoVC.startClouseHomeModel = model
|
let clouseNav = BaseNav(rootViewController: courseInfoVC)
|
clouseNav.tabBarItem = UITabBarItem(title: "开始上课", image: UIImage(named: "tabbar_course"), selectedImage: UIImage(named: "tabbar_course_s")?.withRenderingMode(.alwaysOriginal))
|
self?.tabBarController?.viewControllers![1] = clouseNav
|
}else {
|
if let vc = (self?.tabBarController?.viewControllers?[1] as? BaseNav)?.topViewController as? CourseVC{
|
vc.startClouseHomeModel = model
|
}
|
}
|
}
|
}).disposed(by: disposeBag)
|
}
|
|
override func setRx() {
|
viewModel.storeId.subscribe(onNext: { id in
|
UserDefaults.standard.set(id, forKey: "Current_StoreID")
|
UserDefaults.standard.synchronize()
|
}) { error in
|
|
}.disposed(by: disposeBag)
|
|
}
|
|
override func setUI() {
|
view.backgroundColor = UIColor(hexStr: "EEF0F3")
|
|
layout = WaterFallFlowLayout()
|
layout.cols = 2
|
layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34)
|
layout.delegate = self
|
collectionView.collectionViewLayout = layout
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.backgroundColor = .white
|
collectionView.register(UINib(nibName: "HomeCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeCCell")
|
collectionView.jq_setEmptyView("无数据", image: UIImage(named: "icon_empty"), foregroundColor: .gray, clouse: nil)
|
}
|
|
private func getStoreInfo(){
|
Services.homeInfo().subscribe(onNext: {[weak self] data in
|
if let m = data.data{
|
self?.storeModel = m
|
self?.label_store.text = m.name.isEmpty ? "门店获取失败":m.name
|
var text = "欢迎来到 快乐运动俱乐部"
|
if m.isVip == 0{text.append("\n加入我们 成为会员")}
|
self?.label_vipInfo.text = text
|
|
if m.storeId != 0{
|
self?.viewModel.storeId.accept(m.storeId)
|
self?.viewModel.beginRefresh()
|
}else{
|
self?.defaultData()
|
}
|
}
|
}) { [weak self] error in
|
self?.label_store.text = "门店获取失败"
|
self?.defaultData()
|
}.disposed(by: disposeBag)
|
}
|
|
private func defaultData(){
|
for index in 1...8{
|
let type = HomeItemType(rawValue: index)!
|
let m = HomeStoreConfigModel()
|
m.type = type
|
m.isOpen = 1
|
m.sort = index
|
m.radio = type.defaultImg.size.width / type.defaultImg.size.height
|
items.append(m)
|
}
|
collectionView.reloadData()
|
}
|
|
private func startLocation(){
|
locationTool.startLocation { [weak self] local in
|
locationTool.stopLocation()
|
self?.getStoreInfo()
|
} errorClouse: { [weak self] error in
|
alertError(msg: "定位获取失败")
|
self?.label_store.text = "定位获取失败"
|
self?.getStoreInfo()
|
}
|
}
|
|
@IBAction func scanAction(_ sender: UIButton) {
|
let codeVC = CommonScanQRCodeVC { str, status in
|
|
}
|
push(vc: codeVC)
|
}
|
|
|
@IBAction func joinMemberAction(_ sender: UIButton) {
|
let joinMemberVC = JoinMemberIntroduceVC()
|
push(vc: joinMemberVC)
|
}
|
|
|
@IBAction func chooseStoresAction(_ sender: TapBtn) {
|
StoresChooseView.show { [weak self] storeId,storeName in
|
self?.viewModel.storeId.accept(storeId)
|
self?.label_store.text = storeName
|
self?.viewModel.beginRefresh()
|
}
|
}
|
|
override var preferredStatusBarStyle: UIStatusBarStyle{
|
return .lightContent
|
}
|
}
|
|
extension HomeVC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
// let vc = JQ_CommonWebViewController(url: "http://192.168.110.103:8080/#/packageA/shares/index")
|
// vc.modalPresentationStyle = .fullScreen
|
// present(vc, animated: true)
|
// push(vc: vc)
|
// return
|
|
let item = items[indexPath.row]
|
|
switch item.type{
|
case .course:
|
let vc = CourseListVC()
|
push(vc: vc)
|
case .booking:
|
let vc = YardListVC()
|
push(vc: vc)
|
case .activity:
|
let vc = ActivityListVC()
|
push(vc: vc)
|
case .video:
|
let vc = CourseOnlineListVC(position: 2)
|
vc.title = "看视频得奖励"
|
push(vc: vc)
|
case .coin:
|
let vc = CourseOnlineListVC(position: 1)
|
vc.title = "线上课得积分"
|
push(vc: vc)
|
case .ticket:
|
let vc = WelfareWeeklyListVC()
|
push(vc: vc)
|
case .wisdomCourt:
|
let vc = GamesVC()
|
push(vc: vc)
|
case .welfare:
|
if viewModel.storeId.value != 0{
|
Services.queryStoreFreeBenefit(id: viewModel.storeId.value).subscribe(onNext: {[weak self] data in
|
if let m = data.data{
|
let vc = WelfareFreeVC(m)
|
self?.push(vc: vc)
|
}
|
}).disposed(by: disposeBag)
|
}else{
|
alertError(msg: "当前门店获取失败")
|
}
|
case .none:break
|
}
|
}
|
|
}
|
|
extension HomeVC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeCCell", for: indexPath) as! HomeCCell
|
let item = items[indexPath.row]
|
cell.img.sd_setImage(with: URL(string: item.backgroundImage), placeholderImage: item.type.defaultImg, options: .highPriority)
|
cell.img.cornerRadius = 10
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return items.count
|
}
|
|
}
|
|
extension HomeVC: WaterFallLayoutDelegate{
|
func waterFlowLayout(_ waterFlowLayout: WaterFallFlowLayout, itemHeight indexPath: IndexPath) -> CGFloat {
|
let item = items[indexPath.row]
|
|
if indexPath.row % 2 != 0{
|
return 185
|
}else{
|
return 288
|
}
|
}
|
}
|