// // SPHomeTrendingView.swift // ShortPlay // // Created by 曾觉新 on 2025/4/14. // import UIKit class SPHomeTrendingView: SPHomeDataItemView { private static func itemSize() -> CGSize { let width = floor((kSPScreenWidth - 15 * 2 - 12 * 2) / 3) let imageScale: CGFloat = 153 / 107 let height = width * imageScale + 38 return CGSize(width: width, height: height) } override class func contentHeight(dataArr: [SPShortModel]) -> CGFloat { var height = self.contentToTop var lineCount = dataArr.count / 3 if dataArr.count % 3 > 0 { lineCount += 1 } let contentHeight = itemSize().height * CGFloat(lineCount) + 12 * CGFloat(lineCount - 1) if contentHeight > 0 { height += contentHeight } else { height += 1 } return height } override var dataArr: [SPShortModel]? { didSet { self.collectionView.reloadData() } } private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 12 layout.minimumInteritemSpacing = 12 layout.itemSize = Self.itemSize() layout.sectionInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15) return layout }() private lazy var collectionView: SPCollectionView = { let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) collectionView.delegate = self collectionView.dataSource = self collectionView.isScrollEnabled = false SPHomeTrendingCell.registerCell(collectionView: collectionView) return collectionView }() override init(frame: CGRect) { super.init(frame: frame) self.titleLabel.text = "Trending Now".localized _setupUI() } @MainActor required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension SPHomeTrendingView { private func _setupUI() { contentView.addSubview(collectionView) collectionView.snp.makeConstraints { make in make.edges.equalToSuperview() } } } //MARK: -------------- UICollectionViewDataSource & UICollectionViewDelegate -------------- extension SPHomeTrendingView: UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = SPHomeTrendingCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath) cell.model = dataArr?[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataArr?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let model = self.dataArr?[indexPath.row] let vc = SPTVPlayerListViewController() vc.shortPlayId = model?.short_play_id self.viewController?.navigationController?.pushViewController(vc, animated: true) } }