TSGenerateHistoryVC.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // TSGenerateHistoryVC.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/18.
  6. //
  7. import SwipeCellKit
  8. class TSGenerateHistoryVC: TSBaseVC {
  9. lazy var viewModel : TSGenerateHistoryVM = {
  10. let viewModel = TSGenerateHistoryVM()
  11. return viewModel
  12. }()
  13. var reloadUIBlock:(()->Void)?
  14. lazy var layout: UICollectionViewFlowLayout = {
  15. let layout = UICollectionViewFlowLayout()
  16. layout.scrollDirection = .vertical
  17. layout.itemSize = CGSize(width: k_ScreenWidth-32, height: 74)
  18. layout.minimumInteritemSpacing = 10.0
  19. layout.minimumLineSpacing = 18.0
  20. return layout
  21. }()
  22. lazy var collectionView: UICollectionView = {
  23. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  24. collectionView.delegate = self
  25. collectionView.dataSource = self
  26. collectionView.showsVerticalScrollIndicator = false
  27. collectionView.showsHorizontalScrollIndicator = false
  28. collectionView.backgroundColor = .clear
  29. collectionView.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
  30. collectionView.register(TSAIRintoneHistoryCell.self, forCellWithReuseIdentifier: TSAIRintoneHistoryCell.cellID)
  31. if #available(iOS 11.0, *) {
  32. collectionView.contentInsetAdjustmentBehavior = .never
  33. }
  34. return collectionView
  35. }()
  36. lazy var generalRintoneVC: TSTextGeneralRintoneVC = {
  37. let generalRintoneVC = TSTextGeneralRintoneVC()
  38. generalRintoneVC.reloadUIBlock = { [weak self] in
  39. guard let self = self else { return }
  40. viewModel.updateRecentData()
  41. updateListView()
  42. }
  43. return generalRintoneVC
  44. }()
  45. override func createView() {
  46. setViewBgImageNamed(named: kViewBJ)
  47. addNormalNavBarView()
  48. setPageTitle("Generate History".localized)
  49. contentView.addSubview(collectionView)
  50. collectionView.snp.makeConstraints { make in
  51. make.edges.equalToSuperview()
  52. }
  53. }
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. }
  57. override func viewWillDisappear(_ animated: Bool) {
  58. super.viewWillDisappear(animated)
  59. TSBusinessAudioPlayer.shared.stop()
  60. }
  61. override func dealThings() {
  62. updateListView()
  63. NotificationCenter.default.addObserver(forName: .kGenerateRintoneOperationChanged, object: nil, queue: nil) { notification in
  64. if let userInfo = notification.userInfo as? [String: Any],let state = userInfo["state"] as? TSProgressState {
  65. switch state {
  66. case .start, .success(_),.failed(_),.none:
  67. self.viewModel.updateRecentData()
  68. self.updateListView()
  69. default:break
  70. }
  71. }
  72. }
  73. }
  74. func updateListView(){
  75. collectionView.reloadData()
  76. }
  77. override func navBarClickLeftAction() {
  78. super.navBarClickLeftAction()
  79. self.reloadUIBlock?()
  80. }
  81. }
  82. extension TSGenerateHistoryVC: UICollectionViewDataSource ,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
  83. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  84. return viewModel.modelList.count
  85. }
  86. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  87. if let sectionModel = viewModel.modelList.safeObj(At: section) {
  88. return sectionModel.list.count
  89. }
  90. return 0
  91. }
  92. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  93. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TSAIRintoneHistoryCell.cellID, for: indexPath)
  94. if let sectionModel = viewModel.modelList.safeObj(At: indexPath.section),
  95. let itemModel = sectionModel.list.safeObj(At: indexPath.item),
  96. let cell = cell as? TSAIRintoneHistoryCell
  97. {
  98. cell.delegate = self
  99. cell.setRingBtn.indexPath = indexPath
  100. cell.setRingBtn.addTarget(self, action: #selector(clickSetRingBtn(_ :)), for: .touchUpInside)
  101. if let model = itemModel as? TSActionInfoModel {
  102. cell.model = model
  103. }else if let ringModel = itemModel as? TSRingModel {
  104. cell.ringModel = ringModel
  105. }
  106. }
  107. return cell
  108. }
  109. @objc func clickSetRingBtn(_ btn:TSUIExpandedTouchButton){
  110. let indexPath = btn.indexPath
  111. if let sectionModel = self.viewModel.modelList.safeObj(At: indexPath.section),
  112. let model = sectionModel.list.safeObj(At: indexPath.item){
  113. if let model = model as? TSActionInfoModel {
  114. _ = kPurchaseToolShared.kshareBand(needVip: model.response.vip, vc: self, urlString: model.response.musicUrl, fileName: model.response.title)
  115. }else if let ringModel = model as? TSRingModel {
  116. _ = kPurchaseToolShared.kshareBand(needVip: ringModel.vip, vc: self, urlString: ringModel.audioUrl, fileName: ringModel.title)
  117. // { success in
  118. // if success {
  119. // TSMineRintoneHistory.saveModel(model: ringModel)
  120. // }
  121. // }
  122. }
  123. }
  124. TSBusinessAudioPlayer.shared.stop()
  125. }
  126. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  127. }
  128. }
  129. extension TSGenerateHistoryVC: SwipeCollectionViewCellDelegate {
  130. func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
  131. guard orientation == .right else { return nil }
  132. if indexPath.section != 0 {
  133. return nil
  134. }
  135. if let sectionModel = self.viewModel.modelList.safeObj(At: indexPath.section),
  136. let model = sectionModel.list.safeObj(At: indexPath.item){
  137. if let model = model as? TSActionInfoModel {
  138. switch model.actionStatus {
  139. case .pending,.running:
  140. return nil
  141. default:break
  142. }
  143. }
  144. }
  145. // 删除操作
  146. let deleteAction = SwipeAction(style: .destructive, title: nil) {[weak self] action, indexPath in
  147. guard let self = self else { return }
  148. showCustomAlert(message: "Are you sure to delete".localized, deleteHandler: {
  149. if let sectionModel = self.viewModel.modelList.safeObj(At: indexPath.section),
  150. let model = sectionModel.list.safeObj(At: indexPath.item){
  151. if let model = model as? TSActionInfoModel {
  152. // collectionView.performBatchUpdates({
  153. // self.viewModel.removeModel(model: model)
  154. // if sectionModel.list.count == 0 {
  155. // collectionView.deleteSections([indexPath.section])
  156. // }else{
  157. // collectionView.deleteItems(at: [indexPath])
  158. // }
  159. // })
  160. self.viewModel.removeModel(model: model)
  161. collectionView.reloadData()
  162. TSBusinessAudioPlayer.shared.stop()
  163. }
  164. }
  165. })
  166. }
  167. deleteAction.backgroundColor = "#E83E3E".uiColor
  168. deleteAction.image = UIImage(named: "delete_white")
  169. return [deleteAction]
  170. }
  171. func collectionView(_ collectionView: UICollectionView, editActionsOptionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
  172. var options = SwipeOptions()
  173. // options.expansionStyle = .destructive(automaticallyDelete: false) // 完全滑动时是否自动触发操作
  174. options.transitionStyle = .border // 滑动动画样式
  175. return options
  176. }
  177. }