BeeReel/BeeReel/Class/Player/Controller/BRPlayerListViewController.swift
2025-07-24 14:21:59 +08:00

391 lines
13 KiB
Swift

//
// BRPlayerListViewController.swift
// BeeReel
//
// Created by 鸿 on 2025/6/24.
//
import UIKit
import SJMediaCacheServer
@objc protocol BRPlayerListViewControllerDelegate {
///
@objc optional func br_playerViewControllerShouldLoadMoreData(playerViewController: BRPlayerListViewController) -> Bool
///
@objc optional func br_playerViewControllerLoadMoreData(playerViewController: BRPlayerListViewController)
///
@objc optional func br_playerListViewController(_ viewController: BRPlayerListViewController, didChangeIndexPathForVisible indexPath: IndexPath)
///
@objc optional func br_shouldAutoScrollNextEpisode(_ viewController: BRPlayerListViewController) -> Bool
}
@objc protocol BRPlayerListViewControllerDataSource {
func br_playerListViewController(_ viewController: BRPlayerListViewController, _ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
func br_playerListViewController(_ viewController: BRPlayerListViewController, _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
@objc optional func br_numberOfSections(in viewController: BRPlayerListViewController) -> Int
}
class BRPlayerListViewController: BRViewController {
var contentSize: CGSize {
return CGSize(width: UIScreen.width, height: UIScreen.height - UIScreen.customTabBarHeight)
}
var CellClass: BRPlayerListCell.Type {
return BRPlayerListCell.self
}
private(set) lazy var viewModel: BRPlayerViewModel = {
let vm = BRPlayerViewModel()
vm.delegate = self
return vm
}()
weak var delegate: BRPlayerListViewControllerDelegate?
weak var dataSource: BRPlayerListViewControllerDataSource?
///
private var prePrefetchTask: MCSPrefetchTask?
private var nextPrefetchTask: MCSPrefetchTask?
private lazy var collectionViewLayout: UICollectionViewLayout = {
let layout = UICollectionViewFlowLayout()
layout.itemSize = contentSize
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
return layout
}()
private(set) lazy var collectionView: BRCollectionView = {
let collectionView = BRCollectionView(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
collectionView.register(CellClass.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
deinit {
self.prePrefetchTask?.cancel()
self.nextPrefetchTask?.cancel()
self.collectionView.removeFromSuperview()
}
override func viewDidLoad() {
super.viewDidLoad()
self.statusBarStyle = .lightContent
self.view.backgroundColor = .color1C1C1C()
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActiveNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(willResignActiveNotification), name: UIApplication.willResignActiveNotification, object: nil)
br_setupUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if self.viewModel.isPlaying {
self.viewModel.currentPlayer?.start()
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.viewModel.currentPlayer?.pause()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
guard let self = self else { return }
self.scrollDidEnd(self.collectionView)
}
}
///
var previousVideoUrl: String? { return nil }
///
var nextVideoUrl: String? { return nil }
func play() {
if self.isDidAppear {
self.viewModel.currentPlayer?.start()
}
self.viewModel.isPlaying = true
if (self.collectionView.contentSize.height - self.collectionView.contentOffset.y) / self.contentSize.height <= 3 {
self.loadMoreData()
}
}
func pause() {
self.viewModel.currentPlayer?.pause()
self.viewModel.isPlaying = false
}
func reloadData(completion: (() -> Void)? = nil) {
UIView.performWithoutAnimation {
self.collectionView.reloadData()
}
self.collectionView.performBatchUpdates(nil) { [weak self] finish in
guard let self = self else { return }
let cell = self.collectionView.cellForItem(at: viewModel.currentIndexPath) as? BRPlayerProtocol
self.viewModel.currentPlayer = cell
completion?()
}
}
func scrollToItem(indexPath: IndexPath, animated: Bool = true, completer: (() -> Void)? = nil) {
UIView.performWithoutAnimation {
self.collectionView.scrollToItem(at: indexPath, at: .top, animated: animated);
}
self.collectionView.performBatchUpdates(nil) { [weak self] _ in
guard let self = self else { return }
if !animated {
if viewModel.currentIndexPath != indexPath {
self.skip(indexPath: indexPath)
} else {
self.play()
}
}
completer?()
}
}
func clearData() {
self.viewModel.currentPlayer = nil
self.viewModel.currentIndexPath = .init(row: 0, section: 0)
self.collectionView.contentOffset = .init(x: 0, y: 0)
self.collectionView.reloadData()
}
}
extension BRPlayerListViewController {
private func br_setupUI() {
view.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.top.equalToSuperview()
make.left.equalToSuperview()
make.width.equalTo(self.contentSize.width)
make.height.equalTo(self.contentSize.height)
}
}
}
//MARK: -------------- UICollectionViewDelegate UICollectionViewDataSource --------------
extension BRPlayerListViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell: UICollectionViewCell
if let newCell = self.dataSource?.br_playerListViewController(self, collectionView, cellForItemAt: indexPath) {
cell = newCell
} else {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
}
if let cell = cell as? BRPlayerListCell {
if cell.viewModel == nil {
cell.viewModel = viewModel
}
}
if self.viewModel.currentPlayer == nil, indexPath == viewModel.currentIndexPath, let playerProtocol = cell as? BRPlayerProtocol {
viewModel.currentIndexPath = indexPath
self.viewModel.currentPlayer = playerProtocol
didChangeIndexPathForVisible()
}
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
self.prePrefetchTask?.cancel()
self.nextPrefetchTask?.cancel()
self.prePrefetchTask = self.prefetchTask(url: self.previousVideoUrl)
self.nextPrefetchTask = self.prefetchTask(url: self.nextVideoUrl)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = self.dataSource?.br_playerListViewController(self, collectionView, numberOfItemsInSection: section) {
return count
} else {
return 0
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if let num = self.dataSource?.br_numberOfSections?(in: self) {
return num
} else {
return 1
}
}
//
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollDidEnd(scrollView)
brLog(message: "scrollViewDidEndDecelerating+++++++++++++++")
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
scrollDidEnd(scrollView)
brLog(message: "scrollViewDidEndScrollingAnimation+++++++++++++++")
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
// if !decelerate {
// scrollDidEnd(scrollView)
// }
brLog(message: "scrollViewDidEndDragging+++++++++++++++\(decelerate)")
}
private func scrollDidEnd(_ 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 floor(offsetY) == floor(cell.frame.origin.y) {
if viewModel.currentIndexPath != indexPath {
self.skip(indexPath: indexPath)
}
}
}
}
private func skip(indexPath: IndexPath) {
guard let currentPlayer = self.collectionView.cellForItem(at: indexPath) as? BRPlayerProtocol else { return }
viewModel.currentIndexPath = indexPath
self.viewModel.currentPlayer = currentPlayer
didChangeIndexPathForVisible()
self.play()
}
}
//MARK: -------------- BRPlayerViewModelDelegate --------------
extension BRPlayerListViewController: BRPlayerViewModelDelegate {
func br_currentVideoPlayFinish(viewModel: BRPlayerViewModel) {
var isScroll = true
if let result = self.delegate?.br_shouldAutoScrollNextEpisode?(self) {
isScroll = result
}
if isScroll {
scrollToNextEpisode()
} else {
viewModel.currentPlayer?.replay()
}
}
func br_switchPlayAndPause(viewModel: BRPlayerViewModel) {
if self.viewModel.isPlaying {
self.pause()
} else {
self.play()
}
}
func br_onEpisodeView(viewModel: BRPlayerViewModel) {
}
func br_clickRateButton(viewModel: BRPlayerViewModel) {
}
}
extension BRPlayerListViewController {
///
private func scrollToNextEpisode() {
var contentOffset = self.collectionView.contentOffset
if hasNextEpisode() {
contentOffset.y = floor(contentOffset.y + self.contentSize.height)
self.collectionView.setContentOffset(contentOffset, animated: true)
} else {
self.viewModel.currentPlayer?.replay()
}
}
///
private func hasNextEpisode() -> Bool {
let contentOffset = self.collectionView.contentOffset
let contentSize = self.collectionView.contentSize
if contentOffset.y >= contentSize.height - self.contentSize.height {
return false
}
return true
}
}
extension BRPlayerListViewController {
private func loadMoreData() {
let isLoad = self.delegate?.br_playerViewControllerShouldLoadMoreData?(playerViewController: self)
if isLoad != false {
self.delegate?.br_playerViewControllerLoadMoreData?(playerViewController: self)
}
}
private func didChangeIndexPathForVisible() {
self.delegate?.br_playerListViewController?(self, didChangeIndexPathForVisible: viewModel.currentIndexPath)
}
}
//MARK: -------------- --------------
extension BRPlayerListViewController {
private func prefetchTask(url: String?) -> MCSPrefetchTask? {
return BRPlayerCache.prefetch(urlString: url)
}
}
//MARK: -------------- --------------
extension BRPlayerListViewController {
@objc func didBecomeActiveNotification() {
if self.viewModel.isPlaying && isDidAppear {
self.viewModel.currentPlayer?.start()
}
}
@objc func willResignActiveNotification() {
self.viewModel.currentPlayer?.pause()
}
}