CommunityVideoListController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. import Kingfisher
  11. public enum DepartureVCType{
  12. case personal // 个人中心
  13. case hotVideos // 热门视频
  14. case others
  15. }
  16. class CommunityVideoListController: BaseViewController {
  17. override func didReceiveMemoryWarning() {
  18. super.didReceiveMemoryWarning()
  19. KingfisherManager.shared.cache.clearDiskCache()
  20. KingfisherManager.shared.cache.clearMemoryCache()
  21. }
  22. // 内容Id
  23. var contentId: Int?
  24. // 来源Vc
  25. var departureVc : DepartureVCType? {
  26. didSet {
  27. if departureVc == .personal {
  28. paraType = "one"
  29. } else if departureVc == .hotVideos {
  30. paraType = "hot"
  31. }
  32. }
  33. }
  34. var paraType: String?
  35. // 单个话题id
  36. var topicId: Int?
  37. var videoItemList : Array<CommunityVideoItemModel>?
  38. var commentAlertSheetView : CommunityAllCommentView?
  39. private var prePlayCell: CommunityVideoCoverCollectionCell?
  40. deinit {
  41. if observe != nil {
  42. NotificationCenter.default.removeObserver(observe!)
  43. }
  44. }
  45. weak var observe : NSObjectProtocol?
  46. override var prefersStatusBarHidden: Bool {
  47. return true
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. setupViews()
  52. setupData()
  53. setUpAppStatusNotification()
  54. statusBarStyle = .lightContent
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. UIApplication.shared.isIdleTimerDisabled = true
  58. }
  59. override func viewDidAppear(_ animated: Bool) {
  60. playFirstVideoWhenViewDidAppeared()
  61. }
  62. override func viewWillDisappear(_ animated: Bool) {
  63. pauseWillDisappear()
  64. UIApplication.shared.isIdleTimerDisabled = false
  65. }
  66. func setUpAppStatusNotification() {
  67. observe = NotificationCenter.default.addObserver(forName: UIApplication.willResignActiveNotification, object: nil, queue: OperationQueue.main, using: {
  68. [weak self] (notification) in
  69. self?.pauseCurrentVideo()
  70. })
  71. observe = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
  72. [weak self] (notification) in
  73. if self?.isViewLoaded ?? true && (self?.view.window) != nil {
  74. self?.startPlay(self?.prePlayCell)
  75. }
  76. })
  77. observe = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: OperationQueue.main, using: {
  78. [weak self] (notification) in
  79. if self?.isViewLoaded ?? true && (self?.view.window) != nil {
  80. self?.startPlay(self?.prePlayCell)
  81. }
  82. })
  83. // 子评论页面
  84. observe = NotificationCenter.default.addObserver(forName: NSNotification.Name("JumpToSubCommentController"), object: nil, queue: OperationQueue.main) {
  85. [weak self] (notification) in
  86. let paraDic = notification.object as! [String:Any]
  87. let vc = CommunityVideoSubCommentController()
  88. vc.communityPostCommentModel = paraDic["commmentModel"] as? CommunityPostCommentModel
  89. vc.communityVideoItemModel = paraDic["detailModel"] as? CommunityVideoItemModel
  90. self?.navigationController?.pushViewController(vc, animated: true)
  91. self?.commentAlertSheetView?.hide()
  92. }
  93. }
  94. override func setupViews() {
  95. navigationBar.isHidden = true
  96. view.backgroundColor = UIColor.black
  97. self.view.addSubview(collectionView)
  98. view.addSubview(blurEffectView)
  99. view.addSubview(placeLabel)
  100. view.addSubview(navBackBtn)
  101. collectionView.snp.makeConstraints { (make) in
  102. make.left.top.right.bottom.equalToSuperview()
  103. }
  104. navBackBtn.snp.makeConstraints { (make) in
  105. make.left.equalTo(5)
  106. make.top.equalTo(kSafeStatusBarHeight+2)
  107. make.size.equalTo(40)
  108. }
  109. placeLabel.snp.makeConstraints { (make) in
  110. make.center.equalToSuperview()
  111. make.width.equalTo(120)
  112. make.height.equalTo(60)
  113. }
  114. // 个人中心页进入,不可滑动
  115. if departureVc == .personal {
  116. collectionView.isScrollEnabled = false
  117. }
  118. }
  119. override func setupData() {
  120. navBackBtn.rx.tap.subscribe(onNext: {
  121. [weak self] (data) in
  122. self?.navigationController?.popViewController(animated: true)
  123. }).disposed(by: disposeBag)
  124. collectionView.addHeader(withBeginRefresh: true, animation: false) {
  125. [weak self] (page) in
  126. self?.communityVideoListApi(page:page)
  127. self?.collectionView.mj_header?.isHidden = true
  128. }
  129. collectionView.addPreloadingAutoNormalFooter(withAutomaticallyRefresh: true) {
  130. [weak self] (page) in
  131. self?.communityVideoListApi(page:page)
  132. }
  133. }
  134. private lazy var navBackBtn: UIButton = {
  135. let navBackBtn = UIButton(type: UIButton.ButtonType.custom)
  136. navBackBtn.setImage(kImage(name: "navbar_back_white"), for: UIControl.State.normal)
  137. navBackBtn.isHidden = true
  138. return navBackBtn
  139. }()
  140. private lazy var collectionView: UICollectionView = {
  141. [unowned self] in
  142. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  143. collectionView.backgroundColor = UIColor.black
  144. collectionView.delegate = self;
  145. collectionView.dataSource = self;
  146. collectionView.isPagingEnabled = true
  147. collectionView.showsVerticalScrollIndicator = false
  148. collectionView.showsHorizontalScrollIndicator = false
  149. return collectionView
  150. }()
  151. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  152. let collectionViewLayout = UICollectionViewFlowLayout.init()
  153. collectionViewLayout.minimumLineSpacing = 0
  154. collectionViewLayout.minimumInteritemSpacing = 0
  155. return collectionViewLayout
  156. }()
  157. private lazy var placeLabel: UILabel = {
  158. let placeLabel = UILabel()
  159. placeLabel.text = "视频不见了"
  160. placeLabel.textColor = UIColor.white
  161. placeLabel.textAlignment = .center
  162. placeLabel.cornerRadius = 8
  163. placeLabel.masksToBounds = true
  164. placeLabel.backgroundColor = UIColor(hexString: "000000", alpha: 0.5)
  165. placeLabel.font = kRegularFont15
  166. placeLabel.isHidden = true
  167. return placeLabel
  168. }()
  169. lazy var blurEffectView: UIVisualEffectView = {
  170. let blurEffect = UIBlurEffect(style: .dark)
  171. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  172. blurEffectView.frame = CGRect(x: CGFloat(0), y: 0, width: kScreenWidth, height: kScreenHeight)
  173. blurEffectView.isHidden = true
  174. return blurEffectView
  175. }()
  176. }
  177. // MARK: - collectionView dataSource && delegate
  178. extension CommunityVideoListController: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
  179. func numberOfSections(in collectionView: UICollectionView) -> Int {
  180. return 1
  181. }
  182. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  183. return videoItemList?.count ?? 0
  184. }
  185. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  186. let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
  187. if cell != nil {
  188. collectionView.reloadItems(at: [indexPath])
  189. }
  190. // 记录阅读行为
  191. readVirueRecordAddApi(indexPath.row)
  192. }
  193. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  194. let cell = CommunityVideoCoverCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  195. cell.videoItemMdl = videoItemList?[indexPath.row]
  196. cell.backClosure = {
  197. [weak self] in
  198. self?.navigationController?.popViewController(animated: true)
  199. }
  200. cell.shareClosure = {
  201. [weak self] (videoItemMdl) in
  202. self?.share(videoItemMdl,row:indexPath.row)
  203. }
  204. cell.followClosure = {
  205. [weak self] (videoItemMdl, followButton) in
  206. CommunityFollowUserViewModel.shared.follow(communityVideoItemModel: videoItemMdl,videoItemList:(self?.videoItemList)!, button: followButton)
  207. }
  208. cell.buttonClickClosure = {
  209. [weak self] (clickType, uid, postId,isShow) in
  210. switch clickType {
  211. case videoBtnClickType.typeComment:
  212. self?.showCommentView(postId: postId, videoItemMdl: (self?.videoItemList?[indexPath.row])!, isShow: isShow)
  213. case videoBtnClickType.typeLike:
  214. VirusViewModel.shared.praise(communityVideoItemModel: (self?.videoItemList?[indexPath.row])!)
  215. case videoBtnClickType.typeCollect:
  216. VirusViewModel.shared.collection(communityVideoItemModel: (self?.videoItemList?[indexPath.row])!)
  217. case videoBtnClickType.typePerson:
  218. if uid != UserModel.shared().getModel()?.uid {
  219. let vc = OtherPersonalCenterViewController()
  220. vc.uid = uid
  221. self?.navigationController?.pushViewController(vc, animated: true)
  222. }
  223. }
  224. }
  225. return cell
  226. }
  227. // func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  228. // let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
  229. // if cell != nil {
  230. // if (cell?.videoPlayView.pauseFlag)! {
  231. // cell?.play()
  232. // } else {
  233. // cell?.pause()
  234. // }
  235. // }
  236. // }
  237. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  238. return CGSize(width:kScreenWidth, height:kScreenHeight)
  239. }
  240. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  241. return UIEdgeInsets(top:0, left: 0, bottom: 0, right: 0)
  242. }
  243. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
  244. return CGSize(width: kScreenWidth, height: 0)
  245. }
  246. func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  247. // 上一个collectionItem滑出,停止播放上面的视频
  248. let preCell = cell as? CommunityVideoCoverCollectionCell
  249. preCell?.pause()
  250. preCell?.playStatusImageView.isHidden = true
  251. }
  252. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  253. // 滑动完毕,播放当前cell的video
  254. let index = Int(collectionView.contentOffset.y / CGFloat(kScreenHeight))
  255. let indexPath = IndexPath(item: index, section: 0)
  256. let cell = collectionView.cellForItem(at: indexPath) as? CommunityVideoCoverCollectionCell
  257. if(cell != nil) {
  258. self.startPlay(cell)
  259. self.prePlayCell = cell
  260. }
  261. }
  262. }
  263. // MARK: - Logic
  264. extension CommunityVideoListController {
  265. func startPlay(_ cell: CommunityVideoCoverCollectionCell?) {
  266. cell?.play()
  267. }
  268. func pauseCurrentVideo() {
  269. if prePlayCell != nil {
  270. prePlayCell?.pause()
  271. }
  272. }
  273. func pauseWillDisappear() {
  274. if prePlayCell != nil {
  275. prePlayCell?.pauseWillDisappear()
  276. }
  277. }
  278. func playFirstVideoWhenViewDidAppeared() {
  279. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.4 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { () -> Void in
  280. let showCells = self.collectionView.visibleCells
  281. for tmpCell in showCells {
  282. let videoCell = tmpCell as? CommunityVideoCoverCollectionCell
  283. self.startPlay(videoCell)
  284. self.prePlayCell = videoCell
  285. break
  286. }
  287. }
  288. }
  289. func showCommentView(postId: Int, videoItemMdl: CommunityVideoItemModel,isShow:Bool) {
  290. commentAlertSheetView = CommunityAllCommentView.communityAllCommentView(view:self.view,postId: postId, videoItemMdl: videoItemMdl,isShow:isShow)
  291. }
  292. /// 分享
  293. func share(_ videoItemModel: CommunityVideoItemModel,row:Int) {
  294. var title = videoItemModel.title
  295. if title == nil || title == "" {
  296. title = videoItemModel.content
  297. }
  298. let shareCommunityView = ShareCommunityView.shareCommunityView(
  299. postId: videoItemModel.id ?? 0, uid: videoItemModel.uid ?? 0,
  300. h5Str: (videoItemModel.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")"),
  301. thumbnailImg: UIImage.imageUrl(videoItemModel.img),
  302. sharedTitle: String(title ?? ""),
  303. sharedDetailTitle: kCommunityPostShareDetailTitle,
  304. shareCommunityViewType: ShareCommunityViewType.postDetail,
  305. completion: {
  306. VirusViewModel.shared.forwarVirueRecordAddApi(postId: videoItemModel.id, postAuthorUid: "\(videoItemModel.uid ?? 0)", title: videoItemModel.title, content: videoItemModel.content, postType: videoItemModel.type, postCover: videoItemModel.img)
  307. })
  308. shareCommunityView.saveCompletion = {
  309. [weak self] in
  310. let vc = CommunityShareContentViewController()
  311. vc.imgUrl = videoItemModel.img
  312. let communityPostDetailTopicModel = videoItemModel.topic?[0]
  313. vc.topicStr = communityPostDetailTopicModel?.name ?? ""
  314. if videoItemModel.title == "" || videoItemModel.title == nil {
  315. vc.titleStr =
  316. String(videoItemModel.content?.prefix(20) ?? "")
  317. }else {
  318. vc.titleStr = String(videoItemModel.title?.prefix(20) ?? "")
  319. }
  320. vc.avatarStr = videoItemModel.avatar
  321. vc.nameStr = videoItemModel.username
  322. vc.H5UrlStr = (videoItemModel.h5url ?? "" + "\(UserModel.shared().getModel()?.inviteCode ?? "")")
  323. vc.bean = videoItemModel.willCollectBean
  324. vc.postId = videoItemModel.id
  325. vc.uid = videoItemModel.uid
  326. vc.type = videoItemModel.type
  327. vc.contentStr = videoItemModel.content
  328. self?.navigationController?.pushViewController(vc, animated: true)
  329. }
  330. shareCommunityView.deleteCompletion = {
  331. [weak self] in
  332. self?.communityDeleteApi(postId: videoItemModel.id ?? 0,row:row)
  333. }
  334. }
  335. }
  336. // MARK: - Networking
  337. extension CommunityVideoListController {
  338. /// 视频列表
  339. func communityVideoListApi(page:Int = 1) {
  340. SwiftMoyaNetWorkServiceCommunity.shared().communityVideoListApi(id: contentId ?? 0, type: paraType ?? "", topicId: topicId ?? 0, page: page, completion: {
  341. [weak self] (communityVideoListModel) -> (Void) in
  342. let videoListMdl = communityVideoListModel as? CommunityVideoListModel
  343. if videoListMdl?.pagination?.currentPage == 1{
  344. self?.videoItemList?.removeAll()
  345. self?.collectionView.resetNoMoreData()
  346. }
  347. if self?.videoItemList?.isEmpty ?? true {
  348. self?.videoItemList = videoListMdl?.data
  349. }else {
  350. self?.videoItemList = (self?.videoItemList)! + (videoListMdl?.data)!
  351. }
  352. if self?.videoItemList?.isEmpty ?? true {
  353. self?.navBackBtn.isHidden = false
  354. self?.blurEffectView.isHidden = false
  355. self?.placeLabel.isHidden = false
  356. }else {
  357. self?.navBackBtn.isHidden = true
  358. self?.blurEffectView.isHidden = true
  359. self?.placeLabel.isHidden = true
  360. }
  361. self?.collectionView.reloadData()
  362. MJRefreshManager.mjRefreshManagerHiddenFooter(collectionView: self?.collectionView, pagination: videoListMdl?.pagination)
  363. }) { [weak self] loadingStatus in
  364. self?.navBackBtn.isHidden = false
  365. self?.blurEffectView.isHidden = false
  366. self?.placeLabel.isHidden = false
  367. }
  368. }
  369. /// 阅读virue
  370. func readVirueRecordAddApi(_ indexRow: Int) {
  371. let videoItemMdl = self.videoItemList?[indexRow]
  372. var postDesc : String?
  373. if videoItemMdl?.title == nil || videoItemMdl?.title == "" {
  374. postDesc = "\(videoItemMdl?.content?.prefix(20) ?? "")"
  375. }else {
  376. postDesc = videoItemMdl?.title
  377. }
  378. VirusViewModel.shared.readVirueRecordAddApi(postId: videoItemMdl?.id, postAuthorUid: "\(videoItemMdl?.uid ?? 0)", postDesc: postDesc, postType: videoItemMdl?.type, postCover: videoItemMdl?.img, actionId: "\(videoItemMdl?.id ?? 0)")
  379. }
  380. /// 删除帖子
  381. func communityDeleteApi(postId:Int,row:Int) {
  382. SwiftMoyaNetWorkServiceCommunity.shared().communityDeleteApi(postId: postId) { [weak self] (data) -> (Void) in
  383. self?.navigationController?.popViewController(animated: true)
  384. }
  385. }
  386. }