MVMineProductController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // MVMineProductController.swift
  3. // MusicVideoPlus
  4. //
  5. // Created by SanW on 2021/6/22.
  6. //
  7. import BFFramework
  8. import UIKit
  9. class MVMineProductController: PQBaseViewController {
  10. var itemsList: [PQVideoListModel] = Array<PQVideoListModel>.init()
  11. var pageNum: Int = 1
  12. let margin: CGFloat = 14
  13. let headH: CGFloat = cDefaultMargin * 9
  14. lazy var headInfoView: MVMineHeadInfoView = {
  15. let headInfoView = MVMineHeadInfoView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: headH), margin: 0)
  16. return headInfoView
  17. }()
  18. lazy var flowLayout: PQCollectionViewFlowlayout = {
  19. let flowLayout = PQCollectionViewFlowlayout()
  20. flowLayout.columnCount = 2
  21. flowLayout.sectionInset = .zero
  22. flowLayout.minimumLineSpacing = cDefaultMargin / 2
  23. flowLayout.minimumInteritemSpacing = cDefaultMargin / 2
  24. flowLayout.headerH = headH
  25. return flowLayout
  26. }()
  27. lazy var collectionView: UICollectionView = {
  28. let width = (cScreenWidth - 30) / 2
  29. let proCollectView = UICollectionView(frame: CGRect(x: margin, y: cDevice_iPhoneNavBarAndStatusBarHei, width: cScreenWidth - margin * 2, height: view.frame.height - cDevice_iPhoneNavBarAndStatusBarHei - cSafeAreaHeight), collectionViewLayout: flowLayout)
  30. proCollectView.register(MVMineProductCell.self, forCellWithReuseIdentifier: String(describing: MVMineProductCell.self))
  31. proCollectView.showsVerticalScrollIndicator = false
  32. proCollectView.delegate = self
  33. proCollectView.dataSource = self
  34. proCollectView.backgroundColor = UIColor.white
  35. proCollectView.addRefreshView { [weak self] isRefresh in
  36. self?.loadRequestData(isRefresh: isRefresh)
  37. }
  38. return proCollectView
  39. }()
  40. lazy var emptyView: MVProductEmptyView = {
  41. let emptyView = MVProductEmptyView(frame: CGRect(x: 0, y: headH, width: collectionView.frame.width, height: collectionView.frame.height - headH))
  42. emptyView.btnClickHandle = { [weak self] _ in
  43. let vc = PQStuckPointMaterialController()
  44. self?.navigationController?.pushViewController(vc, animated: true)
  45. }
  46. emptyView.isHidden = true
  47. return emptyView
  48. }()
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. view.addSubview(collectionView)
  52. collectionView.addSubview(emptyView)
  53. collectionView.addSubview(headInfoView)
  54. leftBackButton()
  55. loadRequestData()
  56. setTitle(title: BFLoginUserInfo.shared.nickName, color: UIColor(white: 0, alpha: 0))
  57. MVMineViewModel.userInfoData { [weak self] count, avatarUrl, nickName, msg in
  58. if count != nil {
  59. BFLoginUserInfo.shared.videos = "\(count ?? 0)"
  60. }
  61. if avatarUrl != nil, (avatarUrl?.count ?? 0) > 0 {
  62. BFLoginUserInfo.shared.avatarUrl = avatarUrl ?? ""
  63. }
  64. if nickName != nil, (nickName?.count ?? 0) > 0 {
  65. BFLoginUserInfo.shared.nickName = nickName ?? ""
  66. }
  67. if msg == nil {
  68. saveUserDefaults(key: cUserInfoStorageKey, value: BFLoginUserInfo.shared.toString())
  69. self?.headInfoView.addData()
  70. }
  71. }
  72. PQEventTrackViewModel.baseReportUpload(businessType: .bt_pageView, objectType: .ot_shanyinApp_viewPage_mineTab, pageSource: .sp_shanyinApp_mine, extParams: nil, remindmsg: "")
  73. }
  74. /// 请求网络数据
  75. /// - Parameter isRefresh: <#isRefresh description#>
  76. /// - Returns: <#description#>
  77. func loadRequestData(isRefresh: Bool = true) {
  78. if isRefresh {
  79. pageNum = 1
  80. } else {
  81. pageNum = pageNum + 1
  82. }
  83. MVMineViewModel.userVideoListData(pageNum: pageNum) { [weak self] videoList, _ in
  84. if videoList != nil {
  85. if (videoList?.count ?? 0) > 0 {
  86. if isRefresh {
  87. self?.itemsList = videoList!
  88. } else {
  89. self?.itemsList = self!.itemsList + videoList!
  90. }
  91. self?.flowLayout.findList = self?.itemsList as! [PQVideoListModel]
  92. self?.collectionView.reloadData()
  93. self?.emptyView.isHidden = true
  94. } else {
  95. self?.emptyView.isHidden = false
  96. }
  97. } else {
  98. cShowHUB(superView: nil, msg: "没有网络连接")
  99. self?.pageNum = (self?.pageNum ?? 1) - 1
  100. }
  101. if isRefresh {
  102. self?.collectionView.mj_header?.endRefreshing()
  103. self?.collectionView.mj_footer?.resetNoMoreData()
  104. } else {
  105. self?.collectionView.mj_footer?.endRefreshing()
  106. }
  107. if (videoList?.count ?? 0) < 20 {
  108. self?.collectionView.mj_footer?.endRefreshingWithNoMoreData()
  109. }
  110. }
  111. }
  112. }
  113. extension MVMineProductController: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
  114. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  115. return itemsList.count
  116. }
  117. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  118. let itemData = itemsList[indexPath.item]
  119. let cell = MVMineProductCell.productCell(collectionView: collectionView, indexPath: indexPath)
  120. cell.videoData = itemData
  121. cell.btnClickHandle = { [weak self] sender, videoData in
  122. self?.btnClickHandle(sender: sender, videoData: videoData, indexPath: indexPath)
  123. }
  124. return cell
  125. }
  126. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  127. if itemsList[indexPath.item].auditStatus != 5 || itemsList[indexPath.item].transcodeStatus != 3 {
  128. return
  129. }
  130. let playVC = MVPlayViewController()
  131. playVC.itemsList = itemsList
  132. playVC.currentIndex = indexPath
  133. playVC.pageNum = pageNum
  134. navigationController?.pushViewController(playVC, animated: true)
  135. }
  136. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  137. BFLog(message: "scrollView.contentOffset.y = \(scrollView.contentOffset.y)")
  138. if scrollView.contentOffset.y <= 0 {
  139. navTitleLabel?.textColor = UIColor(white: 0, alpha: 0)
  140. } else if scrollView.contentOffset.y >= headH {
  141. navTitleLabel?.textColor = UIColor(white: 0, alpha: 1)
  142. } else {
  143. var alpha: CGFloat = (scrollView.contentOffset.y / headH)
  144. if alpha < 0 {
  145. alpha = 0
  146. }
  147. if alpha > 1 {
  148. alpha = 1
  149. }
  150. navTitleLabel?.textColor = UIColor(white: 0, alpha: alpha)
  151. }
  152. }
  153. /// 处理按钮点击事件
  154. /// - Parameters:
  155. /// - sender: <#sender description#>
  156. /// - videoData: <#videoData description#>
  157. func btnClickHandle(sender: UIButton, videoData: PQVideoListModel?, indexPath: IndexPath) {
  158. switch sender.tag {
  159. case 1:
  160. let seleView = PQSelectedOprationView.showSelectedOprationView(itemList: ["删除视频"]) { sender in
  161. if sender.tag == 1 {
  162. if !isNetConnected() {
  163. cShowHUB(superView: nil, msg: "没有网络连接")
  164. return
  165. }
  166. PQBaseViewModel.deleteVideo(videoId: Int(videoData?.uniqueId ?? "0") ?? 0) { [weak self] isSuccess, videoId, _ in
  167. if isSuccess {
  168. self?.itemsList.removeAll(where: { tempVideo in
  169. tempVideo.uniqueId == "\(videoId)"
  170. })
  171. self?.flowLayout.findList = self?.itemsList as! [PQVideoListModel]
  172. self?.collectionView.reloadData()
  173. cShowHUB(superView: nil, msg: "删除成功")
  174. if (self?.itemsList.count ?? 0) <= 0 {
  175. self?.loadRequestData()
  176. }
  177. var videos: Int = (Int(BFLoginUserInfo.shared.videos) ?? 0) - 1
  178. if videos < 0 {
  179. videos = 0
  180. }
  181. BFLoginUserInfo.shared.videos = "\(videos)"
  182. self?.headInfoView.updateProducts()
  183. } else {
  184. cShowHUB(superView: nil, msg: "删除失败")
  185. }
  186. }
  187. }
  188. }
  189. seleView.contentView.backgroundColor = UIColor.hexColor(hexadecimal: "#F2F2F2")
  190. seleView.cancelBtn.backgroundColor = UIColor.white
  191. seleView.cancelBtn.setTitleColor(UIColor.black, for: .normal)
  192. seleView.cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
  193. let firstBtn: UIButton? = seleView.contentView.subviews[1] as? UIButton
  194. firstBtn?.backgroundColor = UIColor.white
  195. firstBtn?.setTitleColor(UIColor.hexColor(hexadecimal: "#FF0000"), for: .normal)
  196. firstBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
  197. seleView.contentView.subviews[2].backgroundColor = UIColor.hexColor(hexadecimal: "#F2F2F2")
  198. case 2:
  199. PQBaseViewModel.videoDetailInfo(videoId: videoData?.uniqueId ?? "") { [weak self] videoList, _, msg in
  200. if (videoList?.count ?? 0) > 0, msg == nil, !(videoList?.first?.first?.transcodeStatus == 2 || videoList?.first?.first?.auditStatus == 1 || videoList?.first?.first?.transcodeStatus == 1) {
  201. let tempVideoData = videoList?.first?.first
  202. videoData?.transcodeStatus = videoList?.first?.first?.transcodeStatus ?? 0
  203. videoData?.auditStatus = videoList?.first?.first?.auditStatus ?? 0
  204. videoData?.transcodeStatus = videoList?.first?.first?.transcodeStatus ?? 0
  205. self?.collectionView.reloadData()
  206. self?.collectionView(self!.collectionView, didSelectItemAt: indexPath)
  207. } else {
  208. cShowHUB(superView: nil, msg: "请稍后再刷新")
  209. }
  210. }
  211. default:
  212. break
  213. }
  214. }
  215. @objc func btnClick(sender _: UIButton) {}
  216. }