MoviaBox/Thimra/Class/Home/View/SPHomeExploreView.swift
2025-04-22 15:21:12 +08:00

110 lines
3.3 KiB
Swift

//
// SPHomeExploreView.swift
// Thimra
//
// Created by Overseas on 2025/4/22.
//
import UIKit
class SPHomeExploreView: SPHomeDataItemView {
private static func itemSize() -> CGSize {
let width = floor((kSPScreenWidth - 16 * 2 - 14 * 2) / 3)
let imageScale: CGFloat = 146 / 105
let height = width * imageScale + 36
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) + 14 * CGFloat(lineCount - 1)
if contentHeight > 0 {
height += contentHeight
} else {
height += 1
}
return height
}
override var dataArr: [SPShortModel]? {
didSet {
self.collectionView.reloadData()
}
}
//MARK: UI
private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.itemSize = Self.itemSize()
layout.minimumInteritemSpacing = 14
layout.minimumLineSpacing = 14
layout.sectionInset = .init(top: 0, left: 16, bottom: 0, right: 16)
return layout
}()
private lazy var collectionView: SPCollectionView = {
let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
SPHomeExploreCell.registerCell(collectionView: collectionView)
return collectionView
}()
override init(frame: CGRect) {
super.init(frame: frame)
titleLabel.text = "Explore For You".localized
iconImageView.image = UIImage(named: "mark_icon_01")
_setupUI()
}
@MainActor required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension SPHomeExploreView {
private func _setupUI() {
contentView.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
}
//MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource --------------
extension SPHomeExploreView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = SPHomeExploreCell.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 = SPPlayerDetailViewController()
vc.shortPlayId = model?.short_play_id
self.viewController?.navigationController?.pushViewController(vc, animated: true)
}
}