| | |
| | | //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! |
| | | |
| | | @IBOutlet weak var view_banner: CommonBannerView! |
| | | |
| | | 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() |
| | | } |
| | | } |
| | | private var storeId:Int? |
| | | private var currentLocal:CLLocation? |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | | viewModel.configure(collectionView,needMore: false) |
| | | startLocation() |
| | | |
| | | Services.startCourseHome().subscribe(onNext: {[weak self] data in |
| | |
| | | } |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | } |
| | | |
| | | override func setRx() { |
| | | viewModel.storeId.subscribe(onNext: { id in |
| | | UserDefaults.standard.set(id, forKey: "Current_StoreID") |
| | | UserDefaults.standard.synchronize() |
| | | Services.bannerList(position: .homeTop).subscribe(onNext: {[weak self] data in |
| | | if let models = data.data{ |
| | | let imgs = models.compactMap({$0.img}) |
| | | self?.view_banner.setImages(images: imgs, type: .URL) { index in |
| | | |
| | | } |
| | | } |
| | | }) { error in |
| | | |
| | | }.disposed(by: disposeBag) |
| | | |
| | | 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() |
| | | |
| | | let queue = DispatchQueue(label: "imgRadio") |
| | | queue.async(group: group){ |
| | | group.enter() |
| | | Services.homeStoreConfig2(storeId: weakSelf.viewModel.storeId.value).subscribe(onNext: { data in |
| | | if let items = data.data{ |
| | | weakSelf.items.append(contentsOf: items) |
| | | var i = 0 |
| | | for (_,model) in weakSelf.items.enumerated(){ |
| | | SDWebImageDownloader.shared.downloadImage(with: URL(string: model.backgroundImage)) { image, data, error, status in |
| | | if let i = image{ |
| | | model.radio = i.size.width / i.size.height |
| | | }else{ |
| | | model.radio = model.type.defaultImg.size.width / model.type.defaultImg.size.height |
| | | } |
| | | i += 1 |
| | | if i == weakSelf.items.count - 1{ |
| | | group.leave() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }) { error in |
| | | group.leave() |
| | | }.disposed(by: weakSelf.disposeBag) |
| | | } |
| | | |
| | | 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) |
| | | |
| | | } |
| | | |
| | |
| | | if let storeStr = UserDefaults.standard.object(forKey: "CurrentStore") as? String{ |
| | | if let deserModel = HomeStoreModel.deserialize(from: storeStr){ |
| | | self?.label_store.text = deserModel.name.isEmpty ? "门店获取失败":deserModel.name |
| | | self?.viewModel.storeId.accept(deserModel.storeId) |
| | | self?.storeModel = deserModel |
| | | self?.storeId = deserModel.storeId |
| | | if deserModel.isVip == 0{text.append("\n加入我们 成为会员")} |
| | | self?.label_vipInfo.text = text |
| | | } |
| | | }else{ |
| | | self?.viewModel.storeId.accept(m.storeId) |
| | | self?.label_store.text = m.name.isEmpty ? "门店获取失败":m.name |
| | | UserDefaults.standard.set(m.toJSONString(), forKey: "CurrentStore") |
| | | UserDefaults.standard.synchronize() |
| | | self?.storeModel = m |
| | | self?.storeId = m.storeId |
| | | if m.isVip == 0{text.append("\n加入我们 成为会员")} |
| | | self?.label_vipInfo.text = text |
| | | } |
| | | self?.viewModel.beginRefresh() |
| | | self?.getStoreItemList() |
| | | } |
| | | } |
| | | }) { [weak self] error in |
| | |
| | | }.disposed(by: disposeBag) |
| | | } |
| | | |
| | | private func getStoreItemList(){ |
| | | |
| | | showHUD() |
| | | items.removeAll() |
| | | let group = DispatchGroup() |
| | | let queue = DispatchQueue(label: "imgRadio") |
| | | queue.async(group: group){[weak self] () in |
| | | guard let weakSelf = self else { return } |
| | | group.enter() |
| | | Services.homeStoreConfig(storeId: weakSelf.storeId!).subscribe(onNext: {[weak self] data in |
| | | self?.items.append(contentsOf: (data.data ?? []).filter({$0.isOpen == 1})) |
| | | Services.homeStoreConfig2(storeId: weakSelf.storeId!).subscribe(onNext: {[weak self] data in |
| | | self?.items.append(contentsOf: (data.data ?? [])) |
| | | group.leave() |
| | | }) { error in |
| | | group.leave() |
| | | }.disposed(by: weakSelf.disposeBag) |
| | | }) { error in |
| | | group.leave() |
| | | }.disposed(by: weakSelf.disposeBag) |
| | | } |
| | | |
| | | group.notify(queue: .main){[weak self] () in |
| | | guard let weakSelf = self else { return } |
| | | var i = 0 |
| | | showHUD() |
| | | let w = (weakSelf.view.frame.width - 78.0) / 2.0 |
| | | for (_,model) in weakSelf.items.enumerated(){ |
| | | SDWebImageDownloader.shared.downloadImage(with: URL(string: model.backgroundImage), options: .useNSURLCache) { _, _,_ in |
| | | |
| | | } completed: { image, data, error, status in |
| | | if let img = image{ |
| | | i += 1 |
| | | model.cellHeight = w / (img.size.width / img.size.height) |
| | | }else{ |
| | | i += 1 |
| | | model.cellHeight = w / (model.type.defaultImg.size.width / model.type.defaultImg.size.height) |
| | | } |
| | | |
| | | print("Height:\(model.cellHeight)===\(model.type.rawValue)\n") |
| | | if i == weakSelf.items.count{ |
| | | //重置Layout,不然不更新 |
| | | self!.layout = WaterFallFlowLayout() |
| | | self!.layout.itemCount = self!.items.count |
| | | self!.layout.delegate = self |
| | | self!.layout.cols = 2 |
| | | self!.layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34) |
| | | self!.collectionView.collectionViewLayout = self!.layout |
| | | self!.collectionView.reloadData() |
| | | hiddenHUD() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private func startLocation(){ |
| | | locationTool.startLocation { [weak self] local in |
| | | locationTool.stopLocation() |
| | | self?.getStoreInfo() |
| | | guard self!.currentLocal != nil else { |
| | | self!.currentLocal = local |
| | | self!.getStoreInfo() |
| | | locationTool.stopLocation() |
| | | return |
| | | } |
| | | |
| | | } errorClouse: { [weak self] error in |
| | | alertError(msg: "定位获取失败") |
| | | self?.label_store.text = "定位获取失败" |
| | | self?.getStoreInfo() |
| | | } |
| | | } |
| | | |
| | |
| | | @IBAction func chooseStoresAction(_ sender: TapBtn) { |
| | | StoresChooseView.show { [weak self] storeId,storeName in |
| | | guard let weakSelf = self else { return } |
| | | weakSelf.viewModel.storeId.accept(storeId) |
| | | weakSelf.storeId = storeId |
| | | weakSelf.label_store.text = storeName |
| | | weakSelf.layout = WaterFallFlowLayout() |
| | | weakSelf.layout.cols = 2 |
| | | weakSelf.layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34) |
| | | weakSelf.layout.delegate = self! |
| | | weakSelf.collectionView.collectionViewLayout = weakSelf.layout |
| | | weakSelf.viewModel.beginRefresh() |
| | | weakSelf.items.removeAll() |
| | | weakSelf.getStoreItemList() |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | 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{ |
| | |
| | | let vc = WelfareWeeklyListVC() |
| | | push(vc: vc) |
| | | case .wisdomCourt: |
| | | guard viewModel.storeId.value != 0 else {alert(msg: "请先选择门店");return} |
| | | guard storeId != 0 else {alert(msg: "请先选择门店");return} |
| | | 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 storeId != 0{ |
| | | Services.queryStoreFreeBenefit(id: storeId!).subscribe(onNext: {[weak self] data in |
| | | if let m = data.data{ |
| | | let vc = WelfareFreeVC(m) |
| | | self?.push(vc: vc) |
| | |
| | | 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.sd_setImage(with: URL(string: item.backgroundImage), placeholderImage: item.type.defaultImg, options: .lowPriority) |
| | | cell.img.cornerRadius = 10 |
| | | return cell |
| | | } |
| | |
| | | extension HomeVC: WaterFallLayoutDelegate{ |
| | | func waterFlowLayout(_ waterFlowLayout: WaterFallFlowLayout, itemHeight indexPath: IndexPath) -> CGFloat { |
| | | let item = items[indexPath.row] |
| | | let w = (view.frame.width - 68 - waterFlowLayout.minimumLineSpacing) / Double(waterFlowLayout.cols) |
| | | return w / item.radio |
| | | print("===\(item.cellHeight)") |
| | | return item.cellHeight |
| | | } |
| | | } |
| | | |