MVHomeController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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))
  81. emptyRemindView.fullRefreshBloc = { [weak self] _, _ in
  82. self?.getData()
  83. }
  84. emptyRemindView.isNetLost = true
  85. emptyRemindView.remindLab.textColor = .black
  86. emptyRemindView.remindLab.text = "网络错误"
  87. emptyRemindView.remindLab.font = UIFont.boldSystemFont(ofSize: 20)
  88. emptyRemindView.imageView.image = nil
  89. emptyRemindView.remindSubLab.text = "请检查网络后重试"
  90. emptyRemindView.remindSubLab.isHidden = false
  91. emptyRemindView.refreshBtn.isHidden = false
  92. emptyRemindView.refreshBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  93. emptyRemindView.refreshBtn.setTitle("重试", for: .normal)
  94. return emptyRemindView
  95. }()
  96. var mVideos: Array = Array<MVHotVideoModel>.init()
  97. var mAllVideos: Array = Array<PQVideoListModel>.init()
  98. var lastBnnerSelectIndex: IndexPath = IndexPath(row: 0, section: 0)
  99. // 是否点击的再创作
  100. var isCreateVideo: Bool = false
  101. deinit {
  102. PQNotification.removeObserver(self)
  103. }
  104. // 登录成功后自动跳转到系统相册
  105. @objc func loginSuccesss() {
  106. if isJumpToLogin {
  107. isJumpToLogin = false
  108. PQNotification.removeObserver(self)
  109. // 跳转上传详情页
  110. jumpToDetailVc(type: jumpType, videoData: jumpVideoData)
  111. }
  112. }
  113. @objc func enterBackground() {
  114. BFLog(message: "进入到后台")
  115. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  116. if rootViewController is UINavigationController {
  117. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  118. }
  119. BFLog(message: "rootViewController is \(String(describing: rootViewController))")
  120. if rootViewController is MVHomeController {
  121. controlPlayrPasueOrResume(isPause: true)
  122. }
  123. }
  124. @objc func willEnterForeground() {
  125. BFLog(message: "进入到前台")
  126. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  127. if rootViewController is UINavigationController {
  128. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  129. }
  130. BFLog(message: "rootViewController is \(String(describing: rootViewController))")
  131. if rootViewController is MVHomeController {
  132. controlPlayrPasueOrResume(isPause: false)
  133. }
  134. }
  135. override func viewWillAppear(_: Bool) {
  136. super.viewWillAppear(true)
  137. controlPlayrPasueOrResume(isPause: false)
  138. // PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  139. // PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
  140. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.willResignActiveNotification, object: nil)
  141. PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.didBecomeActiveNotification, object: nil)
  142. }
  143. override func viewWillDisappear(_ animated: Bool) {
  144. super.viewWillDisappear(animated)
  145. controlPlayrPasueOrResume(isPause: true)
  146. }
  147. override func viewDidDisappear(_ animated: Bool) {
  148. super.viewDidDisappear(animated)
  149. controlPlayrPasueOrResume(isPause: true)
  150. }
  151. func getData() {
  152. PQLoadingHUB.shared.showHUB(superView:view)
  153. MVHomeViewModel.getHomeHotVideos { [weak self] hotVideos, msg in
  154. if(msg == nil){
  155. BFLog(1, message: "hotVideos count is :\(hotVideos.count)")
  156. PQLoadingHUB.shared.dismissHUB(superView:(self?.view)!)
  157. self?.showNetworkError(isHidden: true)
  158. self?.refreshData(hotVideos: hotVideos)
  159. }else{
  160. BFLog(message: "请求数据出错 :\(String(describing: msg))")
  161. self?.showNetworkError(isHidden: false)
  162. }
  163. }
  164. }
  165. //显示网络未连接提示
  166. func showNetworkError(isHidden:Bool) {
  167. //有数据时不进行操作
  168. if(MVHotVideoModel.getCacheData().count != 0){
  169. return
  170. }
  171. if(isHidden){
  172. emptyRemindView.removeFromSuperview()
  173. }else{
  174. view.addSubview(emptyRemindView)
  175. }
  176. }
  177. func refreshData( hotVideos:Array<MVHotVideoModel>) {
  178. mVideos = hotVideos
  179. categoryCollectionView.reloadData()
  180. if mVideos.count > 0 {
  181. mVideos[0].isSelected = true
  182. for hotModel in mVideos {
  183. for video in hotModel.videos {
  184. mAllVideos.append(video)
  185. }
  186. }
  187. BFLog(message: " self?.mAllVideos count is \(String(describing: mAllVideos.count))")
  188. }
  189. viewListView.reloadData()
  190. // 自动播放第一个视频 确保 reloaddata 完成
  191. // playVideo(page: 0)
  192. }
  193. override func viewDidLoad() {
  194. super.viewDidLoad()
  195. addNotification(self, selector: #selector(stuckPointDismiss), name: cFinishedPublishedNotiKey, object: nil)
  196. view.addSubview(mineBtn)
  197. view.addSubview(stuckPointBtn)
  198. view.addSubview(settingBtn)
  199. view.addSubview(viewListView)
  200. view.addSubview(categoryCollectionView)
  201. viewListView.dataSource = self
  202. viewListView.delegate = self
  203. viewListView.isShowPageControl = false
  204. viewListView.reloadData()
  205. addLayout()
  206. // 请求用户状态
  207. getUserInfo()
  208. // 网络监听有网后取一次数据
  209. manager?.startListening(onUpdatePerforming: { [weak self] status in
  210. if status == .reachable(.cellular) || status == .reachable(.ethernetOrWiFi) {
  211. BFLog(message: "来网了")
  212. self?.showNetworkError(isHidden: true)
  213. self?.getData()
  214. } else {
  215. let cacheData = MVHotVideoModel.getCacheData()
  216. if(cacheData.count == 0 && self?.mAllVideos.count ?? 0 == 0){
  217. self?.showNetworkError(isHidden: false)
  218. }else{
  219. //没有网时查看有无缓存
  220. if(cacheData.count > 0){
  221. BFLog(message: "有缓存数据 ")
  222. self?.refreshData(hotVideos: cacheData)
  223. }
  224. }
  225. }
  226. })
  227. // 播放器进度和状态回调
  228. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  229. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  230. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  231. cell?.progressView.progress = playProgress / duration
  232. }
  233. // 添加协议
  234. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  235. if showProtocal == nil || showProtocal?.count ?? 0 <= 0 || showProtocal != "1" {
  236. let protocalView: PQServerProtocalView = PQServerProtocalView(frame: view.frame)
  237. protocalView.tag = cProtocalViewTag
  238. protocalView.remindBlock = { [weak self] sender, webUrl in
  239. if sender == nil, webUrl != nil {
  240. let detail = PQBaseWebViewController()
  241. detail.baseUrl = webUrl
  242. self?.navigationController?.pushViewController(detail, animated: true)
  243. }
  244. // 移除继续播放
  245. if sender != nil {
  246. self?.playVideo(page: 0)
  247. }
  248. }
  249. UIApplication.shared.keyWindow?.addSubview(protocalView)
  250. }
  251. // try? AVAudioSession.sharedInstance().setCategory(.playback)
  252. }
  253. func addLayout() {
  254. categoryCollectionView.snp.remakeConstraints { make in
  255. make.width.equalTo(cScreenWidth)
  256. make.height.equalTo(45)
  257. make.left.equalToSuperview().offset(0)
  258. make.top.equalToSuperview().offset(cDevice_iPhoneStatusBarHei)
  259. }
  260. stuckPointBtn.snp.remakeConstraints { make in
  261. make.width.height.equalTo(60)
  262. make.centerX.equalToSuperview()
  263. make.bottom.equalToSuperview().offset(-(cDefaultMargin + cAKSafeAreaHeight))
  264. }
  265. mineBtn.snp.remakeConstraints { make in
  266. make.width.height.equalTo(60)
  267. make.left.equalToSuperview().offset(cDefaultMargin * 3)
  268. make.centerY.equalTo(stuckPointBtn)
  269. }
  270. settingBtn.snp.remakeConstraints { make in
  271. make.width.height.equalTo(60)
  272. make.right.equalToSuperview().offset(-cDefaultMargin * 3)
  273. make.centerY.equalTo(stuckPointBtn)
  274. }
  275. viewListView.snp.remakeConstraints { make in
  276. make.left.right.equalToSuperview()
  277. make.top.equalTo(categoryCollectionView.snp.bottom).offset(cDefaultMargin)
  278. make.bottom.equalTo(stuckPointBtn.snp.top).offset(-cDefaultMargin * 2)
  279. }
  280. }
  281. @objc func btnClick(sender: UIButton) {
  282. switch sender.tag {
  283. case 1000: // 个人中心
  284. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_mineTab, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  285. jumpToDetailVc(type: 1)
  286. case 2000: // 系统相册
  287. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusic, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")
  288. jumpToDetailVc(type: 3)
  289. case 3000: // 设置
  290. navigationController?.pushViewController(MVSettingController(), animated: true)
  291. default:
  292. break
  293. }
  294. }
  295. /// 判断是否可进入对就界面
  296. /// - Parameters:
  297. /// - type: 进入界面 1-我的作品 2-再创作 3-卡点视频
  298. /// PQVideoListModel 不能为空要使用音乐数据 3,直接创作进入相机
  299. /// - videoData: type == 2 时必传
  300. func jumpToDetailVc(type: Int, videoData: PQVideoListModel? = nil) {
  301. jumpType = type
  302. jumpVideoData = videoData
  303. // 无网
  304. if !isNetConnected() {
  305. PQUploadRemindView.showUploadRemindView(title:"似乎已断开与互联网的连接", summary: "", canMoreOpration:true,confirmTitle: "重新连接",cancelTitle:"取消", confirmColor:UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { sender, _ in
  306. if sender.tag == 2 {
  307. openAppSetting()
  308. }
  309. }
  310. return
  311. }
  312. // 先判断是否登录
  313. if !BFLoginUserInfo.shared.isLogin() {
  314. isJumpToLogin = true
  315. addNotification(self, selector: #selector(loginSuccesss), name: cLoginSuccesssNotiKey, object: nil)
  316. let vc = MVLoginController()
  317. let navigationController: UINavigationController = UINavigationController(rootViewController: vc)
  318. navigationController.modalPresentationStyle = .fullScreen
  319. present(navigationController, animated: true, completion: nil)
  320. return
  321. }
  322. // 用户被封禁
  323. BFLog(message: "userStatus is \(BFLoginUserInfo.shared.userStatus)")
  324. if BFLoginUserInfo.shared.userStatus == "3" {
  325. PQBandingPhoneRemindView.accountBlockRemindView(remindTitle: "无法上传视频")
  326. return
  327. }
  328. // 未绑定手机号
  329. BFLog(message: "phoneNumber is \(BFLoginUserInfo.shared.phoneNumber)")
  330. if BFLoginUserInfo.shared.phoneNumber.count <= 0 || BFLoginUserInfo.shared.phoneNumber == "<null>" {
  331. let remindView = PQBandingPhoneRemindView(frame: CGRect(x: 0, y: 0, width: cScreenWidth, height: cScreenHeigth))
  332. remindView.tag = cBandinPhoneTag
  333. remindView.bandingPhoneRemindHandle = { [weak self] sender in
  334. if sender.tag == 2 {
  335. self?.isJumpToLogin = true
  336. addNotification(self!, selector: #selector(self?.loginSuccesss), name: cBandingPhoneSuccessKey, object: nil)
  337. self?.navigationController?.pushViewController(PQBandingPhoneController(), animated: true)
  338. }
  339. }
  340. remindView.remindBlock = { [weak self] _, webUrl in
  341. let detail = PQBaseWebViewController()
  342. detail.baseUrl = webUrl
  343. self?.navigationController?.pushViewController(detail, animated: true)
  344. }
  345. view.addSubview(remindView)
  346. return
  347. }
  348. // 条件都满足转换到对就界面
  349. switch type {
  350. case 1:
  351. navigationController?.pushViewController(MVMineProductController(), animated: true)
  352. case 2:
  353. PQStuckPointViewModel.stuckPointProjectMusicInfo(projectId: videoData?.reCreateVideoData?.projectId ?? "") { [weak self] musicData, _ in
  354. if musicData != nil {
  355. let vc = PQStuckPointMaterialController()
  356. vc.reCreateMusicData = musicData
  357. vc.reCreateVideoData = videoData?.reCreateVideoData
  358. self?.navigationController?.pushViewController(vc, animated: true)
  359. // 关闭视频播放
  360. self?.controlPlayrPasueOrResume(isPause: true)
  361. } else { BFLog(message: "musicData is ni") }
  362. }
  363. case 3:
  364. let nav = UINavigationController(rootViewController: PQStuckPointMaterialController())
  365. nav.modalPresentationStyle = .fullScreen
  366. present(nav, animated: true, completion: nil)
  367. // 关闭视频播放
  368. controlPlayrPasueOrResume(isPause: true)
  369. default:
  370. break
  371. }
  372. }
  373. // 再创作视频
  374. func createVideo(indexPath: IndexPath) {
  375. if mAllVideos.count > indexPath.row {
  376. isCreateVideo = true
  377. let videoListModel = mAllVideos[indexPath.row]
  378. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_clickButton_syncedUpMusicRecreate, pageSource: .sp_shanyinApp_main, extParams: ["videoId":videoListModel.videoId], remindmsg: "")
  379. jumpToDetailVc(type: 2, videoData: videoListModel)
  380. } else {
  381. BFLog(message: "数据出错越界!!! mAllVideos.count: \(mAllVideos.count) indexPath.row: \(indexPath.row)")
  382. }
  383. }
  384. // 卡点模块消失
  385. @objc func stuckPointDismiss() {
  386. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) { [weak self] in
  387. self?.controlPlayrPasueOrResume(isPause: false)
  388. }
  389. }
  390. /// 获取用户状态信息
  391. /// - Returns: description
  392. func getUserInfo() {
  393. PQMineViewModel.userStatus { _, _ in
  394. }
  395. }
  396. }
  397. // MARK: - 播放器操作相关方法
  398. extension MVHomeController {
  399. /// 播放指定位置的视频
  400. /// - Parameter page: 视频数据位置
  401. func playVideo(page: Int) {
  402. BFLog(1, message: "aa : playVideo")
  403. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
  404. if !(UIViewController.getCurrentViewController()?.isMember(of: MVHomeController.self))! {
  405. BFLog(message: "当前显示的界面不是在首界面,不进行播放")
  406. return
  407. }
  408. DispatchQueue.main.async {
  409. let cell: MVBannerCell? = self.viewListView.collectionView.cellForItem(at: IndexPath(row: page, section: 0)) as? MVBannerCell
  410. let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
  411. if self.mVideos.count > 0, cell != nil, showProtocal != nil, showProtocal == "1" {
  412. cell?.pauseView.isHidden = true
  413. cell?.progressView.progress = 0
  414. let videoInfo = self.mAllVideos[page]
  415. videoInfo.playProgress = 0
  416. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonView, objectType: .ot_shanyinApp_viewButton_syncedUpMusicRecreate, pageSource: .sp_shanyinApp_main, extParams: ["videoId":videoInfo.videoId], remindmsg: "")
  417. let hotVideoIndex = self.mVideos.firstIndex(where: { (items) -> Bool in
  418. items.categoryName == videoInfo.categoryName
  419. })
  420. BFLog(message: "播放的视频分类为:\(String(describing: self.mVideos[hotVideoIndex ?? 0].categoryName)) hotVideoIndex :\(String(describing: hotVideoIndex)) page is \(page)")
  421. // 设置选中状态
  422. for hot in self.mVideos {
  423. hot.isSelected = false
  424. }
  425. self.mVideos[hotVideoIndex ?? 0].isSelected = true
  426. self.categoryCollectionView.reloadData()
  427. self.categoryCollectionView.scrollToItem(at: IndexPath(row: hotVideoIndex ?? 0, section: 0), at: .centeredHorizontally, animated: true)
  428. self.lastBnnerSelectIndex = IndexPath(row: page, section: 0)
  429. PQSingletoVideoPlayer.shared.configPlyer(videoData: videoInfo, controllerView: cell!.converView)
  430. self.controlPlayrPasueOrResume(isPause: false)
  431. } else {
  432. BFLog(message: "播放不成功: cell:\(String(describing: cell)) mVideos:\(self.mVideos.count) showProtocal: is \(String(describing: showProtocal))")
  433. }
  434. }
  435. }
  436. }
  437. // 控制播放器的暂停或恢复
  438. func controlPlayrPasueOrResume(isPause: Bool) {
  439. let cell: MVBannerCell? = viewListView.collectionView.cellForItem(at: lastBnnerSelectIndex) as? MVBannerCell
  440. BFLog(message: " player isPause is \(isPause)")
  441. if isPause {
  442. cell?.pauseView.isHidden = false
  443. PQSingletoVideoPlayer.shared.pausePlayer()
  444. } else {
  445. var rootViewController = UIApplication.shared.keyWindow?.rootViewController
  446. if rootViewController is UINavigationController {
  447. rootViewController = (rootViewController as? UINavigationController)?.visibleViewController
  448. }
  449. BFLog(message: "rootViewController is \(String(describing: rootViewController))")
  450. if cell != nil && cell?.videoData != nil{
  451. cell?.pauseView.isHidden = true
  452. if cell?.videoData?.videoId == PQSingletoVideoPlayer.shared.playVideoData?.videoId && PQSingletoVideoPlayer.shared.isPlayBegin {
  453. PQSingletoVideoPlayer.shared.resumePlayer()
  454. return
  455. }
  456. PQSingletoVideoPlayer.shared.configPlyer(videoData: (cell?.videoData)!, controllerView: cell!.converView)
  457. PQSingletoVideoPlayer.shared.startPlayr()
  458. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  459. BFLog(message: "duration \(duration) playProgress\(playProgress) \(duration) 进度\(playProgress / duration)")
  460. let cell: MVBannerCell? = self?.viewListView.collectionView.cellForItem(at: self?.lastBnnerSelectIndex ?? IndexPath()) as? MVBannerCell
  461. cell?.progressView.progress = playProgress / duration
  462. }
  463. PQSingletoVideoPlayer.shared.playStatusBloc = { [weak self] status in
  464. // 播放完成 自动播放下一个
  465. if status == .PQVIDEO_PLAY_STATUS_END {
  466. PQSingletoVideoPlayer.shared.playVideoData!.playProgress = 0
  467. if (self?.lastBnnerSelectIndex.row ?? 0) < (self?.mAllVideos.count ?? 0) - 1 {
  468. self?.lastBnnerSelectIndex = IndexPath(row: (self?.lastBnnerSelectIndex.row ?? 0) + 1, section: 0)
  469. // self?.playVideo(page: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row)
  470. // 视频列表跳转
  471. // self?.viewListView.currentIndex = (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row
  472. self?.viewListView.scrollToItem(at: (self?.lastBnnerSelectIndex ?? IndexPath(row: 0, section: 0)).row, animated: false)
  473. } else {
  474. BFLog(message: "已经是最后一个视频。")
  475. cell?.pauseView.isHidden = false
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. }
  483. // MARK: - 视频列表相关代理
  484. extension MVHomeController: MVBannerDataSource, MVBannerDelegate {
  485. func numberOfItems() -> Int {
  486. return mAllVideos.count
  487. }
  488. func banner(_ banner: MVBanner, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  489. let cell: MVBannerCell = banner.dequeueReusableCell(for: indexPath)
  490. cell.videoData = mAllVideos[indexPath.row]
  491. cell.reCreateBtnClicHandle = { [weak self] _, _ in
  492. BFLog(message: "点击了创同款")
  493. self?.createVideo(indexPath: indexPath)
  494. }
  495. cell.sharedBtnClicHandle = {[weak self, weak cell] type in
  496. if type == 1 {
  497. // 分享朋友圈
  498. if !isNetConnected() {
  499. cShowHUB(superView: self?.view, msg: "没有网络连接")
  500. return
  501. }
  502. if !PQSingletoWXApiUtil.shared.isInstallWX() {
  503. cShowHUB(superView: self?.view, msg: "您还未安装微信客户端!")
  504. return
  505. }
  506. let shareId = getUniqueId(desc: "\(cell?.videoData?.reCreateVideoData?.videoId ?? "")shareId")
  507. PQBaseViewModel.h5ShareLinkInfo(videoId: (cell?.videoData?.uniqueId ?? ""), pageSource: cell?.videoData?.pageSource ?? .sp_category) { [weak self] path, _ in
  508. cHiddenHUB(superView: nil)
  509. if path != nil {
  510. //判断shareText是否有效
  511. var shareText = ""
  512. shareText = cell?.videoData?.title?.replacingOccurrences(of: "\n", with: "") ?? ""
  513. shareText = shareText.replacingOccurrences(of: " ", with: "")
  514. if(shareText.count == 0){
  515. shareText = "\(BFLoginUserInfo.shared.nickName)made a music video for you"
  516. }
  517. PQSingletoWXApiUtil.shared.share(type: 1, scene: 1, title:shareText , description: "", imageUrl: cell?.videoData?.shareImgPath, path: path, videoId: (cell?.videoData?.uniqueId)!, pageSource: cell?.videoData?.pageSource ?? .sp_category, shareId: shareId).wxApiUtilHander = { _, _ in
  518. }
  519. } else {
  520. cShowHUB(superView: self?.view, msg: "没有网络连接")
  521. }
  522. }
  523. // 分享朋友圈上报
  524. self?.reportUpload(isFriend: false, openId: "", shareId: shareId, videoData: cell?.videoData!)
  525. }else if type == 2 {
  526. // 分享微信好友
  527. if !isNetConnected() {
  528. cShowHUB(superView: self?.view, msg: "没有网络连接")
  529. return
  530. }
  531. if !PQSingletoWXApiUtil.shared.isInstallWX() {
  532. cShowHUB(superView: self?.view, msg: "您还未安装微信客户端!")
  533. return
  534. }
  535. cShowHUB(superView: nil, msg: nil)
  536. let shareId = getUniqueId(desc: "\(cell?.videoData?.uniqueId ?? "")shareId")
  537. PQBaseViewModel.wxFriendShareInfo(videoId: (cell?.videoData?.uniqueId)!) { [weak self] imagePath, title, shareWeappRawId, msg in
  538. if msg != nil {
  539. cShowHUB(superView: self?.view, msg: "网络不佳哦")
  540. return
  541. }
  542. PQSingletoWXApiUtil.shared.share(type: 3, scene: 0, shareWeappRawId: shareWeappRawId, title: title, description: title, imageUrl: imagePath, path: cell?.videoData?.videoPath, videoId: (cell?.videoData?.uniqueId)!, pageSource: cell?.videoData?.pageSource ?? .sp_category, shareId: shareId).wxApiUtilHander = { _, _ in
  543. }
  544. cHiddenHUB(superView: nil)
  545. }
  546. // 分享好友上报
  547. self?.reportUpload(isFriend: true, openId: "", shareId: shareId, videoData: cell?.videoData!)
  548. }
  549. }
  550. return cell
  551. }
  552. func reportUpload(isFriend: Bool, openId _: String, shareId: String, videoData: PQVideoListModel?) {
  553. // 分享上报
  554. PQEventTrackViewModel.videoRelationReportUpload(reportLogType: .reportLogType_Action, videoData: videoData, pageSource: nil, businessType: isFriend ? .bt_videoShareFriend : .bt_videoShareH5, objectType: nil, extParams: nil, shareId: shareId, videoIds: nil, playId: PQSingletoVideoPlayer.shared.playId)
  555. if isFriend {
  556. PQEventTrackViewModel.shareReportUpload(videoId: videoData?.uniqueId ?? "0", pageSource: videoData!.pageSource, recommendId: videoData?.recommendId, recommendLogVO: videoData?.recommendLogVO, abInfoData: videoData?.abInfoData, measureType: videoData?.measureType, measureId: videoData?.measureId, businessType: isFriend ? .bt_videoShareFriend : .bt_videoShareH5, targetUid: videoData?.userInfo?.uid, shareId: shareId)
  557. PQEventTrackViewModel.shareReportUpload(screenType: 3, videoId: videoData?.uniqueId ?? "0", pageSource: videoData!.pageSource, recommendId: videoData?.recommendId, recommendLogVO: videoData?.recommendLogVO, abInfoData: videoData?.abInfoData, measureType: videoData?.measureType, measureId: videoData?.measureId, businessType: isFriend ? .bt_videoShareFriend : .bt_videoShareH5, targetUid: videoData?.userInfo?.uid, shareId: shareId)
  558. } else {
  559. PQEventTrackViewModel.shareReportUpload(screenType: 2, videoId: videoData?.uniqueId ?? "0", pageSource: videoData!.pageSource, recommendId: videoData?.recommendId, recommendLogVO: videoData?.recommendLogVO, abInfoData: videoData?.abInfoData, measureType: videoData?.measureType, measureId: videoData?.measureId, businessType: isFriend ? .bt_videoShareFriend : .bt_videoShareH5, targetUid: videoData?.userInfo?.uid, shareId: shareId)
  560. }
  561. }
  562. // MARK: - GXBannerDelegate
  563. func banner(_: MVBanner, didSelectItemAt indexPath: IndexPath) {
  564. NSLog("didSelectItemAt %d", indexPath.row)
  565. lastBnnerSelectIndex = indexPath
  566. controlPlayrPasueOrResume(isPause: PQSingletoVideoPlayer.shared.isPlaying)
  567. }
  568. func pageControl(currentPage page: Int) {
  569. BFLog(1, message: "page is \(page)")
  570. playVideo(page: page)
  571. }
  572. }
  573. // MARK: - 划动分类相关代理
  574. ///
  575. extension MVHomeController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  576. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  577. return mVideos.count
  578. }
  579. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  580. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  581. let cell = MVTagsCell.tagsCell(collectionView: collectionView, indexPath: indexPath)
  582. cell.bgmData = itemData
  583. return cell
  584. }
  585. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  586. let itemData: MVHotVideoModel = mVideos[indexPath.item]
  587. let width: CGFloat = CGFloat(CGFloat(itemData.categoryName.ga_widthForComment(font: UIFont.boldSystemFont(ofSize: 17), height: 17.0)) + 43)
  588. return CGSize(width: width, height: collectionView.frame.height - 14)
  589. }
  590. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  591. for hot in mVideos {
  592. hot.isSelected = false
  593. }
  594. let itemDataNew: MVHotVideoModel = mVideos[indexPath.item]
  595. itemDataNew.isSelected = true
  596. categoryCollectionView.reloadData()
  597. // 找出当前选择分类之前的视频数量和
  598. var videosCount: Int = 0
  599. if indexPath.row != 0 {
  600. for i in 0 ... indexPath.row - 1 {
  601. videosCount = videosCount + mVideos[i].videos.count
  602. }
  603. BFLog(message: "indexPath 选择\(indexPath.item) 之前的视频合\(videosCount)")
  604. }
  605. viewListView.currentIndex = videosCount
  606. viewListView.scrollToItem(at: videosCount, animated: false)
  607. self.playVideo(page: videosCount)
  608. }
  609. }
  610. extension UIViewController {
  611. /// 获取当前显示的VC
  612. ///
  613. /// - Returns: 当前屏幕显示的VC
  614. class func getCurrentViewController() -> UIViewController?{
  615. // 获取当先显示的window
  616. var currentWindow = UIApplication.shared.keyWindow ?? UIWindow()
  617. if currentWindow.windowLevel != UIWindow.Level.normal {
  618. let windowArr = UIApplication.shared.windows
  619. for window in windowArr {
  620. if window.windowLevel == UIWindow.Level.normal {
  621. currentWindow = window
  622. break
  623. }
  624. }
  625. }
  626. return UIViewController.getNextXController(nextController: currentWindow.rootViewController)
  627. }
  628. private class func getNextXController(nextController: UIViewController?) -> UIViewController? {
  629. if nextController == nil {
  630. return nil
  631. }else if nextController?.presentedViewController != nil {
  632. return UIViewController.getNextXController(nextController: nextController?.presentedViewController)
  633. }else if let tabbar = nextController as? UITabBarController {
  634. return UIViewController.getNextXController(nextController: tabbar.selectedViewController)
  635. }else if let nav = nextController as? UINavigationController {
  636. return UIViewController.getNextXController(nextController: nav.visibleViewController)
  637. }
  638. return nextController
  639. }
  640. }