MVHomeController.swift 26 KB

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