瀏覽代碼

封装了一个简单的 collectionView

100Years 1 月之前
父節點
當前提交
5e1823f61b

+ 24 - 25
AIEmoji/Business/TSAILIstVC/TSAIListHistoryBaseVC/TSAIListHistoryBaseCell.swift

@@ -5,7 +5,30 @@
 //  Created by 100Years on 2025/4/18.
 //
 
-class TSAIListHistoryBaseCell: TSBaseCollectionCell {
+class TSAIListHistoryBaseCell: TSBaseCollectionCell,TSSimpleConfigurableView {
+    
+    var delegate: (any TSSmalCoacopods.TSSimpleCollectionViewDelegate)?
+    var indexPath: IndexPath?
+    var data: Any? {
+        didSet {
+            if let dataModel = data as? TSActionInfoModel{
+                videoIconImageView.isHidden = true
+                exampleView.isHidden = true
+                if dataModel.modelType == .example {
+                    textLabel.text = "Example".localized
+                    exampleView.isHidden = false
+                    showImageView.image = UIImage(named: dataModel.response.resultUrl)
+                }else{
+                    if dataModel.isVideo {
+                        videoIconImageView.isHidden = false
+                        self.showImageView.image = UIImage(contentsOfFile: dataModel.videoThumbnailURL.path)
+                    }else {
+                        showImageView.setAsyncImage(urlString: dataModel.response.resultUrl,contentMode: .scaleAspectFill,backgroundColor: .white.withAlphaComponent(0.1))
+                    }
+                }
+            }
+        }
+    }
     
     lazy var textLabel: UILabel = {
         let textLabel = UILabel.createLabel(
@@ -65,28 +88,4 @@ class TSAIListHistoryBaseCell: TSBaseCollectionCell {
             make.width.height.equalTo(24)
         }
     }
-    
-    override func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
-        super.renderView(with: object, component: component, attributes: attributes)
-        debugPrint("TSAIListHistoryBaseCell renderView")
-        if let itemModel = object as? TSGenmojiCoLItemModel{
-            videoIconImageView.isHidden = true
-            exampleView.isHidden = true
-            if itemModel.dataModel.modelType == .example {
-                if itemModel.style == .ptpPicHistory {
-                    textLabel.text = "Example".localized
-                }
-                exampleView.isHidden = false
-                showImageView.image = UIImage(named: itemModel.dataModel.response.resultUrl)
-            }else{
-                if itemModel.dataModel.isVideo {
-                    videoIconImageView.isHidden = false
-                    self.showImageView.image = UIImage(contentsOfFile: itemModel.dataModel.videoThumbnailURL.path)
-                }else {
-                    showImageView.setAsyncImage(urlString: itemModel.dataModel.response.resultUrl,contentMode: .scaleAspectFill,backgroundColor: .white.withAlphaComponent(0.1))
-                }
-            }
-        }
-    }
-    
 }

+ 85 - 51
AIEmoji/Business/TSAILIstVC/TSAIListHistoryBaseVC/TSAIListHistoryBaseVC.swift

@@ -6,7 +6,6 @@
 //
 
 class TSAIListHistoryBaseVC: TSBaseVC {
-    
     var generatorStyle:TSGeneratorImageStyle
     var titleString:String?
     init(titleString:String? = nil,generatorStyle:TSGeneratorImageStyle) {
@@ -18,51 +17,26 @@ class TSAIListHistoryBaseVC: TSBaseVC {
         fatalError("init(coder:) has not been implemented")
     }
     
-    lazy var viewModel: TSAIListHistoryBaseVM = {
-        let viewModel = TSAIListHistoryBaseVM(generatorStyle: generatorStyle)
-        return viewModel
-    }()
+    private let kPTPHistoryW = (k_ScreenWidth-32.0-12.0-2)/2.0
     
     //###################################### 集合视图 ######################################
     let collectionViewBtootm:CGFloat = 80
-    lazy var collectionComponent: TSCollectionViewComponent = {
-        let layout = UICollectionViewFlowLayout()
-        let cp = TSCollectionViewComponent(frame: CGRect.zero, layout: layout, attributes: [:])
-        cp.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: collectionViewBtootm, right: 0)
-        
+    lazy var collectionView: TSSimpleCollectionView = {
+        let identifier = "TSAIListHistoryBaseCell"
         
-        // 禁用自动 contentInset 调整
-          if #available(iOS 11.0, *) {
-              cp.collectionView.contentInsetAdjustmentBehavior = .never
-          } else {
-              automaticallyAdjustsScrollViewInsets = false
-          }
-        
-        cp.sectionActionHandler = { [weak self] cellCp, indexPath in
-            guard let self = self else { return }
-            if let cmd = cellCp as? String, cmd == "delete"  {
-                showCustomAlert(message: "Are you sure to delete".localized, deleteHandler:  {
-                    self.viewModel.removeAllHistoryList()
-                    self.collectionComponent.clear()
-                    self.collectionComponent.reloadView(with: self.viewModel.colDataArray)
-                })
-            }
-        }
+        let itemW = (k_ScreenWidth-32.0-12.0-2.0)/2.0
+        let itemH = kGetScaleHeight(originalSize: CGSize(width: 165.0, height: 293.0), width: itemW)
         
-        cp.itemDidSelectedHandler = { [weak self] (object, indexPath) in
-            guard let self = self else { return }
- 
-            if let sections = viewModel.colDataArray.safeObj(At: indexPath.section) as? TSGenmojiCoLSectionModel{
-                var dataModelArray:[TSActionInfoModel] = []
-                for itemModel in sections.items {
-                    dataModelArray.append(itemModel.dataModel)
-                }
-                
-                let browseVC = TSAIPhotoBrowseVC()
-                browseVC.dataModelArray = dataModelArray
-                browseVC.currentIndex = indexPath.item
-                kPresentModalVC(target: self, modelVC: browseVC,transitionStyle: .crossDissolve)
-            }
+        let layout = UICollectionViewFlowLayout()
+        let cp = TSSimpleCollectionView()
+        cp.layout.itemSize = CGSize(width: itemW, height: itemH)
+        cp.layout.minimumLineSpacing = 12
+        cp.layout.minimumInteritemSpacing = 12
+        cp.delegate = self
+        cp.collectionView.contentInset = UIEdgeInsets(top: 16, left: 16, bottom: collectionViewBtootm, right: 16)
+        cp.registerCell(TSAIListHistoryBaseCell.self,identifier:identifier)
+        cp.cellIdentifierForItem = { data in
+            return identifier
         }
 
         return cp
@@ -90,29 +64,89 @@ class TSAIListHistoryBaseVC: TSBaseVC {
         navRightBtn = setNavigationItem("", imageName: "delete", direction: .right, action: #selector(clickNavRight))
    
         contentView.addSubview(pageNullView)
-        contentView.addSubview(collectionComponent.collectionView)
-        collectionComponent.collectionView.snp.makeConstraints { make in
+        contentView.addSubview(collectionView)
+        collectionView.snp.makeConstraints { make in
             make.edges.equalToSuperview()
         }
-        debugPrint("777")
-        updateView()
-        debugPrint("788")
     }
     
+    override func dealThings() {
+        DispatchQueue.global(qos: .userInitiated).async {
+            _ = self.listModelArray
+            DispatchQueue.main.async {
+                self.updateView()
+            }
+        }
+    }
     
     func updateView() {
-        collectionComponent.clear()
-        collectionComponent.reloadView(with:viewModel.colDataArray)
+        collectionView.reload(with: [listModelArray])
         
-        navRightBtn.isHidden = viewModel.colDataArray.count <= 0
-        pageNullView.isHidden = viewModel.colDataArray.count > 0
+        navRightBtn.isHidden = listModelArray.count <= 0
+        pageNullView.isHidden = listModelArray.count > 0
     }
     
     @objc func clickNavRight() {
         showCustomAlert(message: "Are you sure to delete".localized, deleteHandler:  {
-            self.viewModel.removeAllHistoryList()
+            self.removeAllHistoryList()
             self.updateView()
         })
     }
     
 }
+
+extension TSAIListHistoryBaseVC: TSSimpleCollectionViewDelegate {
+    
+    func collectionView(didTrigger event: TSSmalCoacopods.TSSimpleCellEvent) {
+        switch event.action {
+        case .tap:
+            let browseVC = TSAIPhotoBrowseVC()
+            browseVC.dataModelArray = listModelArray
+            browseVC.currentIndex = event.indexPath.item
+            kPresentModalVC(target: self, modelVC: browseVC,transitionStyle: .crossDissolve)
+        default:
+            break
+        }
+    }
+    
+}
+
+extension TSAIListHistoryBaseVC{
+    func removeAllHistoryList(){
+        switch generatorStyle {
+        case .ageOld:
+            TSChangeOldAgeHistory.shared.removeALLModel()
+        case .ageChild:
+            TSChangeBabyAgeHistory.shared.removeALLModel()
+        case .oldPhoto:
+            TSChangeOldPhotoHistory.shared.removeALLModel()
+        case .eyeOpen:
+            TSAIEyeOpenHistory.shared.removeALLModel()
+        case .pretty:
+            TSAIPhotoPrettyHistory.shared.removeALLModel()
+        case .photoLive:
+            TSAIPhotoLiveHistory.shared.removeALLModel()
+        case .photoExpand:
+            TSAIPhotoExpandHistory.shared.removeALLModel()
+        }
+    }
+    
+    var listModelArray:[TSActionInfoModel]{
+        switch generatorStyle {
+        case .ageOld:
+            TSChangeOldAgeHistory.shared.listModels
+        case .ageChild:
+            TSChangeBabyAgeHistory.shared.listModels
+        case .oldPhoto:
+            TSChangeOldPhotoHistory.shared.listModels
+        case .eyeOpen:
+            TSAIEyeOpenHistory.shared.listModels
+        case .pretty:
+            TSAIPhotoPrettyHistory.shared.listModels
+        case .photoLive:
+            TSAIPhotoLiveHistory.shared.listModels
+        case .photoExpand:
+            TSAIPhotoExpandHistory.shared.listModels
+        }
+    }
+}

+ 1 - 27
AIEmoji/Business/TSAILIstVC/TSAIListHistoryBaseVC/TSAIListHistoryBaseVM.swift

@@ -9,34 +9,10 @@ import Alamofire
 import ObjectMapper
 class TSAIListHistoryBaseVM {
 
-    var colDataArray:[TSComponent] = [TSComponent]()
-    lazy var historySeciton: TSGenmojiCoLSectionModel = {
-        debugPrint("123")
-        let sectionModel = TSGenmojiCoLSectionModel()
-        sectionModel.style = .changeAgeHistory
-        sectionModel.name = "History".localized
-        for model in listModelArray {
-            let itemModel = TSGenmojiCoLItemModel()
-            itemModel.style = sectionModel.style
-            itemModel.dataModel = model
-            sectionModel.items.append(itemModel)
-        }
-        debugPrint("456")
-        return sectionModel
-    }()
-    
+
     var generatorStyle:TSGeneratorImageStyle
     init(generatorStyle:TSGeneratorImageStyle) {
         self.generatorStyle = generatorStyle
-        self.combinedData()
-    }
-    
-    func combinedData(){
-        colDataArray.removeAll()
-        
-        if historySeciton.items.count > 0 {
-            colDataArray.append(historySeciton)
-        }
     }
     
     func removeAllHistoryList(){
@@ -56,8 +32,6 @@ class TSAIListHistoryBaseVM {
         case .photoExpand:
             TSAIPhotoExpandHistory.shared.removeALLModel()
         }
-        
-        colDataArray.removeAll()
     }
     
 }