123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- //
- // CommunityVideoListController.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/7/3.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 推荐--视频内容详情
- import UIKit
- import RxSwift
- import Kingfisher
- public enum DepartureVCType{
- case personal // 个人中心
- case hotVideos // 热门视频
- case others
- }
- class CommunityVideoListController: BaseViewController {
-
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- KingfisherManager.shared.cache.clearDiskCache()
- KingfisherManager.shared.cache.clearMemoryCache()
- }
-
- // 内容Id
- var contentId: Int?
- // 来源Vc
- var departureVc : DepartureVCType? {
- didSet {
- if departureVc == .personal {
- paraType = "one"
- } else if departureVc == .hotVideos {
- paraType = "hot"
- }
- }
- }
- var paraType: String?
- // 单个话题id
- var topicId: Int?
-
- var videoItemList : Array<CommunityVideoItemModel>?
-
- var commentAlertSheetView : CommunityAllCommentView?
-
- 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()
- setupData()
- setUpAppStatusNotification()
- statusBarStyle = .lightContent
- }
-
- override func viewWillAppear(_ animated: Bool) {
- UIApplication.shared.isIdleTimerDisabled = true
- }
-
- override func viewDidAppear(_ animated: Bool) {
- playFirstVideoWhenViewDidAppeared()
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- pauseWillDisappear()
- UIApplication.shared.isIdleTimerDisabled = false
- }
-
- 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
- if self?.isViewLoaded ?? true && (self?.view.window) != nil {
- self?.startPlay(self?.prePlayCell)
- }
- })
-
- observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main, using: {
- [weak self] (notification) in
- if self?.isViewLoaded ?? true && (self?.view.window) != nil {
- self?.startPlay(self?.prePlayCell)
- }
- })
-
- // 子评论页面
- observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("JumpToSubCommentController"), object: nil, queue: OperationQueue.main) {
- [weak self] (notification) in
- let paraDic = notification.object as! [String:Any]
-
- let vc = CommunityVideoSubCommentController()
- vc.communityPostCommentModel = paraDic["commmentModel"] as? CommunityPostCommentModel
- vc.communityVideoItemModel = paraDic["detailModel"] as? CommunityVideoItemModel
- self?.navigationController?.pushViewController(vc, animated: true)
-
- self?.commentAlertSheetView?.hide()
- }
- }
-
- override func setupViews() {
- navigationBar.isHidden = true
- view.backgroundColor = UIColor.black
- self.view.addSubview(collectionView)
- view.addSubview(blurEffectView)
- view.addSubview(placeLabel)
- view.addSubview(navBackBtn)
- collectionView.snp.makeConstraints { (make) in
- make.left.top.right.bottom.equalToSuperview()
- }
- navBackBtn.snp.makeConstraints { (make) in
- make.left.equalTo(5)
- make.top.equalTo(kSafeStatusBarHeight+2)
- make.size.equalTo(40)
- }
- placeLabel.snp.makeConstraints { (make) in
- make.center.equalToSuperview()
- make.width.equalTo(120)
- make.height.equalTo(60)
- }
-
- // 个人中心页进入,不可滑动
- if departureVc == .personal {
- collectionView.isScrollEnabled = false
- }
-
- }
-
- override func setupData() {
- navBackBtn.rx.tap.subscribe(onNext: {
- [weak self] (data) in
- self?.navigationController?.popViewController(animated: true)
- }).disposed(by: disposeBag)
- collectionView.addHeader(withBeginRefresh: true, animation: false) {
- [weak self] (page) in
- self?.communityVideoListApi(page:page)
- self?.collectionView.mj_header?.isHidden = true
- }
- collectionView.addPreloadingAutoNormalFooter(withAutomaticallyRefresh: true) {
- [weak self] (page) in
- self?.communityVideoListApi(page:page)
- }
- }
-
- private lazy var navBackBtn: UIButton = {
- let navBackBtn = UIButton(type: UIButton.ButtonType.custom)
- navBackBtn.setImage(kImage(name: "navbar_back_white"), for: UIControl.State.normal)
- navBackBtn.isHidden = true
- return navBackBtn
- }()
-
- private lazy var collectionView: UICollectionView = {
- [unowned self] in
- let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
- collectionView.backgroundColor = UIColor.black
- 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
- }()
-
- private lazy var placeLabel: UILabel = {
- let placeLabel = UILabel()
- placeLabel.text = "视频不见了"
- placeLabel.textColor = UIColor.white
- placeLabel.textAlignment = .center
- placeLabel.cornerRadius = 8
- placeLabel.masksToBounds = true
- placeLabel.backgroundColor = UIColor(hexString: "000000", alpha: 0.5)
- placeLabel.font = kRegularFont15
- placeLabel.isHidden = true
- return placeLabel
- }()
-
- lazy var blurEffectView: UIVisualEffectView = {
- let blurEffect = UIBlurEffect(style: .dark)
- let blurEffectView = UIVisualEffectView(effect: blurEffect)
- blurEffectView.frame = CGRect(x: CGFloat(0), y: 0, width: kScreenWidth, height: kScreenHeight)
- blurEffectView.isHidden = true
- return blurEffectView
- }()
-
-
- }
- // 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, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
- let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
- if cell != nil {
- collectionView.reloadItems(at: [indexPath])
- }
-
- // 记录阅读行为
- readVirueRecordAddApi(indexPath.row)
- }
-
- 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] (videoItemMdl) in
- self?.share(videoItemMdl,row:indexPath.row)
- }
- cell.followClosure = {
- [weak self] (videoItemMdl, followButton) in
- CommunityFollowUserViewModel.shared.follow(communityVideoItemModel: videoItemMdl,videoItemList:(self?.videoItemList)!, button: followButton)
- }
- cell.buttonClickClosure = {
- [weak self] (clickType, uid, postId,isShow) in
- switch clickType {
- case videoBtnClickType.typeComment:
- self?.showCommentView(postId: postId, videoItemMdl: (self?.videoItemList?[indexPath.row])!, isShow: isShow)
-
- 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:
-
- if uid != UserModel.shared().getModel()?.uid {
- let vc = OtherPersonalCenterViewController()
- vc.uid = uid
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- }
- }
- return cell
- }
-
- // func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- // let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
- // if cell != nil {
- // if (cell?.videoPlayView.pauseFlag)! {
- // cell?.play()
- // } else {
- // cell?.pause()
- // }
- // }
- // }
-
- 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: 0)
- }
-
- func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
- // 上一个collectionItem滑出,停止播放上面的视频
- let preCell = cell as? CommunityVideoCoverCollectionCell
- preCell?.pause()
- preCell?.playStatusImageView.isHidden = true
- }
-
- func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
- // 滑动完毕,播放当前cell的video
- 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)
- self.prePlayCell = cell
- }
- }
-
- }
- // MARK: - Logic
- extension CommunityVideoListController {
-
- func startPlay(_ cell: CommunityVideoCoverCollectionCell?) {
- cell?.play()
- }
-
- func pauseCurrentVideo() {
- if prePlayCell != nil {
- prePlayCell?.pause()
- }
- }
-
- func pauseWillDisappear() {
- if prePlayCell != nil {
- prePlayCell?.pauseWillDisappear()
- }
- }
-
- func playFirstVideoWhenViewDidAppeared() {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.4 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { () -> Void in
- let showCells = self.collectionView.visibleCells
- for tmpCell in showCells {
- let videoCell = tmpCell as? CommunityVideoCoverCollectionCell
- self.startPlay(videoCell)
- self.prePlayCell = videoCell
- break
- }
- }
- }
-
- func showCommentView(postId: Int, videoItemMdl: CommunityVideoItemModel,isShow:Bool) {
- commentAlertSheetView = CommunityAllCommentView.communityAllCommentView(view:self.view,postId: postId, videoItemMdl: videoItemMdl,isShow:isShow)
- }
-
- /// 分享
- func share(_ videoItemModel: CommunityVideoItemModel,row:Int) {
- var title = videoItemModel.title
- if title == nil || title == "" {
- title = videoItemModel.content
- }
- let shareCommunityView = ShareCommunityView.shareCommunityView(
- postId: videoItemModel.id ?? 0, uid: videoItemModel.uid ?? 0,
- h5Str: (videoItemModel.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
- thumbnailImg: UIImage.imageUrl(videoItemModel.img),
- sharedTitle: String(title ?? ""),
- sharedDetailTitle: kCommunityPostShareDetailTitle,
- shareCommunityViewType: ShareCommunityViewType.postDetail,
- completion: {
- VirusViewModel.shared.forwarVirueRecordAddApi(postId: videoItemModel.id, postAuthorUid: "\(videoItemModel.uid ?? 0)", title: videoItemModel.title, content: videoItemModel.content, postType: videoItemModel.type, postCover: videoItemModel.img)
- })
- shareCommunityView.saveCompletion = {
- [weak self] in
- let vc = CommunityShareContentViewController()
- vc.imgUrl = videoItemModel.img
- let communityPostDetailTopicModel = videoItemModel.topic?[0]
- vc.topicStr = communityPostDetailTopicModel?.name ?? ""
- if videoItemModel.title == "" || videoItemModel.title == nil {
- vc.titleStr =
- String(videoItemModel.content?.prefix(20) ?? "")
- }else {
- vc.titleStr = String(videoItemModel.title?.prefix(20) ?? "")
- }
- vc.avatarStr = videoItemModel.avatar
- vc.nameStr = videoItemModel.username
- vc.H5UrlStr = (videoItemModel.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
- vc.bean = videoItemModel.willCollectBean
- vc.postId = videoItemModel.id
- vc.uid = videoItemModel.uid
- vc.type = videoItemModel.type
- vc.contentStr = videoItemModel.content
- self?.navigationController?.pushViewController(vc, animated: true)
- }
-
- shareCommunityView.deleteCompletion = {
- [weak self] in
- self?.communityDeleteApi(postId: videoItemModel.id ?? 0,row:row)
- }
- }
-
- }
- // MARK: - Networking
- extension CommunityVideoListController {
- /// 视频列表
- func communityVideoListApi(page:Int = 1) {
-
- SwiftMoyaNetWorkServiceCommunity.shared().communityVideoListApi(id: contentId ?? 0, type: paraType ?? "", topicId: topicId ?? 0, page: page, completion: {
- [weak self] (communityVideoListModel) -> (Void) in
- let videoListMdl = communityVideoListModel as? CommunityVideoListModel
- if videoListMdl?.pagination?.currentPage == 1{
- self?.videoItemList?.removeAll()
- self?.collectionView.resetNoMoreData()
- }
- if self?.videoItemList?.isEmpty ?? true {
- self?.videoItemList = videoListMdl?.data
- }else {
- self?.videoItemList = (self?.videoItemList)! + (videoListMdl?.data)!
- }
- if self?.videoItemList?.isEmpty ?? true {
- self?.navBackBtn.isHidden = false
- self?.blurEffectView.isHidden = false
- self?.placeLabel.isHidden = false
- }else {
- self?.navBackBtn.isHidden = true
- self?.blurEffectView.isHidden = true
- self?.placeLabel.isHidden = true
- }
- self?.collectionView.reloadData()
- MJRefreshManager.mjRefreshManagerHiddenFooter(collectionView: self?.collectionView, pagination: videoListMdl?.pagination)
- }) { [weak self] loadingStatus in
- self?.navBackBtn.isHidden = false
- self?.blurEffectView.isHidden = false
- self?.placeLabel.isHidden = false
- }
- }
-
- /// 阅读virue
- func readVirueRecordAddApi(_ indexRow: Int) {
- let videoItemMdl = self.videoItemList?[indexRow]
-
- var postDesc : String?
- if videoItemMdl?.title == nil || videoItemMdl?.title == "" {
- postDesc = "\(videoItemMdl?.content?.prefix(20) ?? "")"
- }else {
- postDesc = videoItemMdl?.title
- }
-
- VirusViewModel.shared.readVirueRecordAddApi(postId: videoItemMdl?.id, postAuthorUid: "\(videoItemMdl?.uid ?? 0)", postDesc: postDesc, postType: videoItemMdl?.type, postCover: videoItemMdl?.img, actionId: "\(videoItemMdl?.id ?? 0)")
- }
-
- /// 删除帖子
- func communityDeleteApi(postId:Int,row:Int) {
- SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
- self?.navigationController?.popViewController(animated: true)
- }
- }
- }
|