MVHomeController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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: "#3DC1C1")
  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. controlPlayrPasueOrResume(isPause: true)
  106. }
  107. @objc func willEnterForeground() {
  108. BFLog(message: "进入到前台")
  109. controlPlayrPasueOrResume(isPause: false)
  110. }
  111. override func viewWillAppear(_: Bool) {
  112. super.viewWillAppear(true)
  113. controlPlayrPasueOrResume(isPause: false)
  114. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  115. PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
  116. }
  117. override func viewWillDisappear(_ animated: Bool) {
  118. super.viewWillDisappear(animated)
  119. controlPlayrPasueOrResume(isPause: true)
  120. }
  121. func getData() {
  122. if mVideos.count > 0 {
  123. BFLog(message: "已经请求过数据")
  124. return
  125. }
  126. PQLoadingHUB.shared.showHUB(superView:view)
  127. MVHomeViewModel.getHomeHotVideos { [weak self] hotVideos, msg in
  128. if(msg == nil){
  129. BFLog(message: "hotVideos count is :\(hotVideos.count)")
  130. PQLoadingHUB.shared.dismissHUB(superView:(self?.view)!)
  131. self?.showNetworkError(isHidden: true)
  132. self?.refreshData(hotVideos: hotVideos)
  133. }else{
  134. BFLog(message: "请求数据出错 :\(String(describing: msg))")
  135. self?.showNetworkError(isHidden: false)
  136. }
  137. }
  138. }
  139. //显示网络未连接提示
  140. func showNetworkError(isHidden:Bool) {
  141. emptyRemindView.isHidden = isHidden
  142. emptyRemindView.remindLab.textColor = .black
  143. emptyRemindView.remindLab.text = "网络错误"
  144. emptyRemindView.remindLab.font = UIFont.boldSystemFont(ofSize: 20)
  145. emptyRemindView.imageView.image = nil
  146. emptyRemindView.remindSubLab.text = "请检查网络后重试"
  147. emptyRemindView.remindSubLab.isHidden = false
  148. emptyRemindView.refreshBtn.isHidden = false
  149. emptyRemindView.refreshBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  150. emptyRemindView.refreshBtn.setTitle("重试", for: .normal)
  151. if(isHidden){
  152. emptyRemindView.removeFromSuperview()
  153. }else{
  154. view.addSubview(emptyRemindView)
  155. }
  156. }
  157. func refreshData( hotVideos:Array<MVHotVideoModel>) {
  158. mVideos = hotVideos
  159. categoryCollectionView.reloadData()
  160. if mVideos.count > 0 {
  161. mVideos[0].isSelected = true
  162. for hotModel in mVideos {
  163. for video in hotModel.videos {
  164. mAllVideos.append(video)
  165. }
  166. }
  167. BFLog(message: " self?.mAllVideos count is \(String(describing: mAllVideos.count))")
  168. }
  169. viewListView.reloadData()
  170. // 自动播放第一个视频 确保 reloaddata 完成
  171. playVideo(page: 0)
  172. }
  173. override func viewDidLoad() {
  174. super.viewDidLoad()
  175. addNotification(self, selector: #selector(stuckPointDismiss), name: cFinishedPublishedNotiKey, object: nil)
  176. view.addSubview(mineBtn)
  177. view.addSubview(stuckPointBtn)
  178. view.addSubview(settingBtn)
  179. view.addSubview(viewListView)
  180. view.addSubview(categoryCollectionView)
  181. viewListView.dataSource = self
  182. viewListView.delegate = self
  183. viewListView.isShowPageControl = false
  184. viewListView.reloadData()
  185. addLayout()
  186. // 请求用户状态
  187. getUserInfo()
  188. // 网络监听有网后取一次数据
  189. manager?.startListening()
  190. manager?.listener = { [weak self] status in
  191. if status == .reachable(.wwan) || status == .reachable(.ethernetOrWiFi) {
  192. BFLog(message: "来网了")
  193. self?.showNetworkError(isHidden: true)
  194. self?.getData()
  195. } else {
  196. if(MVHotVideoModel.getCacheData().count == 0){
  197. self?.showNetworkError(isHidden: false)
  198. }
  199. }
  200. }
  201. let cacheData = MVHotVideoModel.getCacheData()
  202. if(cacheData.count > 0){
  203. BFLog(message: "有缓存数据 ")
  204. refreshData(hotVideos: cacheData)
  205. }
  206. // 播放器进度和状态回调
  207. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  208. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  209. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  210. cell?.progressView.progress = playProgress / duration
  211. }
  212. PQSingletoVideoPlayer.shared.playStatusBloc = { [weak self] status in
  213. // 播放完成 自动播放下一个
  214. if status == .PQVIDEO_PLAY_STATUS_END {
  215. PQSingletoVideoPlayer.shared.playVideoData!.playProgress = 0
  216. if (self?.lastBnnerSelectIndex.row ?? 0) < (self?.mAllVideos.count ?? 0) - 1 {
  217. self?.lastBnnerSelectIndex = IndexPath(row: (self?.lastBnnerSelectIndex.row ?? 0) + 1, section: 0)
  218. self?.playVideo(page: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row)
  219. // 视频列表跳转
  220. self?.viewListView.currentIndex = (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row
  221. self?.viewListView.scrollToItem(at: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row, animated: false)
  222. } else { BFLog(message: "已经是最后一个视频。") }
  223. }
  224. }
  225. // 添加协议
  226. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  227. if showProtocal == nil || showProtocal?.count ?? 0 <= 0 || showProtocal != "1" {
  228. let protocalView: PQServerProtocalView = PQServerProtocalView(frame: view.frame)
  229. protocalView.tag = cProtocalViewTag
  230. protocalView.remindBlock = { [weak self] sender, webUrl in
  231. if sender == nil, webUrl != nil {
  232. let detail = PQBaseWebViewController()
  233. detail.baseUrl = webUrl
  234. self?.navigationController?.pushViewController(detail, animated: true)
  235. }
  236. // 移除继续播放
  237. if sender != nil {
  238. self?.playVideo(page: 0)
  239. }
  240. }
  241. UIApplication.shared.keyWindow?.addSubview(protocalView)
  242. }
  243. }
  244. func addLayout() {
  245. categoryCollectionView.snp.remakeConstraints { make in
  246. make.width.equalTo(cScreenWidth)
  247. make.height.equalTo(45)
  248. make.left.equalToSuperview().offset(0)
  249. make.top.equalToSuperview().offset(cDevice_iPhoneStatusBarHei)
  250. }
  251. stuckPointBtn.snp.remakeConstraints { make in
  252. make.width.height.equalTo(60)
  253. make.centerX.equalToSuperview()
  254. make.bottom.equalToSuperview().offset(-(cDefaultMargin + cAKSafeAreaHeight))
  255. }
  256. mineBtn.snp.remakeConstraints { make in
  257. make.width.height.equalTo(60)
  258. make.left.equalToSuperview().offset(cDefaultMargin * 3)
  259. make.centerY.equalTo(stuckPointBtn)
  260. }
  261. settingBtn.snp.remakeConstraints { make in
  262. make.width.height.equalTo(60)
  263. make.right.equalToSuperview().offset(-cDefaultMargin * 3)
  264. make.centerY.equalTo(stuckPointBtn)
  265. }
  266. viewListView.snp.remakeConstraints { make in
  267. make.left.right.equalToSuperview()
  268. make.top.equalTo(categoryCollectionView.snp_bottom).offset(cDefaultMargin)
  269. make.bottom.equalTo(stuckPointBtn.snp_top).offset(-cDefaultMargin * 2)
  270. }
  271. }
  272. @objc func btnClick(sender: UIButton) {
  273. switch sender.tag {
  274. case 1000: // 个人中心
  275. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_mineTab, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  276. jumpToDetailVc(type: 1)
  277. case 2000: // 系统相册
  278. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusic, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  279. jumpToDetailVc(type: 3)
  280. case 3000: // 设置
  281. navigationController?.pushViewController(MVSettingController(), animated: true)
  282. default:
  283. break
  284. }
  285. }
  286. /// 判断是否可进入对就界面
  287. /// - Parameters:
  288. /// - type: 进入界面 1-我的作品 2-再创作 3-卡点视频
  289. /// PQVideoListModel 不能为空要使用音乐数据 3,直接创作进入相机
  290. /// - videoData: type == 2 时必传
  291. func jumpToDetailVc(type: Int, videoData: PQVideoListModel? = nil) {
  292. jumpType = type
  293. jumpVideoData = videoData
  294. // 无网
  295. if !isNetConnected() {
  296. let remindData: PQBaseModel = PQBaseModel()
  297. remindData.title = "似乎已断开与互联网的连接"
  298. let remindView = PQRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  299. UIApplication.shared.keyWindow?.addSubview(remindView)
  300. remindView.remindData = remindData
  301. remindView.remindBlock = { [weak self] sender, _ in
  302. if sender.tag == 2 {
  303. openAppSetting()
  304. }
  305. }
  306. return
  307. }
  308. // 先判断是否登录
  309. if !BFLoginUserInfo.shared.isLogin() {
  310. isJumpToLogin = true
  311. addNotification(self, selector: #selector(loginSuccesss), name: cLoginSuccesssNotiKey, object: nil)
  312. let vc = MVLoginController()
  313. vc.modalPresentationStyle = .fullScreen
  314. present(vc, animated: true, completion: nil)
  315. return
  316. }
  317. // 用户被封禁
  318. BFLog(message: "userStatus is \(BFLoginUserInfo.shared.userStatus)")
  319. if BFLoginUserInfo.shared.userStatus == "3" {
  320. PQBandingPhoneRemindView.accountBlockRemindView(remindTitle: "无法上传视频")
  321. return
  322. }
  323. // 未绑定手机号
  324. BFLog(message: "phoneNumber is \(BFLoginUserInfo.shared.phoneNumber)")
  325. if BFLoginUserInfo.shared.phoneNumber.count <= 0 || BFLoginUserInfo.shared.phoneNumber == "<null>" {
  326. let remindView = PQBandingPhoneRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  327. remindView.tag = cBandinPhoneTag
  328. remindView.bandingPhoneRemindHandle = { [weak self] sender in
  329. if sender.tag == 2 {
  330. self?.isJumpToLogin = true
  331. addNotification(self!, selector: #selector(self?.loginSuccesss), name: cBandingPhoneSuccessKey, object: nil)
  332. self?.navigationController?.pushViewController(PQBandingPhoneController(), animated: true)
  333. }
  334. }
  335. remindView.remindBlock = { [weak self] _, webUrl in
  336. let detail = PQBaseWebViewController()
  337. detail.baseUrl = webUrl
  338. self?.navigationController?.pushViewController(detail, animated: true)
  339. }
  340. view.addSubview(remindView)
  341. return
  342. }
  343. // 条件都满足转换到对就界面
  344. switch type {
  345. case 1:
  346. navigationController?.pushViewController(MVMineProductController(), animated: true)
  347. case 2:
  348. PQStuckPointViewModel.stuckPointProjectMusicInfo(projectId: videoData?.reCreateVideoData?.projectId ?? "") { [weak self] musicData, _ in
  349. if musicData != nil {
  350. let vc = PQStuckPointMaterialController()
  351. vc.reCreateMusicData = musicData
  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 = .overFullScreen
  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() + 1) { [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. PQSingletoVideoPlayer.shared.startPlayr()
  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 \(rootViewController)")
  443. if rootViewController is MVHomeController && 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. }
  448. }
  449. }
  450. }
  451. // MARK: - 视频列表相关代理
  452. extension MVHomeController: MVBannerDataSource, MVBannerDelegate {
  453. func numberOfItems() -> Int {
  454. return mAllVideos.count
  455. }
  456. func banner(_ banner: MVBanner, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  457. let cell: MVBannerCell = banner.dequeueReusableCell(for: indexPath)
  458. cell.videoData = mAllVideos[indexPath.row]
  459. cell.reCreateBtnClicHandle = { [weak self] _, _ in
  460. BFLog(message: "点击了创同款")
  461. self?.createVideo(indexPath: indexPath)
  462. }
  463. return cell
  464. }
  465. // MARK: - GXBannerDelegate
  466. func banner(_: MVBanner, didSelectItemAt indexPath: IndexPath) {
  467. NSLog("didSelectItemAt %d", indexPath.row)
  468. lastBnnerSelectIndex = indexPath
  469. controlPlayrPasueOrResume(isPause: PQSingletoVideoPlayer.shared.isPlaying)
  470. }
  471. func pageControl(currentPage page: Int) {
  472. BFLog(message: "page is \(page)")
  473. playVideo(page: page)
  474. }
  475. }
  476. // MARK: - 划动分类相关代理
  477. ///
  478. extension MVHomeController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  479. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  480. return mVideos.count
  481. }
  482. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  483. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  484. let cell = MVTagsCell.tagsCell(collectionView: collectionView, indexPath: indexPath)
  485. cell.bgmData = itemData
  486. return cell
  487. }
  488. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  489. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  490. let width: CGFloat = CGFloat(CGFloat(itemData.categoryName.ga_widthForComment(font: UIFont.boldSystemFont(ofSize: 17), height: 17.0)) + 43)
  491. return CGSize(width: width, height: collectionView.frame.height - 14)
  492. }
  493. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  494. for hot in mVideos {
  495. hot.isSelected = false
  496. }
  497. let itemDataNew: MVHotVideoModel = mVideos[indexPath.item]
  498. itemDataNew.isSelected = true
  499. categoryCollectionView.reloadData()
  500. // 找出当前选择分类之前的视频数量和
  501. var videosCount: Int = 0
  502. if indexPath.row != 0 {
  503. for i in 0 ... indexPath.row - 1 {
  504. videosCount = videosCount + mVideos[i].videos.count
  505. }
  506. BFLog(message: "indexPath 选择\(indexPath.item) 之前的视频合\(videosCount)")
  507. }
  508. viewListView.currentIndex = videosCount
  509. viewListView.scrollToItem(at: videosCount, animated: false)
  510. self.playVideo(page: videosCount)
  511. }
  512. }
  513. extension UIViewController {
  514. /// 获取当前显示的VC
  515. ///
  516. /// - Returns: 当前屏幕显示的VC
  517. class func getCurrentViewController() -> UIViewController?{
  518. // 获取当先显示的window
  519. var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
  520. if currentWindow.windowLevel != UIWindow.Level.normal {
  521. let windowArr = UIApplication.shared.windows
  522. for window in windowArr {
  523. if window.windowLevel == UIWindow.Level.normal {
  524. currentWindow = window
  525. break
  526. }
  527. }
  528. }
  529. return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
  530. }
  531. private class func getNextXController(nextController: UIViewController?) -> UIViewController? {
  532. if nextController == nil {
  533. return nil
  534. }else if nextController?.presentedViewController != nil {
  535. return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
  536. }else if let tabbar = nextController as? UITabBarController {
  537. return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
  538. }else if let nav = nextController as? UINavigationController {
  539. return UIViewController.getNextXController(nextController: nav.visibleViewController)
  540. }
  541. return nextController
  542. }
  543. }