// // CommunityVideoListController.swift // RainbowPlanet // // Created by Christopher on 2019/7/3. // Copyright © 2019 RainbowPlanet. All rights reserved. // 推荐--视频内容详情 import UIKit import RxSwift class CommunityVideoListController: BaseViewController { // 内容Id var contentId: Int? { didSet { setupData() } } var videoItemList: Array? private var prePlayCell: CommunityVideoCoverCollectionCell? deinit { if observe != nil { NotificationCenter.default.removeObserver(observe!) } } weak var observe : NSObjectProtocol? override var prefersStatusBarHidden: Bool { return true } override func viewDidLoad() { super.viewDidLoad() setupViews() setUpAppStatusNotification() } override func viewDidAppear(_ animated: Bool) { playFirstVideoWhenViewDidAppeared() } override func viewWillDisappear(_ animated: Bool) { pauseCurrentVideo() } func setUpAppStatusNotification() { observe = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main, using: { [weak self] (notification) in self?.pauseCurrentVideo() }) observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: { [weak self] (notification) in self?.startPlay(self?.prePlayCell) }) observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main, using: { [weak self] (notification) in self?.startPlay(self?.prePlayCell) }) } override func setupViews() { self.view.addSubview(collectionView) let emptyView = DIYEmptyView.empty(with: kImage(name: "default_page_data"), titleStr: nil, detailStr: "当前暂无数据") emptyView!.contentViewY = kScaleValue(value: 182) collectionView.ly_emptyView = emptyView collectionView.ly_startLoading() collectionView.snp.makeConstraints { (make) in make.top.equalTo(0) make.left.right.bottom.equalToSuperview() } } override func setupData() { collectionView.addHeaderWithHeader(withBeginRefresh: true, animation: false) { [weak self] (page) in self?.communityVideoListApi() } } /// 视频列表 func communityVideoListApi() { SwiftMoyaNetWorkServiceCommunity.shared().communityVideoListApi(id: contentId ?? 0) { [weak self] (communityVideoListModel) -> (Void) in let videoListMdl = communityVideoListModel as? CommunityVideoListModel self?.videoItemList = videoListMdl?.data self?.collectionView.reloadData() } } private lazy var collectionView: UICollectionView = { let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout) collectionView.backgroundColor = kf7f8faColor collectionView.delegate = self; collectionView.dataSource = self; collectionView.isPagingEnabled = true collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false return collectionView }() private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let collectionViewLayout = UICollectionViewFlowLayout.init() collectionViewLayout.minimumLineSpacing = 0 collectionViewLayout.minimumInteritemSpacing = 0 return collectionViewLayout }() } // MARK: - collectionView dataSource && delegate extension CommunityVideoListController: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource { func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return videoItemList?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = CommunityVideoCoverCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath) cell.videoItemMdl = videoItemList?[indexPath.row] cell.backClosure = { [weak self] in self?.navigationController?.popViewController(animated: true) } cell.shareClosure = { [weak self] in print("------点击了[分享]") } cell.followClosure = { (videoItemMdl, followButton) in CommunityFollowUserViewModel.shared.follow(communityVideoItemModel: videoItemMdl, button: followButton) } cell.buttonClickClosure = { [weak self] (clickType, uid, postId) in switch clickType { case videoBtnClickType.typeComment: self?.showCommentView(postId: postId, videoItemMdl: (self?.videoItemList?[indexPath.row])!) case videoBtnClickType.typeLike: VirusViewModel.shared.praise(communityVideoItemModel: (self?.videoItemList?[indexPath.row])!) case videoBtnClickType.typeCollect: VirusViewModel.shared.collection(communityVideoItemModel: (self?.videoItemList?[indexPath.row])!) case videoBtnClickType.typePerson: let vc = OtherPersonalCenterViewController() vc.uid = uid self?.navigationController?.pushViewController(vc, animated: true) default: break } } return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("----collectionClicked -- \(indexPath.row)") } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width:kScreenWidth, height:kScreenHeight) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top:0, left: 0, bottom: 0, right: 0) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSize(width: kScreenWidth, height: 10) } func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { prePlayCell = cell as? CommunityVideoCoverCollectionCell self.pauseCurrentVideo() } func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { let index = Int(collectionView.contentOffset.y / CGFloat(kScreenHeight)) let indexPath = IndexPath(item: index, section: 0) let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell if(cell != nil) { self.startPlay(cell) } } } // MARK: - extension CommunityVideoListController { func startPlay(_ cell: CommunityVideoCoverCollectionCell?) { cell?.play() } func pauseCurrentVideo() { if prePlayCell != nil { prePlayCell?.pause() } } func playFirstVideoWhenViewDidAppeared() { let showCells = collectionView.visibleCells for tmpCell in showCells { let videoCell = tmpCell as? CommunityVideoCoverCollectionCell startPlay(videoCell) prePlayCell = videoCell break } } func showCommentView(postId: Int, videoItemMdl: CommunityVideoItemModel) { AlertSheetView.commentAlertSheetView(postId: postId, videoItemMdl: videoItemMdl, cancelClosure: { [weak self] in }) { [weak self] (payType) in print("----\(payType)") } } }