TSDiscoverListVC.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // TSDiscoverListVC.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/17.
  6. //
  7. class TSDiscoverListVC: TSBaseVC {
  8. var reloadUIBlock:(()->Void)?
  9. var ringCategoryModel:TSRingCategoryModel
  10. init(ringCategoryModel: TSRingCategoryModel) {
  11. self.ringCategoryModel = ringCategoryModel
  12. super.init()
  13. }
  14. @MainActor required init?(coder: NSCoder) {
  15. fatalError("init(coder:) has not been implemented")
  16. }
  17. lazy var viewModel:TSDiscoverListVM = {
  18. let viewModel = TSDiscoverListVM(categoryID:ringCategoryModel.categoryId,animationView: self.view)
  19. return viewModel
  20. }()
  21. lazy var layout: UICollectionViewFlowLayout = {
  22. let layout = UICollectionViewFlowLayout()
  23. layout.scrollDirection = .vertical
  24. layout.itemSize = CGSize(width: k_ScreenWidth-32, height: 74)
  25. layout.minimumInteritemSpacing = 10.0
  26. layout.minimumLineSpacing = 18.0
  27. return layout
  28. }()
  29. lazy var collectionView: UICollectionView = {
  30. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  31. collectionView.delegate = self
  32. collectionView.dataSource = self
  33. collectionView.showsVerticalScrollIndicator = false
  34. collectionView.showsHorizontalScrollIndicator = false
  35. collectionView.backgroundColor = .clear
  36. collectionView.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
  37. collectionView.register(TSAIRintoneHistoryCell.self, forCellWithReuseIdentifier: TSAIRintoneHistoryCell.cellID)
  38. if #available(iOS 11.0, *) {
  39. collectionView.contentInsetAdjustmentBehavior = .never
  40. }
  41. collectionView.addRefresh{ [weak self] in
  42. guard let self = self else { return }
  43. TSBusinessAudioPlayer.shared.stop()
  44. viewModel.reloadAllData { [weak self] success in
  45. guard let self = self else { return }
  46. if success {
  47. updateListView()
  48. }
  49. collectionView.endRefreshing()
  50. }
  51. }
  52. collectionView.addLoadMore { [weak self] in
  53. guard let self = self else { return }
  54. viewModel.getMoreData { [weak self] success,haveMore in
  55. guard let self = self else { return }
  56. if success {
  57. updateListView()
  58. }
  59. collectionView.endRefreshing(noMore: !haveMore)
  60. }
  61. }
  62. return collectionView
  63. }()
  64. override func createView() {
  65. setViewBgImageNamed(named: kViewBJ)
  66. addNormalNavBarView()
  67. setPageTitle(ringCategoryModel.title)
  68. contentView.addSubview(collectionView)
  69. collectionView.snp.makeConstraints { make in
  70. make.edges.equalToSuperview()
  71. }
  72. }
  73. override func viewWillAppear(_ animated: Bool) {
  74. super.viewWillAppear(animated)
  75. }
  76. override func viewWillDisappear(_ animated: Bool) {
  77. super.viewWillDisappear(animated)
  78. TSBusinessAudioPlayer.shared.stop()
  79. }
  80. override func dealThings() {
  81. viewModel.getData {[weak self] success in
  82. guard let self = self else { return }
  83. if success {
  84. updateListView()
  85. }
  86. }
  87. }
  88. override func navBarClickLeftAction() {
  89. super.navBarClickLeftAction()
  90. reloadUIBlock?()
  91. }
  92. func updateListView(){
  93. collectionView.reloadData()
  94. }
  95. }
  96. extension TSDiscoverListVC: UICollectionViewDataSource ,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
  97. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  98. return viewModel.modelList.count
  99. }
  100. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  101. if let sectionModel = viewModel.modelList.safeObj(At: section) {
  102. return sectionModel.list.count
  103. }
  104. return 0
  105. }
  106. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  107. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSAIRintoneHistoryCell.cellID, for: indexPath)
  108. if let sectionModel = viewModel.modelList.safeObj(At: indexPath.section),
  109. let itemModel = sectionModel.list.safeObj(At: indexPath.item),
  110. let cell = cell as? TSAIRintoneHistoryCell
  111. {
  112. cell.setTargetVC(targetVC: self, indexPath: indexPath)
  113. // cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
  114. if let model = itemModel as? TSActionInfoModel {
  115. cell.model = model
  116. cell.ringModel = nil
  117. }else if let ringModel = itemModel as? TSRingModel {
  118. cell.model = nil
  119. cell.ringModel = ringModel
  120. }
  121. }
  122. return cell
  123. }
  124. // @objc func clickSetRingBtn(_ btn:TSUIExpandedTouchButton){
  125. // let indexPath = btn.indexPath
  126. // if let sectionModel = self.viewModel.modelList.safeObj(At: indexPath.section),
  127. // let itemModel = sectionModel.list.safeObj(At: indexPath.item){
  128. //// if let model = itemModel as? TSActionInfoModel {
  129. //// _ = kPurchaseToolShared.kshareBand(needVip: model.response.vip, vc: self, urlString: model.response.musicUrl, fileName: model.response.title)
  130. //// }else
  131. // if let ringModel = itemModel as? TSRingModel {
  132. // _ = kPurchaseToolShared.kshareBand(needVip: ringModel.vip, vc: self, urlString: ringModel.audioUrl, fileName: ringModel.title){ success in
  133. // if success {
  134. // TSMineRintoneHistory.shared.saveModel(model: ringModel)
  135. // }
  136. // }
  137. // }
  138. // }
  139. // TSBusinessAudioPlayer.shared.stop()
  140. // }
  141. }