Veloria/Veloria/Class/Player/View/VPEpisodeView.swift
2025-06-17 14:43:34 +08:00

347 lines
11 KiB
Swift

//
// VPEpisodeView.swift
// Veloria
//
// Created by on 2025/5/23.
//
import UIKit
class VPEpisodeView: HWPanModalContentView {
var currentIndex: Int = 0 {
didSet {
self.collectionView.reloadData()
}
}
var shortModel: VPShortModel? {
didSet {
coverImageView.vp_setImage(url: shortModel?.image_url)
videoNameLabel.text = shortModel?.name
if let category = shortModel?.category?.first, !category.isEmpty {
tagView.setTitle(category, for: .normal)
tagView.isHidden = false
} else {
tagView.isHidden = true
}
desLabel.text = shortModel?.vp_description
}
}
var dataArr: [VPVideoInfoModel] = [] {
didSet {
self.collectionView.reloadData()
var menuDataArr = [String]()
let totalEpisode = dataArr.count
var index = 0
var remainingEpisodes = totalEpisode
while remainingEpisodes > 0 {
let minIndex = index * 30
var maxIndex = minIndex + 29
if maxIndex >= dataArr.count {
maxIndex = dataArr.count - 1
}
let minEpisode = dataArr[minIndex].episode ?? "0"
let maxEpisode = dataArr[maxIndex].episode ?? "0"
if minEpisode == maxEpisode {
menuDataArr.append("\(minEpisode)")
} else {
menuDataArr.append("\(minEpisode)-\(maxEpisode)")
}
remainingEpisodes -= 30
index += 1
}
self.menuView.dataArr = menuDataArr
}
}
var didSelectedIndex: ((_ index: Int) -> Void)?
var isDecelerating = false
var isDragging = false
//MARK: UI
private lazy var bgView: UIImageView = {
let imageView = UIImageView(image: UIImage(named: "bg_image_01"))
return imageView
}()
private lazy var closeButton: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "close_icon_01"), for: .normal)
button.addTarget(self, action: #selector(handleCloseButton), for: .touchUpInside)
return button
}()
private lazy var coverImageView: VPImageView = {
let imageView = VPImageView()
imageView.layer.cornerRadius = 6
imageView.layer.masksToBounds = true
return imageView
}()
private lazy var videoNameLabel: UILabel = {
let label = UILabel()
label.font = .fontMedium(ofSize: 15)
label.textColor = .colorFFFFFF()
label.numberOfLines = 2
return label
}()
private lazy var tagView: JXButton = {
let view = JXButton(type: .custom)
view.isUserInteractionEnabled = false
view.backgroundColor = .colorFFFFFF(alpha: 0.1)
view.leftAndRightMargin = 6
view.layer.cornerRadius = 3
view.layer.masksToBounds = true
view.jx_font = .fontRegular(ofSize: 12)
view.setTitleColor(.colorAFAFAF(), for: .normal)
return view
}()
private lazy var desLabel: UILabel = {
let label = UILabel()
label.font = .fontRegular(ofSize: 12)
label.textColor = .colorFFFFFF(alpha: 0.6)
label.numberOfLines = 3
return label
}()
private lazy var lineView: UIView = {
let view = UIView()
view.backgroundColor = .color545458(alpha: 0.45)
return view
}()
private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
let itemWidth = floor((UIScreen.width - 8 * 4 - 30) / 5)
let layout = UICollectionViewFlowLayout()
layout.itemSize = .init(width: itemWidth, height: 54)
layout.minimumLineSpacing = 9
layout.minimumInteritemSpacing = 8
layout.sectionInset = .init(top: 0, left: 15, bottom: 0, right: 15)
return layout
}()
private lazy var collectionView: VPCollectionView = {
let collectionView = VPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.contentInset = .init(top: 0, left: 0, bottom: UIScreen.tabbarSafeBottomMargin, right: 0)
collectionView.register(VPEpisodeCell.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
private lazy var menuView: VPEpisodeMenuView = {
let view = VPEpisodeMenuView()
view.didSelectedIndex = { [weak self] index in
guard let self = self else { return }
var row = 0
if index > 0 {
row = index * 30 + 10
let count = self.dataArr.count
if row >= count {
row = count - 1
}
}
let indexPath = IndexPath.init(row: row, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
}
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
vp_setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc private func handleCloseButton() {
self.dismiss(animated: true) {
}
}
//MARK: HWPanModalPresentable
override func panScrollable() -> UIScrollView? {
return collectionView
}
override func longFormHeight() -> PanModalHeight {
return PanModalHeightMake(.content, UIScreen.height * (2 / 3))
}
override func showDragIndicator() -> Bool {
return false
}
override func backgroundConfig() -> HWBackgroundConfig {
let config = HWBackgroundConfig()
config.backgroundAlpha = 0.6
return config
}
}
extension VPEpisodeView {
private func vp_setupUI() {
addSubview(bgView)
addSubview(closeButton)
addSubview(coverImageView)
addSubview(videoNameLabel)
addSubview(tagView)
addSubview(desLabel)
addSubview(lineView)
addSubview(menuView)
addSubview(collectionView)
bgView.snp.makeConstraints { make in
make.left.right.top.equalToSuperview()
}
closeButton.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-5)
make.top.equalToSuperview().offset(5)
make.width.equalTo(40)
make.height.equalTo(40)
}
coverImageView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.top.equalToSuperview().offset(45)
make.width.equalTo(64)
make.height.equalTo(82)
}
videoNameLabel.snp.makeConstraints { make in
make.left.equalTo(coverImageView.snp.right).offset(10)
make.centerY.equalTo(coverImageView.snp.top).offset(20)
make.right.lessThanOrEqualToSuperview().offset(-15)
}
tagView.snp.makeConstraints { make in
make.left.equalTo(videoNameLabel)
make.bottom.equalTo(coverImageView).offset(-12)
}
desLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.right.lessThanOrEqualToSuperview().offset(-15)
make.top.equalTo(coverImageView.snp.bottom).offset(10)
}
lineView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(15)
make.centerX.equalToSuperview()
make.top.equalTo(desLabel.snp.bottom).offset(46)
make.height.equalTo(1)
}
menuView.snp.makeConstraints { make in
make.left.right.equalTo(self.lineView)
make.bottom.equalTo(self.lineView).offset(0.5)
}
collectionView.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.bottom.equalToSuperview()
make.top.equalTo(lineView.snp.bottom).offset(14)
}
}
}
//MARK: -------------- UICollectionViewDelegate UICollectionViewDataSource --------------
extension VPEpisodeView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! VPEpisodeCell
cell.videoInfoModel = self.dataArr[indexPath.row]
cell.vp_isSelected = indexPath.row == currentIndex
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataArr.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard indexPath.row != currentIndex else { return }
var lastIndex = indexPath.row - 1
var lastIsLock = false
if lastIndex > 0 && lastIndex < self.dataArr.count {
let lastModel = self.dataArr[lastIndex]
lastIsLock = lastModel.is_lock ?? false
}
if lastIsLock {
VPToast.show(text: "veloria_jump_unlock_error".localized)
return
}
self.didSelectedIndex?(indexPath.row)
self.dismiss(animated: true) {
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if isDragging || isDecelerating {
updateMuneSelectedIndex()
}
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
isDecelerating = false
updateMuneSelectedIndex()
}
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
isDecelerating = true
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isDragging = true
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
isDragging = false
}
func updateMuneSelectedIndex() {
let indexPathArr = collectionView.indexPathsForVisibleItems
var minRow = dataArr.count - 1
var maxRow = 0
for indexPath in indexPathArr {
if indexPath.row < minRow {
minRow = indexPath.row
}
if indexPath.row > maxRow {
maxRow = indexPath.row
}
}
let selectedIndex = maxRow / 30
if menuView.selectedIndex != selectedIndex {
menuView.selectedIndex = selectedIndex
}
}
}