MVHomeController.swift 34 KB

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