MVMineProductController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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: BFBaseViewController {
  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. BFLoadingHUB.shared.showHUB(superView:view)
  79. if isRefresh {
  80. pageNum = 1
  81. } else {
  82. pageNum = pageNum + 1
  83. }
  84. MVMineViewModel.userVideoListData(pageNum: pageNum) { [weak self] videoList, _ in
  85. if videoList != nil {
  86. if (videoList?.count ?? 0) > 0 {
  87. if isRefresh {
  88. self?.itemsList = videoList!
  89. } else {
  90. self?.itemsList = self!.itemsList + videoList!
  91. }
  92. if self?.itemsList != nil {
  93. self?.flowLayout.findList = self?.itemsList as! [Any]
  94. }
  95. self?.collectionView.reloadData()
  96. self?.emptyView.isHidden = true
  97. } else {
  98. self?.emptyView.isHidden = false
  99. }
  100. } else {
  101. cShowHUB(superView: nil, msg: "没有网络连接")
  102. self?.pageNum = (self?.pageNum ?? 1) - 1
  103. }
  104. if isRefresh {
  105. self?.collectionView.mj_header?.endRefreshing()
  106. self?.collectionView.mj_footer?.resetNoMoreData()
  107. } else {
  108. self?.collectionView.mj_footer?.endRefreshing()
  109. }
  110. if (videoList?.count ?? 0) < 20, videoList != nil {
  111. self?.collectionView.mj_footer?.endRefreshingWithNoMoreData()
  112. }
  113. BFLoadingHUB.shared.dismissHUB(superView:(self?.view ?? UIView()))
  114. }
  115. }
  116. }
  117. extension MVMineProductController: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
  118. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  119. return itemsList.count
  120. }
  121. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  122. let itemData = itemsList[indexPath.item]
  123. let cell = MVMineProductCell.productCell(collectionView: collectionView, indexPath: indexPath)
  124. cell.videoData = itemData
  125. cell.btnClickHandle = { [weak self] sender, videoData in
  126. self?.btnClickHandle(sender: sender, videoData: videoData, indexPath: indexPath)
  127. }
  128. return cell
  129. }
  130. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  131. if itemsList[indexPath.item].auditStatus != 5 || itemsList[indexPath.item].transcodeStatus != 3 {
  132. return
  133. }
  134. let playVC = MVPlayViewController()
  135. playVC.itemsList = itemsList
  136. playVC.currentIndex = indexPath
  137. playVC.pageNum = pageNum
  138. navigationController?.pushViewController(playVC, animated: true)
  139. }
  140. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  141. BFLog(message: "scrollView.contentOffset.y = \(scrollView.contentOffset.y)")
  142. if scrollView.contentOffset.y <= 0 {
  143. navTitleLabel?.textColor = UIColor(white: 0, alpha: 0)
  144. } else if scrollView.contentOffset.y >= headH {
  145. navTitleLabel?.textColor = UIColor(white: 0, alpha: 1)
  146. } else {
  147. var alpha: CGFloat = (scrollView.contentOffset.y / headH)
  148. if alpha < 0 {
  149. alpha = 0
  150. }
  151. if alpha > 1 {
  152. alpha = 1
  153. }
  154. navTitleLabel?.textColor = UIColor(white: 0, alpha: alpha)
  155. }
  156. }
  157. /// 处理按钮点击事件
  158. /// - Parameters:
  159. /// - sender: <#sender description#>
  160. /// - videoData: <#videoData description#>
  161. func btnClickHandle(sender: UIButton, videoData: PQVideoListModel?, indexPath: IndexPath) {
  162. switch sender.tag {
  163. case 1:
  164. let seleView = PQSelectedOprationView.showSelectedOprationView(itemList: ["删除视频"]) { sender in
  165. if sender.tag == 1 {
  166. if !isNetConnected() {
  167. cShowHUB(superView: nil, msg: "没有网络连接")
  168. return
  169. }
  170. PQBaseViewModel.deleteVideo(videoId: Int(videoData?.uniqueId ?? "0") ?? 0) { [weak self] isSuccess, videoId, _ in
  171. if isSuccess {
  172. self?.itemsList.removeAll(where: { tempVideo in
  173. tempVideo.uniqueId == "\(videoId)"
  174. })
  175. self?.flowLayout.findList = self?.itemsList as! [PQVideoListModel]
  176. self?.collectionView.reloadData()
  177. cShowHUB(superView: nil, msg: "删除成功")
  178. if (self?.itemsList.count ?? 0) <= 0 {
  179. self?.loadRequestData()
  180. }
  181. var videos: Int = (Int(BFLoginUserInfo.shared.videos) ?? 0) - 1
  182. if videos < 0 {
  183. videos = 0
  184. }
  185. BFLoginUserInfo.shared.videos = "\(videos)"
  186. self?.headInfoView.updateProducts()
  187. } else {
  188. cShowHUB(superView: nil, msg: "删除失败")
  189. }
  190. }
  191. }
  192. }
  193. seleView.contentView.backgroundColor = UIColor.hexColor(hexadecimal: "#F2F2F2")
  194. seleView.cancelBtn.backgroundColor = UIColor.white
  195. seleView.cancelBtn.setTitleColor(UIColor.black, for: .normal)
  196. seleView.cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
  197. let firstBtn: UIButton? = seleView.contentView.subviews[1] as? UIButton
  198. firstBtn?.backgroundColor = UIColor.white
  199. firstBtn?.setTitleColor(UIColor.hexColor(hexadecimal: "#FF0000"), for: .normal)
  200. firstBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
  201. seleView.contentView.subviews[2].backgroundColor = UIColor.hexColor(hexadecimal: "#F2F2F2")
  202. case 2:
  203. PQBaseViewModel.videoDetailInfo(videoId: videoData?.uniqueId ?? "") { [weak self] videoList, _, msg in
  204. if (videoList?.count ?? 0) > 0, msg == nil, !(videoList?.first?.first?.transcodeStatus == 2 || videoList?.first?.first?.auditStatus == 1 || videoList?.first?.first?.transcodeStatus == 1) {
  205. let tempVideoData = videoList?.first?.first
  206. if tempVideoData != nil {
  207. self?.itemsList[indexPath.item] = tempVideoData!
  208. }
  209. self?.collectionView.reloadData()
  210. self?.collectionView(self!.collectionView, didSelectItemAt: indexPath)
  211. } else {
  212. cShowHUB(superView: nil, msg: "请稍后再刷新")
  213. }
  214. }
  215. default:
  216. break
  217. }
  218. }
  219. @objc func btnClick(sender _: UIButton) {}
  220. }