CommunityVideoListController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // CommunityVideoListController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/7/3.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 推荐--视频内容详情
  8. import UIKit
  9. import RxSwift
  10. class CommunityVideoListController: BaseViewController {
  11. // 内容Id
  12. var contentId: Int? {
  13. didSet {
  14. setupData()
  15. }
  16. }
  17. var videoItemList: Array<CommunityVideoItemModel>?
  18. private var prePlayCell: CommunityVideoCoverCollectionCell?
  19. deinit {
  20. if observe != nil {
  21. NotificationCenter.default.removeObserver(observe!)
  22. }
  23. }
  24. weak var observe : NSObjectProtocol?
  25. override var prefersStatusBarHidden: Bool {
  26. return true
  27. }
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. setupViews()
  31. setUpAppStatusNotification()
  32. }
  33. override func viewDidAppear(_ animated: Bool) {
  34. playFirstVideoWhenViewDidAppeared()
  35. }
  36. override func viewWillDisappear(_ animated: Bool) {
  37. pauseCurrentVideo()
  38. }
  39. func setUpAppStatusNotification() {
  40. observe = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main, using: {
  41. [weak self] (notification) in
  42. self?.pauseCurrentVideo()
  43. })
  44. observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
  45. [weak self] (notification) in
  46. self?.startPlay(self?.prePlayCell)
  47. })
  48. observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main, using: {
  49. [weak self] (notification) in
  50. self?.startPlay(self?.prePlayCell)
  51. })
  52. }
  53. override func setupViews() {
  54. self.view.addSubview(collectionView)
  55. let emptyView = DIYEmptyView.empty(with: kImage(name: "default_page_data"), titleStr: nil, detailStr: "当前暂无数据")
  56. emptyView!.contentViewY = kScaleValue(value: 182)
  57. collectionView.ly_emptyView = emptyView
  58. collectionView.ly_startLoading()
  59. collectionView.snp.makeConstraints { (make) in
  60. make.top.equalTo(0)
  61. make.left.right.bottom.equalToSuperview()
  62. }
  63. }
  64. override func setupData() {
  65. collectionView.addHeaderWithHeader(withBeginRefresh: true, animation: false) { [weak self] (page) in
  66. self?.communityVideoListApi()
  67. }
  68. }
  69. /// 视频列表
  70. func communityVideoListApi() {
  71. SwiftMoyaNetWorkServiceCommunity.shared().communityVideoListApi(id: contentId ?? 0) {
  72. [weak self] (communityVideoListModel) -> (Void) in
  73. let videoListMdl = communityVideoListModel as? CommunityVideoListModel
  74. self?.videoItemList = videoListMdl?.data
  75. self?.collectionView.reloadData()
  76. }
  77. }
  78. private lazy var collectionView: UICollectionView = {
  79. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  80. collectionView.backgroundColor = kf7f8faColor
  81. collectionView.delegate = self;
  82. collectionView.dataSource = self;
  83. collectionView.isPagingEnabled = true
  84. collectionView.showsVerticalScrollIndicator = false
  85. collectionView.showsHorizontalScrollIndicator = false
  86. return collectionView
  87. }()
  88. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  89. let collectionViewLayout = UICollectionViewFlowLayout.init()
  90. collectionViewLayout.minimumLineSpacing = 0
  91. collectionViewLayout.minimumInteritemSpacing = 0
  92. return collectionViewLayout
  93. }()
  94. }
  95. // MARK: - collectionView dataSource && delegate
  96. extension CommunityVideoListController: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  97. func numberOfSections(in collectionView: UICollectionView) -> Int {
  98. return 1
  99. }
  100. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  101. return videoItemList?.count ?? 0
  102. }
  103. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  104. let cell = CommunityVideoCoverCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  105. cell.videoItemMdl = videoItemList?[indexPath.row]
  106. cell.backClosure = {
  107. [weak self] in
  108. self?.navigationController?.popViewController(animated: true)
  109. }
  110. cell.shareClosure = {
  111. [weak self] in
  112. print("------点击了[分享]")
  113. }
  114. cell.followClosure = { (videoItemMdl, followButton) in
  115. CommunityFollowUserViewModel.shared.follow(communityVideoItemModel: videoItemMdl, button: followButton)
  116. }
  117. cell.buttonClickClosure = {
  118. [weak self] (clickType, uid, postId) in
  119. switch clickType {
  120. case videoBtnClickType.typeComment:
  121. self?.showCommentView(postId: postId, videoItemMdl: (self?.videoItemList?[indexPath.row])!)
  122. case videoBtnClickType.typeLike:
  123. VirusViewModel.shared.praise(communityVideoItemModel: (self?.videoItemList?[indexPath.row])!)
  124. case videoBtnClickType.typeCollect: VirusViewModel.shared.collection(communityVideoItemModel: (self?.videoItemList?[indexPath.row])!)
  125. case videoBtnClickType.typePerson:
  126. let vc = OtherPersonalCenterViewController()
  127. vc.uid = uid
  128. self?.navigationController?.pushViewController(vc, animated: true)
  129. default:
  130. break
  131. }
  132. }
  133. return cell
  134. }
  135. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  136. print("----collectionClicked -- \(indexPath.row)")
  137. }
  138. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  139. return CGSize(width:kScreenWidth, height:kScreenHeight)
  140. }
  141. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  142. return UIEdgeInsets(top:0, left: 0, bottom: 0, right: 0)
  143. }
  144. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  145. return CGSize(width: kScreenWidth, height: 10)
  146. }
  147. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  148. prePlayCell = cell as? CommunityVideoCoverCollectionCell
  149. self.pauseCurrentVideo()
  150. }
  151. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  152. let index = Int(collectionView.contentOffset.y / CGFloat(kScreenHeight))
  153. let indexPath = IndexPath(item: index, section: 0)
  154. let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
  155. if(cell != nil) {
  156. self.startPlay(cell)
  157. }
  158. }
  159. }
  160. // MARK: -
  161. extension CommunityVideoListController {
  162. func startPlay(_ cell: CommunityVideoCoverCollectionCell?) {
  163. cell?.play()
  164. }
  165. func pauseCurrentVideo() {
  166. if prePlayCell != nil {
  167. prePlayCell?.pause()
  168. }
  169. }
  170. func playFirstVideoWhenViewDidAppeared() {
  171. let showCells = collectionView.visibleCells
  172. for tmpCell in showCells {
  173. let videoCell = tmpCell as? CommunityVideoCoverCollectionCell
  174. startPlay(videoCell)
  175. prePlayCell = videoCell
  176. break
  177. }
  178. }
  179. func showCommentView(postId: Int, videoItemMdl: CommunityVideoItemModel) {
  180. AlertSheetView.commentAlertSheetView(postId: postId, videoItemMdl: videoItemMdl, cancelClosure: {
  181. [weak self] in
  182. }) {
  183. [weak self] (payType) in
  184. print("----\(payType)")
  185. }
  186. }
  187. }