MVHomeController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. //
  2. // MVHomeController.swift
  3. // MusicVideoPlus
  4. //
  5. // Created by ak on 2021/6/2.
  6. // 首界面
  7. import BFFramework
  8. class MVHomeController: MVBaseController {
  9. var jumpType: Int = 1 // 跳转类型 type: 1-我的作品 2-再创作 3-卡点视频
  10. var jumpVideoData: PQVideoListModel?
  11. var isJumpToLogin: Bool = false
  12. // 视频播放列表
  13. public let margin: CGFloat = cDefaultMargin * 3
  14. public let maxHeight: CGFloat = cScreenHeigth - (cDevice_iPhoneStatusBarHei + 45 + 60 + (cDefaultMargin * 2 + cSafeAreaHeight) + cDefaultMargin * 3)
  15. lazy var viewListView: MVBanner = {
  16. let maxWidth: CGFloat = 9.0 / 16.0 * maxHeight
  17. let margin = (cScreenWidth - maxWidth) / 2
  18. let lineSpacing = margin - cDefaultMargin * 3
  19. let videoListView = MVBanner(frame: .zero, margin: margin, lineSpacing: 15, minScale: MVBanner.Scale(scale: 1))
  20. videoListView.backgroundColor = UIColor.white
  21. videoListView.register(classCellType: MVBannerCell.self)
  22. return videoListView
  23. }()
  24. // 卡点视频 btn
  25. lazy var stuckPointBtn: UIButton = {
  26. let stuckPointBtn = UIButton(type: .custom)
  27. stuckPointBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  28. stuckPointBtn.setImage(UIImage(named: "add"), for: .normal)
  29. stuckPointBtn.adjustsImageWhenHighlighted = false
  30. stuckPointBtn.tag = 2000
  31. stuckPointBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  32. stuckPointBtn.addCorner(corner: 30)
  33. return stuckPointBtn
  34. }()
  35. // 个人中心btn
  36. lazy var mineBtn: UIButton = {
  37. let mineBtn = UIButton(type: .custom)
  38. mineBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  39. mineBtn.setBackgroundImage(UIImage(named: "wode"), for: .normal)
  40. mineBtn.setImage(UIImage(named: "wode"), for: .normal)
  41. mineBtn.adjustsImageWhenHighlighted = false
  42. mineBtn.tag = 1000
  43. mineBtn.addCorner(corner: cDefaultMargin * 3)
  44. return mineBtn
  45. }()
  46. // 设置btn
  47. lazy var settingBtn: UIButton = {
  48. let settingBtn = UIButton(type: .custom)
  49. settingBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  50. settingBtn.setBackgroundImage(UIImage(named: "set"), for: .normal)
  51. settingBtn.adjustsImageWhenHighlighted = false
  52. settingBtn.tag = 3000
  53. return settingBtn
  54. }()
  55. // 标签分类
  56. lazy var categoryCollectionView: UICollectionView = {
  57. let flowLayout = UICollectionViewFlowLayout()
  58. flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 11, bottom: 0, right: 11)
  59. flowLayout.minimumLineSpacing = 0
  60. flowLayout.minimumInteritemSpacing = 0
  61. flowLayout.scrollDirection = .horizontal
  62. let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 45), collectionViewLayout: flowLayout)
  63. collectionView.showsVerticalScrollIndicator = false
  64. collectionView.showsHorizontalScrollIndicator = false
  65. collectionView.delegate = self
  66. collectionView.dataSource = self
  67. collectionView.backgroundColor = UIColor.clear
  68. collectionView.register(MVTagsCell.self, forCellWithReuseIdentifier: String(describing: MVTagsCell.self))
  69. if #available(iOS 11.0, *) {
  70. collectionView.contentInsetAdjustmentBehavior = .never
  71. } else {
  72. automaticallyAdjustsScrollViewInsets = false
  73. }
  74. // 延迟scrollView上子视图的响应,所以当直接拖动UISlider时,如果此时touch时间在150ms以内,UIScrollView会认为是拖动自己,从而拦截了event,导致UISlider接收不到滑动的event
  75. collectionView.delaysContentTouches = false
  76. return collectionView
  77. }()
  78. //网络错误提示
  79. lazy var emptyRemindView: PQEmptyRemindView = {
  80. let emptyRemindView = PQEmptyRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth - 120 - cAKSafeAreaHeight))
  81. emptyRemindView.fullRefreshBloc = { [weak self] _, _ in
  82. self?.getData()
  83. }
  84. return emptyRemindView
  85. }()
  86. var mVideos: Array = Array<MVHotVideoModel>.init()
  87. var mAllVideos: Array = Array<PQVideoListModel>.init()
  88. var lastBnnerSelectIndex: IndexPath = IndexPath(row: 0, section: 0)
  89. // 是否点击的再创作
  90. var isCreateVideo: Bool = false
  91. deinit {
  92. PQNotification.removeObserver(self)
  93. }
  94. // 登录成功后自动跳转到系统相册
  95. @objc func loginSuccesss() {
  96. if isJumpToLogin {
  97. isJumpToLogin = false
  98. PQNotification.removeObserver(self)
  99. // 跳转上传详情页
  100. jumpToDetailVc(type: jumpType, videoData: jumpVideoData)
  101. }
  102. }
  103. @objc func enterBackground() {
  104. BFLog(message: "进入到后台")
  105. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  106. if rootViewController is UINavigationController {
  107. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  108. }
  109. BFLog(message: "rootViewController is \(rootViewController)")
  110. if rootViewController is MVHomeController {
  111. controlPlayrPasueOrResume(isPause: true)
  112. }
  113. }
  114. @objc func willEnterForeground() {
  115. BFLog(message: "进入到前台")
  116. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  117. if rootViewController is UINavigationController {
  118. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  119. }
  120. BFLog(message: "rootViewController is \(rootViewController)")
  121. if rootViewController is MVHomeController {
  122. controlPlayrPasueOrResume(isPause: false)
  123. }
  124. }
  125. override func viewWillAppear(_: Bool) {
  126. super.viewWillAppear(true)
  127. controlPlayrPasueOrResume(isPause: false)
  128. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  129. PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
  130. }
  131. override func viewWillDisappear(_ animated: Bool) {
  132. super.viewWillDisappear(animated)
  133. controlPlayrPasueOrResume(isPause: true)
  134. }
  135. func getData() {
  136. PQLoadingHUB.shared.showHUB(superView:view)
  137. MVHomeViewModel.getHomeHotVideos { [weak self] hotVideos, msg in
  138. if(msg == nil){
  139. BFLog(message: "hotVideos count is :\(hotVideos.count)")
  140. PQLoadingHUB.shared.dismissHUB(superView:(self?.view)!)
  141. self?.showNetworkError(isHidden: true)
  142. self?.refreshData(hotVideos: hotVideos)
  143. }else{
  144. BFLog(message: "请求数据出错 :\(String(describing: msg))")
  145. self?.showNetworkError(isHidden: false)
  146. }
  147. }
  148. }
  149. //显示网络未连接提示
  150. func showNetworkError(isHidden:Bool) {
  151. emptyRemindView.isHidden = isHidden
  152. emptyRemindView.remindLab.textColor = .black
  153. emptyRemindView.remindLab.text = "网络错误"
  154. emptyRemindView.remindLab.font = UIFont.boldSystemFont(ofSize: 20)
  155. emptyRemindView.imageView.image = nil
  156. emptyRemindView.remindSubLab.text = "请检查网络后重试"
  157. emptyRemindView.remindSubLab.isHidden = false
  158. emptyRemindView.refreshBtn.isHidden = false
  159. emptyRemindView.refreshBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  160. emptyRemindView.refreshBtn.setTitle("重试", for: .normal)
  161. if(isHidden){
  162. emptyRemindView.removeFromSuperview()
  163. }else{
  164. view.addSubview(emptyRemindView)
  165. }
  166. }
  167. func refreshData( hotVideos:Array<MVHotVideoModel>) {
  168. mVideos = hotVideos
  169. categoryCollectionView.reloadData()
  170. if mVideos.count > 0 {
  171. mVideos[0].isSelected = true
  172. for hotModel in mVideos {
  173. for video in hotModel.videos {
  174. mAllVideos.append(video)
  175. }
  176. }
  177. BFLog(message: " self?.mAllVideos count is \(String(describing: mAllVideos.count))")
  178. }
  179. viewListView.reloadData()
  180. // 自动播放第一个视频 确保 reloaddata 完成
  181. playVideo(page: 0)
  182. }
  183. override func viewDidLoad() {
  184. super.viewDidLoad()
  185. addNotification(self, selector: #selector(stuckPointDismiss), name: cFinishedPublishedNotiKey, object: nil)
  186. view.addSubview(mineBtn)
  187. view.addSubview(stuckPointBtn)
  188. view.addSubview(settingBtn)
  189. view.addSubview(viewListView)
  190. view.addSubview(categoryCollectionView)
  191. viewListView.dataSource = self
  192. viewListView.delegate = self
  193. viewListView.isShowPageControl = false
  194. viewListView.reloadData()
  195. addLayout()
  196. // 请求用户状态
  197. getUserInfo()
  198. // 网络监听有网后取一次数据
  199. manager?.startListening()
  200. manager?.listener = { [weak self] status in
  201. if status == .reachable(.wwan) || status == .reachable(.ethernetOrWiFi) {
  202. BFLog(message: "来网了")
  203. self?.showNetworkError(isHidden: true)
  204. self?.getData()
  205. } else {
  206. if(MVHotVideoModel.getCacheData().count == 0){
  207. self?.showNetworkError(isHidden: false)
  208. }
  209. }
  210. }
  211. let cacheData = MVHotVideoModel.getCacheData()
  212. if(cacheData.count > 0){
  213. BFLog(message: "有缓存数据 ")
  214. refreshData(hotVideos: cacheData)
  215. }
  216. // 播放器进度和状态回调
  217. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  218. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  219. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  220. cell?.progressView.progress = playProgress / duration
  221. }
  222. // 添加协议
  223. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  224. if showProtocal == nil || showProtocal?.count ?? 0 <= 0 || showProtocal != "1" {
  225. let protocalView: PQServerProtocalView = PQServerProtocalView(frame: view.frame)
  226. protocalView.tag = cProtocalViewTag
  227. protocalView.remindBlock = { [weak self] sender, webUrl in
  228. if sender == nil, webUrl != nil {
  229. let detail = PQBaseWebViewController()
  230. detail.baseUrl = webUrl
  231. self?.navigationController?.pushViewController(detail, animated: true)
  232. }
  233. // 移除继续播放
  234. if sender != nil {
  235. self?.playVideo(page: 0)
  236. }
  237. }
  238. UIApplication.shared.keyWindow?.addSubview(protocalView)
  239. }
  240. }
  241. func addLayout() {
  242. categoryCollectionView.snp.remakeConstraints { make in
  243. make.width.equalTo(cScreenWidth)
  244. make.height.equalTo(45)
  245. make.left.equalToSuperview().offset(0)
  246. make.top.equalToSuperview().offset(cDevice_iPhoneStatusBarHei)
  247. }
  248. stuckPointBtn.snp.remakeConstraints { make in
  249. make.width.height.equalTo(60)
  250. make.centerX.equalToSuperview()
  251. make.bottom.equalToSuperview().offset(-(cDefaultMargin + cAKSafeAreaHeight))
  252. }
  253. mineBtn.snp.remakeConstraints { make in
  254. make.width.height.equalTo(60)
  255. make.left.equalToSuperview().offset(cDefaultMargin * 3)
  256. make.centerY.equalTo(stuckPointBtn)
  257. }
  258. settingBtn.snp.remakeConstraints { make in
  259. make.width.height.equalTo(60)
  260. make.right.equalToSuperview().offset(-cDefaultMargin * 3)
  261. make.centerY.equalTo(stuckPointBtn)
  262. }
  263. viewListView.snp.remakeConstraints { make in
  264. make.left.right.equalToSuperview()
  265. make.top.equalTo(categoryCollectionView.snp_bottom).offset(cDefaultMargin)
  266. make.bottom.equalTo(stuckPointBtn.snp_top).offset(-cDefaultMargin * 2)
  267. }
  268. }
  269. @objc func btnClick(sender: UIButton) {
  270. switch sender.tag {
  271. case 1000: // 个人中心
  272. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_mineTab, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  273. jumpToDetailVc(type: 1)
  274. case 2000: // 系统相册
  275. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusic, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  276. jumpToDetailVc(type: 3)
  277. case 3000: // 设置
  278. navigationController?.pushViewController(MVSettingController(), animated: true)
  279. default:
  280. break
  281. }
  282. }
  283. /// 判断是否可进入对就界面
  284. /// - Parameters:
  285. /// - type: 进入界面 1-我的作品 2-再创作 3-卡点视频
  286. /// PQVideoListModel 不能为空要使用音乐数据 3,直接创作进入相机
  287. /// - videoData: type == 2 时必传
  288. func jumpToDetailVc(type: Int, videoData: PQVideoListModel? = nil) {
  289. jumpType = type
  290. jumpVideoData = videoData
  291. // 无网
  292. if !isNetConnected() {
  293. let remindData: PQBaseModel = PQBaseModel()
  294. remindData.title = "似乎已断开与互联网的连接"
  295. let remindView = PQRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  296. UIApplication.shared.keyWindow?.addSubview(remindView)
  297. remindView.remindData = remindData
  298. remindView.remindBlock = { [weak self] sender, _ in
  299. if sender.tag == 2 {
  300. openAppSetting()
  301. }
  302. }
  303. return
  304. }
  305. // 先判断是否登录
  306. if !BFLoginUserInfo.shared.isLogin() {
  307. isJumpToLogin = true
  308. addNotification(self, selector: #selector(loginSuccesss), name: cLoginSuccesssNotiKey, object: nil)
  309. let vc = MVLoginController()
  310. let navigationController: UINavigationController = UINavigationController(rootViewController: vc)
  311. navigationController.modalPresentationStyle = .fullScreen
  312. present(navigationController, animated: true, completion: nil)
  313. return
  314. }
  315. // 用户被封禁
  316. BFLog(message: "userStatus is \(BFLoginUserInfo.shared.userStatus)")
  317. if BFLoginUserInfo.shared.userStatus == "3" {
  318. PQBandingPhoneRemindView.accountBlockRemindView(remindTitle: "无法上传视频")
  319. return
  320. }
  321. // 未绑定手机号
  322. BFLog(message: "phoneNumber is \(BFLoginUserInfo.shared.phoneNumber)")
  323. if BFLoginUserInfo.shared.phoneNumber.count <= 0 || BFLoginUserInfo.shared.phoneNumber == "<null>" {
  324. let remindView = PQBandingPhoneRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  325. remindView.tag = cBandinPhoneTag
  326. remindView.bandingPhoneRemindHandle = { [weak self] sender in
  327. if sender.tag == 2 {
  328. self?.isJumpToLogin = true
  329. addNotification(self!, selector: #selector(self?.loginSuccesss), name: cBandingPhoneSuccessKey, object: nil)
  330. self?.navigationController?.pushViewController(PQBandingPhoneController(), animated: true)
  331. }
  332. }
  333. remindView.remindBlock = { [weak self] _, webUrl in
  334. let detail = PQBaseWebViewController()
  335. detail.baseUrl = webUrl
  336. self?.navigationController?.pushViewController(detail, animated: true)
  337. }
  338. view.addSubview(remindView)
  339. return
  340. }
  341. // 条件都满足转换到对就界面
  342. switch type {
  343. case 1:
  344. navigationController?.pushViewController(MVMineProductController(), animated: true)
  345. case 2:
  346. PQStuckPointViewModel.stuckPointProjectMusicInfo(projectId: videoData?.reCreateVideoData?.projectId ?? "") { [weak self] musicData, _ in
  347. if musicData != nil {
  348. let vc = PQStuckPointMaterialController()
  349. vc.reCreateMusicData = musicData
  350. vc.reCreateVideoData = videoData?.reCreateVideoData
  351. self?.navigationController?.pushViewController(vc, animated: true)
  352. // 关闭视频播放
  353. self?.controlPlayrPasueOrResume(isPause: true)
  354. } else { BFLog(message: "musicData is ni") }
  355. }
  356. case 3:
  357. let nav = UINavigationController(rootViewController: PQStuckPointMaterialController())
  358. nav.modalPresentationStyle = .fullScreen
  359. present(nav, animated: true, completion: nil)
  360. // 关闭视频播放
  361. controlPlayrPasueOrResume(isPause: true)
  362. default:
  363. break
  364. }
  365. }
  366. // 再创作视频
  367. func createVideo(indexPath: IndexPath) {
  368. if mAllVideos.count > indexPath.row {
  369. isCreateVideo = true
  370. let videoListModel = mAllVideos[indexPath.row]
  371. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusicRecreate, pageSource: .sp_shanyinApp_main, extParams: ["videoId":videoListModel.videoId], remindmsg: "")
  372. jumpToDetailVc(type: 2, videoData: videoListModel)
  373. } else {
  374. BFLog(message: "数据出错越界!!! mAllVideos.count: \(mAllVideos.count) indexPath.row: \(indexPath.row)")
  375. }
  376. }
  377. // 卡点模块消失
  378. @objc func stuckPointDismiss() {
  379. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) { [weak self] in
  380. self?.controlPlayrPasueOrResume(isPause: false)
  381. }
  382. }
  383. /// 获取用户状态信息
  384. /// - Returns: description
  385. func getUserInfo() {
  386. PQMineViewModel.userStatus { _, _ in
  387. }
  388. }
  389. }
  390. // MARK: - 播放器操作相关方法
  391. extension MVHomeController {
  392. /// 播放指定位置的视频
  393. /// - Parameter page: 视频数据位置
  394. func playVideo(page: Int) {
  395. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
  396. if !(UIViewController.getCurrentViewController()?.isMember(of: MVHomeController.self))! {
  397. BFLog(message: "当前显示的界面不是在首界面,不进行播放")
  398. return
  399. }
  400. DispatchQueue.main.async {
  401. let cell: MVBannerCell? = self.viewListView.collectionView.cellForItem(at: IndexPath(row: page, section: 0)) as? MVBannerCell
  402. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  403. if self.mVideos.count > 0, cell != nil, showProtocal != nil, showProtocal == "1" {
  404. cell?.pauseView.isHidden = true
  405. cell?.progressView.progress = 0
  406. let videoInfo = self.mAllVideos[page]
  407. videoInfo.playProgress = 0
  408. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonView, objectType: .ot_shanyinApp_viewButton_syncedUpMusicRecreate, pageSource: .sp_shanyinApp_main, extParams: ["videoId":videoInfo.videoId], remindmsg: "")
  409. let hotVideoIndex = self.mVideos.firstIndex(where: { (items) -> Bool in
  410. items.categoryName == videoInfo.categoryName
  411. })
  412. BFLog(message: "播放的视频分类为:\(String(describing: self.mVideos[hotVideoIndex ?? 0].categoryName)) hotVideoIndex :\(String(describing: hotVideoIndex)) page is \(page)")
  413. // 设置选中状态
  414. for hot in self.mVideos {
  415. hot.isSelected = false
  416. }
  417. self.mVideos[hotVideoIndex ?? 0].isSelected = true
  418. self.categoryCollectionView.reloadData()
  419. self.categoryCollectionView.scrollToItem(at: IndexPath(row: hotVideoIndex ?? 0, section: 0), at: .centeredHorizontally, animated: true)
  420. self.lastBnnerSelectIndex = IndexPath(row: page, section: 0)
  421. PQSingletoVideoPlayer.shared.configPlyer(videoData: videoInfo, controllerView: cell!.converView)
  422. self.controlPlayrPasueOrResume(isPause: false)
  423. } else {
  424. BFLog(message: "播放不成功: cell:\(String(describing: cell)) mVideos:\(self.mVideos.count) showProtocal: is \(String(describing: showProtocal))")
  425. }
  426. }
  427. }
  428. }
  429. // 控制播放器的暂停或恢复
  430. func controlPlayrPasueOrResume(isPause: Bool) {
  431. let cell: MVBannerCell? = viewListView.collectionView.cellForItem(at: lastBnnerSelectIndex) as? MVBannerCell
  432. BFLog(message: " player isPause is \(isPause)")
  433. if isPause {
  434. cell?.pauseView.isHidden = false
  435. PQSingletoVideoPlayer.shared.pausePlayer()
  436. } else {
  437. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  438. if rootViewController is UINavigationController {
  439. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  440. }
  441. BFLog(message: "rootViewController is \(String(describing: rootViewController))")
  442. if cell != nil && cell?.videoData != nil{
  443. cell?.pauseView.isHidden = true
  444. PQSingletoVideoPlayer.shared.configPlyer(videoData: (cell?.videoData)!, controllerView: cell!.converView)
  445. PQSingletoVideoPlayer.shared.startPlayr()
  446. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  447. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  448. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  449. cell?.progressView.progress = playProgress / duration
  450. }
  451. PQSingletoVideoPlayer.shared.playStatusBloc = { [weak self] status in
  452. // 播放完成 自动播放下一个
  453. if status == .PQVIDEO_PLAY_STATUS_END {
  454. PQSingletoVideoPlayer.shared.playVideoData!.playProgress = 0
  455. if (self?.lastBnnerSelectIndex.row ?? 0) < (self?.mAllVideos.count ?? 0) - 1 {
  456. self?.lastBnnerSelectIndex = IndexPath(row: (self?.lastBnnerSelectIndex.row ?? 0) + 1, section: 0)
  457. self?.playVideo(page: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row)
  458. // 视频列表跳转
  459. self?.viewListView.currentIndex = (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row
  460. self?.viewListView.scrollToItem(at: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row, animated: false)
  461. } else {
  462. BFLog(message: "已经是最后一个视频。")
  463. cell?.pauseView.isHidden = false
  464. }
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. // MARK: - 视频列表相关代理
  472. extension MVHomeController: MVBannerDataSource, MVBannerDelegate {
  473. func numberOfItems() -> Int {
  474. return mAllVideos.count
  475. }
  476. func banner(_ banner: MVBanner, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  477. let cell: MVBannerCell = banner.dequeueReusableCell(for: indexPath)
  478. cell.videoData = mAllVideos[indexPath.row]
  479. cell.reCreateBtnClicHandle = { [weak self] _, _ in
  480. BFLog(message: "点击了创同款")
  481. self?.createVideo(indexPath: indexPath)
  482. }
  483. return cell
  484. }
  485. // MARK: - GXBannerDelegate
  486. func banner(_: MVBanner, didSelectItemAt indexPath: IndexPath) {
  487. NSLog("didSelectItemAt %d", indexPath.row)
  488. lastBnnerSelectIndex = indexPath
  489. controlPlayrPasueOrResume(isPause: PQSingletoVideoPlayer.shared.isPlaying)
  490. }
  491. func pageControl(currentPage page: Int) {
  492. BFLog(message: "page is \(page)")
  493. playVideo(page: page)
  494. }
  495. }
  496. // MARK: - 划动分类相关代理
  497. ///
  498. extension MVHomeController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  499. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  500. return mVideos.count
  501. }
  502. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  503. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  504. let cell = MVTagsCell.tagsCell(collectionView: collectionView, indexPath: indexPath)
  505. cell.bgmData = itemData
  506. return cell
  507. }
  508. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  509. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  510. let width: CGFloat = CGFloat(CGFloat(itemData.categoryName.ga_widthForComment(font: UIFont.boldSystemFont(ofSize: 17), height: 17.0)) + 43)
  511. return CGSize(width: width, height: collectionView.frame.height - 14)
  512. }
  513. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  514. for hot in mVideos {
  515. hot.isSelected = false
  516. }
  517. let itemDataNew: MVHotVideoModel = mVideos[indexPath.item]
  518. itemDataNew.isSelected = true
  519. categoryCollectionView.reloadData()
  520. // 找出当前选择分类之前的视频数量和
  521. var videosCount: Int = 0
  522. if indexPath.row != 0 {
  523. for i in 0 ... indexPath.row - 1 {
  524. videosCount = videosCount + mVideos[i].videos.count
  525. }
  526. BFLog(message: "indexPath 选择\(indexPath.item) 之前的视频合\(videosCount)")
  527. }
  528. viewListView.currentIndex = videosCount
  529. viewListView.scrollToItem(at: videosCount, animated: false)
  530. self.playVideo(page: videosCount)
  531. }
  532. }
  533. extension UIViewController {
  534. /// 获取当前显示的VC
  535. ///
  536. /// - Returns: 当前屏幕显示的VC
  537. class func getCurrentViewController() -> UIViewController?{
  538. // 获取当先显示的window
  539. var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
  540. if currentWindow.windowLevel != UIWindow.Level.normal {
  541. let windowArr = UIApplication.shared.windows
  542. for window in windowArr {
  543. if window.windowLevel == UIWindow.Level.normal {
  544. currentWindow = window
  545. break
  546. }
  547. }
  548. }
  549. return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
  550. }
  551. private class func getNextXController(nextController: UIViewController?) -> UIViewController? {
  552. if nextController == nil {
  553. return nil
  554. }else if nextController?.presentedViewController != nil {
  555. return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
  556. }else if let tabbar = nextController as? UITabBarController {
  557. return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
  558. }else if let nav = nextController as? UINavigationController {
  559. return UIViewController.getNextXController(nextController: nav.visibleViewController)
  560. }
  561. return nextController
  562. }
  563. }