无故事王国
2023-11-02 cff96b5ae124efe4379cb4dc1695d668f3190a49
WanPai/Root/Home/VC/HomeVC.swift
@@ -34,6 +34,12 @@
        super.viewDidLoad()
      startLocation()
//      let vc = PaymentResultVC(result: .success, objType: .member)
//      push(vc: vc)
        Services.startCourseHome().subscribe(onNext: {[weak self] data in
            if let model = data.data{
                if model.isThere == 1{
@@ -110,70 +116,77 @@
   private func getStoreItemList(){
      showHUD()
//      showHUD()
      items.removeAll()
      Services.homeStoreConfig(storeId: storeId!).subscribe(onNext: {[weak self] data in
         guard let weakSelf = self else { return }
         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 ?? []))
            self?.downloadImgs()
         }) { error in
         }.disposed(by: weakSelf.disposeBag)
      }) { error in
      }.disposed(by: disposeBag)
   }
   //图片下载
   private func downloadImgs(){
      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(){
            if let m = model as? HomeStoreConfigModel{
               SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
               } completed: {[weak self]  image, data, error, status in
                  if let img = image{
                     m.cellHeight = w / (img.size.width / img.size.height)
                  }else{
                     m.cellHeight =  w / (m.type.defaultImg.size.width / m.type.defaultImg.size.height)
      let w = (view.frame.width - 78.0) / 2.0
      for item in items{
         if let m = item as? HomeStoreConfigModel{
            queue.async(group: group){ () in
               group.enter()
               guard SDImageCache.shared.diskImageDataExists(withKey: m.backgroundImage) else {
                  SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
                  } completed: { image, data, error, status in
                     if let img = image{
                        m.cellHeight = w / (img.size.width / img.size.height)
                     }else{
                        m.cellHeight =  w / (m.type.defaultImg.size.width / m.type.defaultImg.size.height)
                     }
                     group.leave()
                  }
                  i += 1
                  if i == weakSelf.items.count{
                     self?.updateUI()
                  }
                  return
               }
               if let data = SDImageCache.shared.diskCache.data(forKey: m.backgroundImage),let img = UIImage(data: data){
                  m.cellHeight = w / (img.size.width / img.size.height)
                  group.leave()
               }
            }
         }
            if let m = model as? HomeStoreTopBannerModel{
               SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
               } completed: {[weak self]  image, data, error, status in
                  if let img = image{
                     m.cellHeight = w / (img.size.width / img.size.height)
                     i += 1
                     if i == weakSelf.items.count{
                        self?.updateUI()
         if let m = item as? HomeStoreTopBannerModel{
            queue.async(group: group){() in
               group.enter()
               guard SDImageCache.shared.diskImageDataExists(withKey: m.backgroundImage) else {
                  SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage), options: .useNSURLCache) { _, _,_ in
                  } completed: { image, data, error, status in
                     if let img = image{
                        m.cellHeight = w / (img.size.width / img.size.height)
                     }
                     group.leave()
                  }
               return
               }
               if let data = SDImageCache.shared.diskCache.data(forKey: m.backgroundImage),let img = UIImage(data: data){
                  m.cellHeight = w / (img.size.width / img.size.height)
                  group.leave()
               }
            }
         }
      }
   }
   private func updateUI(){
         //重置Layout,不然不更新
      self.collectionView.collectionViewLayout = self.layout
      self.collectionView.reloadData()
      hiddenHUD()
      group.notify(queue: .main) {
         self.layout.collectionView?.reloadData()
         self.collectionView.reloadData()
      }
   }
   private func startLocation(){
@@ -311,6 +324,7 @@
extension HomeVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeCCell", for: indexPath) as! HomeCCell
      guard indexPath.row < items.count else {return cell}
        let item = items[indexPath.row]
      if let m = item as? HomeStoreConfigModel{
@@ -326,23 +340,6 @@
    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 let m = item as? HomeStoreConfigModel{
         return  m.cellHeight
      }
      if let m = item as? HomeStoreTopBannerModel{
         return  m.cellHeight
      }
      return 0
    }
}