178 lines
5.4 KiB
Swift
178 lines
5.4 KiB
Swift
//
|
|
// SPPlayerListViewController.swift
|
|
// ShortPlay
|
|
//
|
|
// Created by 曾觉新 on 2025/4/8.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class SPPlayerListViewController: SPViewController {
|
|
|
|
|
|
var contentSize: CGSize {
|
|
return CGSize(width: kSPScreenWidth, height: kSPScreenHeight - kSPTabBarHeight)
|
|
}
|
|
|
|
|
|
private var dataArr: [Any] = []
|
|
var pagination: SPListPaginationModel?
|
|
|
|
private var viewModel = SPPlayerListViewModel()
|
|
|
|
private(set) var currentIndexPath = IndexPath(row: 0, section: 0)
|
|
|
|
private lazy var collectionViewLayout: UICollectionViewLayout = {
|
|
let layout = UICollectionViewFlowLayout()
|
|
layout.itemSize = contentSize
|
|
layout.minimumInteritemSpacing = 0
|
|
layout.minimumLineSpacing = 0
|
|
return layout
|
|
}()
|
|
|
|
private lazy var collectionView: SPCollectionView = {
|
|
let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.isPagingEnabled = true
|
|
collectionView.showsVerticalScrollIndicator = false
|
|
collectionView.showsHorizontalScrollIndicator = false
|
|
collectionView.bounces = false
|
|
collectionView.scrollsToTop = false
|
|
SPPlayerListCell.registerCell(collectionView: collectionView)
|
|
return collectionView
|
|
}()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
//视频缓存
|
|
do {
|
|
try? KTVHTTPCache.proxyStart()
|
|
}
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActiveNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willResignActiveNotification), name: UIApplication.willResignActiveNotification, object: nil)
|
|
|
|
sp_setupUI()
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
}
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
if !self.dataArr.isEmpty && self.viewModel.isPlaying {
|
|
self.viewModel.currentPlayer?.start()
|
|
}
|
|
}
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
super.viewDidDisappear(animated)
|
|
self.viewModel.currentPlayer?.pause()
|
|
}
|
|
|
|
|
|
func setDataArr(dataArr: [Any]) {
|
|
self.dataArr = dataArr
|
|
|
|
CATransaction.begin()
|
|
self.collectionView.reloadData()
|
|
CATransaction.commit()
|
|
}
|
|
|
|
|
|
func clearDataArr() {
|
|
self.dataArr.removeAll()
|
|
self.viewModel.currentPlayer = nil
|
|
self.collectionView.reloadData()
|
|
}
|
|
|
|
|
|
func play() {
|
|
if self.isDidAppear {
|
|
self.viewModel.currentPlayer?.start()
|
|
}
|
|
|
|
self.viewModel.isPlaying = true
|
|
|
|
if self.dataArr.count - currentIndexPath.row <= 2 {
|
|
// self.loadMoreData()
|
|
}
|
|
|
|
if currentIndexPath.row <= 2 {
|
|
// self.loadUpMoreData()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension SPPlayerListViewController {
|
|
private func sp_setupUI() {
|
|
view.addSubview(collectionView)
|
|
|
|
collectionView.snp.makeConstraints { make in
|
|
make.edges.equalToSuperview()
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource --------------
|
|
extension SPPlayerListViewController: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = SPPlayerListCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath)
|
|
cell.model = dataArr[indexPath.row]
|
|
|
|
if self.viewModel.currentPlayer == nil, indexPath == currentIndexPath {
|
|
self.currentIndexPath = indexPath
|
|
self.viewModel.currentPlayer = cell
|
|
}
|
|
return cell
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return dataArr.count
|
|
}
|
|
|
|
//滑动停止
|
|
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
let offsetY = scrollView.contentOffset.y
|
|
let indexPaths = self.collectionView.indexPathsForVisibleItems
|
|
for indexPath in indexPaths {
|
|
guard let cell = self.collectionView.cellForItem(at: indexPath) else { continue }
|
|
if offsetY == cell.frame.origin.y {
|
|
if self.currentIndexPath != indexPath {
|
|
self.skip(indexPath: indexPath)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func skip(indexPath: IndexPath) {
|
|
currentIndexPath = indexPath
|
|
guard let currentPlayer = self.collectionView.cellForItem(at: indexPath) as? SPPlayerProtocol else { return }
|
|
self.viewModel.currentPlayer = currentPlayer
|
|
// currentCell = self.collectionView.cellForItem(at: indexPath) as? BCListPlayerCell
|
|
self.play()
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: -------------- APP生命周期 --------------
|
|
extension SPPlayerListViewController {
|
|
|
|
@objc func didBecomeActiveNotification() {
|
|
if !self.dataArr.isEmpty && self.viewModel.isPlaying && isDidAppear {
|
|
self.viewModel.currentPlayer?.start()
|
|
}
|
|
}
|
|
|
|
@objc func willResignActiveNotification() {
|
|
self.viewModel.currentPlayer?.pause()
|
|
}
|
|
|
|
|
|
}
|