135 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Swift
		
	
	
	
		
		
			
		
	
	
			135 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Swift
		
	
	
	
|  | // | ||
|  | //  CLBaseGridController.swift | ||
|  | //  Crush | ||
|  | // | ||
|  | //  Created by Leon on 2025/7/24. | ||
|  | // | ||
|  | 
 | ||
|  | import UIKit | ||
|  | 
 | ||
|  | class CLBaseGridController: CLBaseViewController,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{ | ||
|  |     // MARK: - Properties | ||
|  | 
 | ||
|  |     var collectionView: UICollectionView! | ||
|  |     var layout: UICollectionViewFlowLayout! | ||
|  |     var page: Int = 1 | ||
|  |     var pageSize: Int = 10 | ||
|  |     var datas: [Any] = [] | ||
|  | 
 | ||
|  |     var listViewDidScrollCallback: ((UIScrollView) -> Void)? | ||
|  | 
 | ||
|  |     // MARK: - Lifecycle | ||
|  | 
 | ||
|  |     override func viewDidLoad() { | ||
|  |         super.viewDidLoad() | ||
|  |         initData() | ||
|  |         initUI() | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: - Setup | ||
|  | 
 | ||
|  |     private func initData() { | ||
|  |         page = 1 | ||
|  |         pageSize = 10 | ||
|  |         datas = [] | ||
|  |     } | ||
|  | 
 | ||
|  |     private func initUI() { | ||
|  |         layout = UICollectionViewFlowLayout() | ||
|  |         layout.minimumLineSpacing = 0 | ||
|  |         layout.minimumInteritemSpacing = 0 | ||
|  | 
 | ||
|  |         collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) | ||
|  |         collectionView.delegate = self | ||
|  |         collectionView.dataSource = self | ||
|  |         collectionView.backgroundColor = .clear | ||
|  |         collectionView.showsVerticalScrollIndicator = true | ||
|  |         collectionView.showsHorizontalScrollIndicator = false | ||
|  | 
 | ||
|  |         view.addSubview(collectionView) | ||
|  |         collectionView.snp.makeConstraints { make in | ||
|  |             make.top.equalTo(navigationView.snp.bottom) | ||
|  |             make.leading.trailing.bottom.equalTo(view) | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: - Public Methods | ||
|  | 
 | ||
|  |     func addRefreshHeaderFooter() { | ||
|  |         addRefreshHeader() | ||
|  |         addRefreshFooter() | ||
|  |     } | ||
|  | 
 | ||
|  |     func addRefreshHeader() { | ||
|  |         RefreshHeaderAnimator { [weak self] in | ||
|  |             self?.loadNewData() | ||
|  |         }.link(to: collectionView) | ||
|  |     } | ||
|  | 
 | ||
|  |     func addRefreshFooter() { | ||
|  |         RefreshFooterAnimator { [weak self] in | ||
|  |             self?.loadMoreData() | ||
|  |         }.link(to: collectionView) | ||
|  |     } | ||
|  | 
 | ||
|  |     func removeRefreshHeader() { | ||
|  |         collectionView.mj_header = nil | ||
|  |     } | ||
|  | 
 | ||
|  |     func removeRefreshFooter() { | ||
|  |         collectionView.mj_footer = nil | ||
|  |     } | ||
|  | 
 | ||
|  |     func loadNewData() { | ||
|  |         page = 1 | ||
|  |         loadData() | ||
|  |     } | ||
|  | 
 | ||
|  |     func loadMoreData() { | ||
|  |         page += 1 | ||
|  |         loadData() | ||
|  |     } | ||
|  | 
 | ||
|  |     func loadData() { | ||
|  |         // To be implemented by subclasses | ||
|  |     } | ||
|  | 
 | ||
|  |     func scrollCurrentContentTop() { | ||
|  |         collectionView.setContentOffset(.zero, animated: true) | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: - UICollectionViewDataSource | ||
|  | 
 | ||
|  |     func numberOfSections(in collectionView: UICollectionView) -> Int { | ||
|  |         return 1 | ||
|  |     } | ||
|  | 
 | ||
|  |     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
|  |         return datas.count | ||
|  |     } | ||
|  | 
 | ||
|  |     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
|  |         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) | ||
|  |         return cell | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: - UICollectionViewDelegate | ||
|  | 
 | ||
|  |     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | ||
|  |         // To be implemented by subclasses | ||
|  |     } | ||
|  | 
 | ||
|  |     // MARK: - UICollectionViewDelegateFlowLayout | ||
|  | 
 | ||
|  | //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | ||
|  | //        // Default size, to be overridden by subclasses | ||
|  | //        return CGSize(width: collectionView.frame.width, height: 72) | ||
|  | //    } | ||
|  | 
 | ||
|  |     // MARK: - UIScrollViewDelegate | ||
|  | 
 | ||
|  |     func scrollViewDidScroll(_ scrollView: UIScrollView) { | ||
|  |         listViewDidScrollCallback?(scrollView) | ||
|  |     } | ||
|  | } |