Music_Player3/relax.offline.mp3.music/MP/MPPositive/Views/Home/MPPositive_HomeShowTableViewCell.swift
2024-07-05 17:25:49 +08:00

198 lines
9.5 KiB
Swift

//
// MPPositive_HomeShowTableViewCell.swift
// MusicPlayer
//
// Created by Mr.Zhou on 2024/4/22.
//
import UIKit
///bCell
class MPPositive_HomeShowTableViewCell: UITableViewCell {
//
enum HomeShowType:Int {
case Frist = 0
case Second = 1
case Third = 2
case Fourth = 3
case Fifth = 4
///layout
var layout:UICollectionViewFlowLayout {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = .init(top: 15*width, left: 16*width, bottom: 0, right: 16*width)
layout.minimumLineSpacing = 8*width
switch self {
case .Frist:
layout.itemSize = .init(width: 168*width, height: 134*width)
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
case .Second:
layout.itemSize = .init(width: 96*width, height: 121*width)
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
case .Third:
layout.itemSize = .init(width: 109*width, height: 152*width)
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
case .Fourth:
layout.itemSize = .init(width: 94*width, height: 116*width)
layout.minimumInteritemSpacing = 0
layout.scrollDirection = .horizontal
case .Fifth:
layout.itemSize = .init(width: 339*width, height: 237*width)
layout.sectionInset = .init(top: 15*width, left: 16*width, bottom: 0, right: 20*width)
layout.minimumInteritemSpacing = 16
layout.scrollDirection = .vertical
}
return layout
}
}
//Label
private lazy var titleLabel:UILabel = createLabel("Title", font: .systemFont(ofSize: 18*width, weight: .regular), textColor: .white, textAlignment: .left)
//
private lazy var moreBtn:UIButton = {
let btn:UIButton = .init()
btn.setBackgroundImage(UIImage(named: "Home_More'logo"), for: .normal)
btn.addTarget(self, action: #selector(moreFindClick(_ :)), for: .touchUpInside)
return btn
}()
//collectionView
private lazy var collectionView:UICollectionView = {
let collectionView:UICollectionView = .init(frame: .init(x: 0, y: 0, width: screen_Width, height: screen_Height), collectionViewLayout: showType.layout)
collectionView.showsVerticalScrollIndicator = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.backgroundColor = .clear
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(MPPositive_HomeListFirstCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFirstCollectionViewCellID)
collectionView.register(MPPositive_HomeListSecondCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListSecondCollectionViewCellID)
collectionView.register(MPPositive_HomeListThirdCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListThirdCollectionViewCellID)
collectionView.register(MPPositive_HomeListFourthCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFourthCollectionViewCellID)
collectionView.register(MPPositive_HomeListFifthCollectionViewCell.self, forCellWithReuseIdentifier: MPPositive_HomeListFifthCollectionViewCellID)
return collectionView
}()
private let MPPositive_HomeListFirstCollectionViewCellID = "MPPositive_HomeListFirstCollectionViewCell"
private let MPPositive_HomeListSecondCollectionViewCellID = "MPPositive_HomeListSecondCollectionViewCell"
private let MPPositive_HomeListThirdCollectionViewCellID = "MPPositive_HomeListThirdCollectionViewCell"
private let MPPositive_HomeListFourthCollectionViewCellID = "MPPositive_HomeListFourthCollectionViewCell"
private let MPPositive_HomeListFifthCollectionViewCellID = "MPPositive_HomeListFifthCollectionViewCell"
///
var browseViewModel:MPPositive_BrowseModuleListViewModel!{
didSet{
titleLabel.text = browseViewModel.title
}
}
///
var showType:HomeShowType = .Frist{
didSet{
collectionView.collectionViewLayout = showType.layout
collectionView.reloadData()
collectionView.scrollToItem(at: .init(row: 0, section: 0), at: .top, animated: false)
}
}
////
var requestNextBlock:((MPPositive_BrowseItemViewModel) -> Void)?
///
var findMoreBlock:(() -> Void)?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
confirgue()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
//
private func confirgue() {
selectionStyle = .none
backgroundColor = .clear
contentView.backgroundColor = .clear
//UI
contentView.addSubview(moreBtn)
moreBtn.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-16*width)
make.top.equalToSuperview()
make.width.height.equalTo(24*width)
}
contentView.addSubview(titleLabel)
titleLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(16*width)
make.bottom.equalTo(moreBtn.snp.bottom)
make.right.equalTo(moreBtn.snp.left).offset(-10*width)
}
contentView.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.top.equalTo(moreBtn.snp.bottom)
make.left.right.equalToSuperview()
make.bottom.equalToSuperview().offset(-32*width).priority(999)
}
}
//
@objc private func moreFindClick(_ sender:UIButton) {
guard findMoreBlock != nil else {
return
}
findMoreBlock!()
}
//tableViewCell
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
collectionView.layoutIfNeeded()
switch showType {
case .Fifth:
//
let height = collectionView.collectionViewLayout.collectionViewContentSize.height
return CGSize(width: size.width, height: size.height + height)
default:
//
let height = showType.layout.itemSize.height + showType.layout.sectionInset.top + showType.layout.sectionInset.bottom
return CGSize(width: size.width, height: size.height + height)
}
}
}
//MARK: - collectionView
extension MPPositive_HomeShowTableViewCell:UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return browseViewModel.items.count > 5 ? 5:browseViewModel.items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch showType {
case .Frist:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFirstCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFirstCollectionViewCell
cell.itemViewModel = browseViewModel.items[indexPath.row]
return cell
case .Second:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListSecondCollectionViewCellID, for: indexPath) as! MPPositive_HomeListSecondCollectionViewCell
cell.itemViewModel = browseViewModel.items[indexPath.row]
return cell
case .Third:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListThirdCollectionViewCellID, for: indexPath) as! MPPositive_HomeListThirdCollectionViewCell
cell.itemViewModel = browseViewModel.items[indexPath.row]
return cell
case .Fourth:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFourthCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFourthCollectionViewCell
cell.itemViewModel = browseViewModel.items[indexPath.row]
return cell
case .Fifth:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MPPositive_HomeListFifthCollectionViewCellID, for: indexPath) as! MPPositive_HomeListFifthCollectionViewCell
cell.itemViewModel = browseViewModel.items[indexPath.row]
return cell
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard requestNextBlock != nil else {
return
}
requestNextBlock!(browseViewModel.items[indexPath.row])
}
}