MVHomeController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. let cacheData = MVHotVideoModel.getCacheData()
  211. if(cacheData.count > 0){
  212. BFLog(message: "有缓存数据 ")
  213. self?.refreshData(hotVideos: cacheData)
  214. }
  215. }
  216. }
  217. // 播放器进度和状态回调
  218. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  219. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  220. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  221. cell?.progressView.progress = playProgress / duration
  222. }
  223. // 添加协议
  224. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  225. if showProtocal == nil || showProtocal?.count ?? 0 <= 0 || showProtocal != "1" {
  226. let protocalView: PQServerProtocalView = PQServerProtocalView(frame: view.frame)
  227. protocalView.tag = cProtocalViewTag
  228. protocalView.remindBlock = { [weak self] sender, webUrl in
  229. if sender == nil, webUrl != nil {
  230. let detail = PQBaseWebViewController()
  231. detail.baseUrl = webUrl
  232. self?.navigationController?.pushViewController(detail, animated: true)
  233. }
  234. // 移除继续播放
  235. if sender != nil {
  236. self?.playVideo(page: 0)
  237. }
  238. }
  239. UIApplication.shared.keyWindow?.addSubview(protocalView)
  240. }
  241. }
  242. func addLayout() {
  243. categoryCollectionView.snp.remakeConstraints { make in
  244. make.width.equalTo(cScreenWidth)
  245. make.height.equalTo(45)
  246. make.left.equalToSuperview().offset(0)
  247. make.top.equalToSuperview().offset(cDevice_iPhoneStatusBarHei)
  248. }
  249. stuckPointBtn.snp.remakeConstraints { make in
  250. make.width.height.equalTo(60)
  251. make.centerX.equalToSuperview()
  252. make.bottom.equalToSuperview().offset(-(cDefaultMargin + cAKSafeAreaHeight))
  253. }
  254. mineBtn.snp.remakeConstraints { make in
  255. make.width.height.equalTo(60)
  256. make.left.equalToSuperview().offset(cDefaultMargin * 3)
  257. make.centerY.equalTo(stuckPointBtn)
  258. }
  259. settingBtn.snp.remakeConstraints { make in
  260. make.width.height.equalTo(60)
  261. make.right.equalToSuperview().offset(-cDefaultMargin * 3)
  262. make.centerY.equalTo(stuckPointBtn)
  263. }
  264. viewListView.snp.remakeConstraints { make in
  265. make.left.right.equalToSuperview()
  266. make.top.equalTo(categoryCollectionView.snp_bottom).offset(cDefaultMargin)
  267. make.bottom.equalTo(stuckPointBtn.snp_top).offset(-cDefaultMargin * 2)
  268. }
  269. }
  270. @objc func btnClick(sender: UIButton) {
  271. switch sender.tag {
  272. case 1000: // 个人中心
  273. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_mineTab, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  274. jumpToDetailVc(type: 1)
  275. case 2000: // 系统相册
  276. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusic, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  277. jumpToDetailVc(type: 3)
  278. case 3000: // 设置
  279. navigationController?.pushViewController(MVSettingController(), animated: true)
  280. default:
  281. break
  282. }
  283. }
  284. /// 判断是否可进入对就界面
  285. /// - Parameters:
  286. /// - type: 进入界面 1-我的作品 2-再创作 3-卡点视频
  287. /// PQVideoListModel 不能为空要使用音乐数据 3,直接创作进入相机
  288. /// - videoData: type == 2 时必传
  289. func jumpToDetailVc(type: Int, videoData: PQVideoListModel? = nil) {
  290. jumpType = type
  291. jumpVideoData = videoData
  292. // 无网
  293. if !isNetConnected() {
  294. let remindData: PQBaseModel = PQBaseModel()
  295. remindData.title = "似乎已断开与互联网的连接"
  296. let remindView = PQRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  297. UIApplication.shared.keyWindow?.addSubview(remindView)
  298. remindView.remindData = remindData
  299. remindView.remindBlock = { [weak self] sender, _ in
  300. if sender.tag == 2 {
  301. openAppSetting()
  302. }
  303. }
  304. return
  305. }
  306. // 先判断是否登录
  307. if !BFLoginUserInfo.shared.isLogin() {
  308. isJumpToLogin = true
  309. addNotification(self, selector: #selector(loginSuccesss), name: cLoginSuccesssNotiKey, object: nil)
  310. let vc = MVLoginController()
  311. let navigationController: UINavigationController = UINavigationController(rootViewController: vc)
  312. navigationController.modalPresentationStyle = .fullScreen
  313. present(navigationController, animated: true, completion: nil)
  314. return
  315. }
  316. // 用户被封禁
  317. BFLog(message: "userStatus is \(BFLoginUserInfo.shared.userStatus)")
  318. if BFLoginUserInfo.shared.userStatus == "3" {
  319. PQBandingPhoneRemindView.accountBlockRemindView(remindTitle: "无法上传视频")
  320. return
  321. }
  322. // 未绑定手机号
  323. BFLog(message: "phoneNumber is \(BFLoginUserInfo.shared.phoneNumber)")
  324. if BFLoginUserInfo.shared.phoneNumber.count <= 0 || BFLoginUserInfo.shared.phoneNumber == "<null>" {
  325. let remindView = PQBandingPhoneRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  326. remindView.tag = cBandinPhoneTag
  327. remindView.bandingPhoneRemindHandle = { [weak self] sender in
  328. if sender.tag == 2 {
  329. self?.isJumpToLogin = true
  330. addNotification(self!, selector: #selector(self?.loginSuccesss), name: cBandingPhoneSuccessKey, object: nil)
  331. self?.navigationController?.pushViewController(PQBandingPhoneController(), animated: true)
  332. }
  333. }
  334. remindView.remindBlock = { [weak self] _, webUrl in
  335. let detail = PQBaseWebViewController()
  336. detail.baseUrl = webUrl
  337. self?.navigationController?.pushViewController(detail, animated: true)
  338. }
  339. view.addSubview(remindView)
  340. return
  341. }
  342. // 条件都满足转换到对就界面
  343. switch type {
  344. case 1:
  345. navigationController?.pushViewController(MVMineProductController(), animated: true)
  346. case 2:
  347. PQStuckPointViewModel.stuckPointProjectMusicInfo(projectId: videoData?.reCreateVideoData?.projectId ?? "") { [weak self] musicData, _ in
  348. if musicData != nil {
  349. let vc = PQStuckPointMaterialController()
  350. vc.reCreateMusicData = musicData
  351. vc.reCreateVideoData = videoData?.reCreateVideoData
  352. self?.navigationController?.pushViewController(vc, animated: true)
  353. // 关闭视频播放
  354. self?.controlPlayrPasueOrResume(isPause: true)
  355. } else { BFLog(message: "musicData is ni") }
  356. }
  357. case 3:
  358. let nav = UINavigationController(rootViewController: PQStuckPointMaterialController())
  359. nav.modalPresentationStyle = .fullScreen
  360. present(nav, animated: true, completion: nil)
  361. // 关闭视频播放
  362. controlPlayrPasueOrResume(isPause: true)
  363. default:
  364. break
  365. }
  366. }
  367. // 再创作视频
  368. func createVideo(indexPath: IndexPath) {
  369. if mAllVideos.count > indexPath.row {
  370. isCreateVideo = true
  371. let videoListModel = mAllVideos[indexPath.row]
  372. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusicRecreate, pageSource: .sp_shanyinApp_main, extParams: ["videoId":videoListModel.videoId], remindmsg: "")
  373. jumpToDetailVc(type: 2, videoData: videoListModel)
  374. } else {
  375. BFLog(message: "数据出错越界!!! mAllVideos.count: \(mAllVideos.count) indexPath.row: \(indexPath.row)")
  376. }
  377. }
  378. // 卡点模块消失
  379. @objc func stuckPointDismiss() {
  380. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) { [weak self] in
  381. self?.controlPlayrPasueOrResume(isPause: false)
  382. }
  383. }
  384. /// 获取用户状态信息
  385. /// - Returns: description
  386. func getUserInfo() {
  387. PQMineViewModel.userStatus { _, _ in
  388. }
  389. }
  390. }
  391. // MARK: - 播放器操作相关方法
  392. extension MVHomeController {
  393. /// 播放指定位置的视频
  394. /// - Parameter page: 视频数据位置
  395. func playVideo(page: Int) {
  396. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
  397. if !(UIViewController.getCurrentViewController()?.isMember(of: MVHomeController.self))! {
  398. BFLog(message: "当前显示的界面不是在首界面,不进行播放")
  399. return
  400. }
  401. DispatchQueue.main.async {
  402. let cell: MVBannerCell? = self.viewListView.collectionView.cellForItem(at: IndexPath(row: page, section: 0)) as? MVBannerCell
  403. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  404. if self.mVideos.count > 0, cell != nil, showProtocal != nil, showProtocal == "1" {
  405. cell?.pauseView.isHidden = true
  406. cell?.progressView.progress = 0
  407. let videoInfo = self.mAllVideos[page]
  408. videoInfo.playProgress = 0
  409. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonView, objectType: .ot_shanyinApp_viewButton_syncedUpMusicRecreate, pageSource: .sp_shanyinApp_main, extParams: ["videoId":videoInfo.videoId], remindmsg: "")
  410. let hotVideoIndex = self.mVideos.firstIndex(where: { (items) -> Bool in
  411. items.categoryName == videoInfo.categoryName
  412. })
  413. BFLog(message: "播放的视频分类为:\(String(describing: self.mVideos[hotVideoIndex ?? 0].categoryName)) hotVideoIndex :\(String(describing: hotVideoIndex)) page is \(page)")
  414. // 设置选中状态
  415. for hot in self.mVideos {
  416. hot.isSelected = false
  417. }
  418. self.mVideos[hotVideoIndex ?? 0].isSelected = true
  419. self.categoryCollectionView.reloadData()
  420. self.categoryCollectionView.scrollToItem(at: IndexPath(row: hotVideoIndex ?? 0, section: 0), at: .centeredHorizontally, animated: true)
  421. self.lastBnnerSelectIndex = IndexPath(row: page, section: 0)
  422. PQSingletoVideoPlayer.shared.configPlyer(videoData: videoInfo, controllerView: cell!.converView)
  423. self.controlPlayrPasueOrResume(isPause: false)
  424. } else {
  425. BFLog(message: "播放不成功: cell:\(String(describing: cell)) mVideos:\(self.mVideos.count) showProtocal: is \(String(describing: showProtocal))")
  426. }
  427. }
  428. }
  429. }
  430. // 控制播放器的暂停或恢复
  431. func controlPlayrPasueOrResume(isPause: Bool) {
  432. let cell: MVBannerCell? = viewListView.collectionView.cellForItem(at: lastBnnerSelectIndex) as? MVBannerCell
  433. BFLog(message: " player isPause is \(isPause)")
  434. if isPause {
  435. cell?.pauseView.isHidden = false
  436. PQSingletoVideoPlayer.shared.pausePlayer()
  437. } else {
  438. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  439. if rootViewController is UINavigationController {
  440. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  441. }
  442. BFLog(message: "rootViewController is \(String(describing: rootViewController))")
  443. if cell != nil && cell?.videoData != nil{
  444. cell?.pauseView.isHidden = true
  445. PQSingletoVideoPlayer.shared.configPlyer(videoData: (cell?.videoData)!, controllerView: cell!.converView)
  446. PQSingletoVideoPlayer.shared.startPlayr()
  447. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  448. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  449. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  450. cell?.progressView.progress = playProgress / duration
  451. }
  452. PQSingletoVideoPlayer.shared.playStatusBloc = { [weak self] status in
  453. // 播放完成 自动播放下一个
  454. if status == .PQVIDEO_PLAY_STATUS_END {
  455. PQSingletoVideoPlayer.shared.playVideoData!.playProgress = 0
  456. if (self?.lastBnnerSelectIndex.row ?? 0) < (self?.mAllVideos.count ?? 0) - 1 {
  457. self?.lastBnnerSelectIndex = IndexPath(row: (self?.lastBnnerSelectIndex.row ?? 0) + 1, section: 0)
  458. self?.playVideo(page: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row)
  459. // 视频列表跳转
  460. self?.viewListView.currentIndex = (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row
  461. self?.viewListView.scrollToItem(at: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row, animated: false)
  462. } else {
  463. BFLog(message: "已经是最后一个视频。")
  464. cell?.pauseView.isHidden = false
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. // MARK: - 视频列表相关代理
  473. extension MVHomeController: MVBannerDataSource, MVBannerDelegate {
  474. func numberOfItems() -> Int {
  475. return mAllVideos.count
  476. }
  477. func banner(_ banner: MVBanner, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  478. let cell: MVBannerCell = banner.dequeueReusableCell(for: indexPath)
  479. cell.videoData = mAllVideos[indexPath.row]
  480. cell.reCreateBtnClicHandle = { [weak self] _, _ in
  481. BFLog(message: "点击了创同款")
  482. self?.createVideo(indexPath: indexPath)
  483. }
  484. return cell
  485. }
  486. // MARK: - GXBannerDelegate
  487. func banner(_: MVBanner, didSelectItemAt indexPath: IndexPath) {
  488. NSLog("didSelectItemAt %d", indexPath.row)
  489. lastBnnerSelectIndex = indexPath
  490. controlPlayrPasueOrResume(isPause: PQSingletoVideoPlayer.shared.isPlaying)
  491. }
  492. func pageControl(currentPage page: Int) {
  493. BFLog(message: "page is \(page)")
  494. playVideo(page: page)
  495. }
  496. }
  497. // MARK: - 划动分类相关代理
  498. ///
  499. extension MVHomeController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  500. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  501. return mVideos.count
  502. }
  503. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  504. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  505. let cell = MVTagsCell.tagsCell(collectionView: collectionView, indexPath: indexPath)
  506. cell.bgmData = itemData
  507. return cell
  508. }
  509. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  510. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  511. let width: CGFloat = CGFloat(CGFloat(itemData.categoryName.ga_widthForComment(font: UIFont.boldSystemFont(ofSize: 17), height: 17.0)) + 43)
  512. return CGSize(width: width, height: collectionView.frame.height - 14)
  513. }
  514. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  515. for hot in mVideos {
  516. hot.isSelected = false
  517. }
  518. let itemDataNew: MVHotVideoModel = mVideos[indexPath.item]
  519. itemDataNew.isSelected = true
  520. categoryCollectionView.reloadData()
  521. // 找出当前选择分类之前的视频数量和
  522. var videosCount: Int = 0
  523. if indexPath.row != 0 {
  524. for i in 0 ... indexPath.row - 1 {
  525. videosCount = videosCount + mVideos[i].videos.count
  526. }
  527. BFLog(message: "indexPath 选择\(indexPath.item) 之前的视频合\(videosCount)")
  528. }
  529. viewListView.currentIndex = videosCount
  530. viewListView.scrollToItem(at: videosCount, animated: false)
  531. self.playVideo(page: videosCount)
  532. }
  533. }
  534. extension UIViewController {
  535. /// 获取当前显示的VC
  536. ///
  537. /// - Returns: 当前屏幕显示的VC
  538. class func getCurrentViewController() -> UIViewController?{
  539. // 获取当先显示的window
  540. var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
  541. if currentWindow.windowLevel != UIWindow.Level.normal {
  542. let windowArr = UIApplication.shared.windows
  543. for window in windowArr {
  544. if window.windowLevel == UIWindow.Level.normal {
  545. currentWindow = window
  546. break
  547. }
  548. }
  549. }
  550. return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
  551. }
  552. private class func getNextXController(nextController: UIViewController?) -> UIViewController? {
  553. if nextController == nil {
  554. return nil
  555. }else if nextController?.presentedViewController != nil {
  556. return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
  557. }else if let tabbar = nextController as? UITabBarController {
  558. return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
  559. }else if let nav = nextController as? UINavigationController {
  560. return UIViewController.getNextXController(nextController: nav.visibleViewController)
  561. }
  562. return nextController
  563. }
  564. }