ASMyRingVC.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // APMyRingVC.swift
  3. // AIPlayRingtones
  4. //
  5. // Created by 100Years on 2025/5/15.
  6. //
  7. import SwipeCellKit
  8. class APMyRingVC: TSBaseVC {
  9. var listModelArray: [ASActionInfoModel] = []
  10. var dataChangedBlock:(()->Void)?
  11. lazy var navBarView: TSBaseNavContentBarView = {
  12. let navBarView = TSBaseNavContentBarView()
  13. let titleImageView = UIImageView.createImageView(imageName: "nav_title_myRingtones", contentMode: .scaleToFill)
  14. navBarView.barView.addSubview(titleImageView)
  15. titleImageView.snp.makeConstraints { make in
  16. make.center.equalToSuperview()
  17. }
  18. // let vipBtn = UIButton.createButton(image: .aiRintoneIcon) { [weak self] in
  19. // guard let self = self else { return }
  20. //
  21. // if let model = dbHistory.listModels.first,model.modelType != 1 {
  22. // ASRMShared.writeThread {
  23. // model.status = "failed"
  24. // }
  25. // }
  26. //
  27. // updateListView()
  28. // }
  29. // navBarView.barView.addSubview(vipBtn)
  30. // vipBtn.snp.makeConstraints { make in
  31. // make.centerY.equalToSuperview()
  32. // make.trailing.equalTo(-16) // (-60)
  33. // make.width.height.equalTo(24)
  34. // }
  35. return navBarView
  36. }()
  37. lazy var layout: UICollectionViewFlowLayout = {
  38. let layout = UICollectionViewFlowLayout()
  39. layout.scrollDirection = .vertical
  40. layout.itemSize = CGSize(width: k_ScreenWidth-32, height: 74)
  41. layout.minimumInteritemSpacing = 10.0
  42. layout.minimumLineSpacing = 18.0
  43. return layout
  44. }()
  45. lazy var collectionView: UICollectionView = {
  46. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  47. collectionView.delegate = self
  48. collectionView.dataSource = self
  49. collectionView.showsVerticalScrollIndicator = false
  50. collectionView.showsHorizontalScrollIndicator = false
  51. collectionView.backgroundColor = .clear
  52. collectionView.contentInset = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
  53. collectionView.register(ASMyRingCell.self, forCellWithReuseIdentifier: ASMyRingCell.cellID)
  54. if #available(iOS 11.0, *) {
  55. collectionView.contentInsetAdjustmentBehavior = .never
  56. }
  57. return collectionView
  58. }()
  59. lazy var pageNullView: ASPageNullView = {
  60. let pageNullView = ASPageNullView()
  61. pageNullView.isHidden = true
  62. return pageNullView
  63. }()
  64. override func createView() {
  65. navBarContentView.addSubview(navBarView)
  66. navBarView.snp.makeConstraints { make in
  67. make.edges.equalToSuperview()
  68. }
  69. contentView.addSubview(pageNullView)
  70. pageNullView.snp.makeConstraints { make in
  71. make.edges.equalToSuperview()
  72. }
  73. contentView.addSubview(collectionView)
  74. collectionView.snp.makeConstraints { make in
  75. make.edges.equalToSuperview()
  76. }
  77. }
  78. override func viewWillAppear(_ animated: Bool) {
  79. super.viewWillAppear(animated)
  80. debugPrint("viewWillAppear listModels.count = \(ASRMShared.ringDBHistory.listModels.count)")
  81. }
  82. override func viewWillDisappear(_ animated: Bool) {
  83. super.viewWillDisappear(animated)
  84. TSBusinessAudioPlayer.shared.stop()
  85. }
  86. override func dealThings() {
  87. updateListView()
  88. NotificationCenter.default.addObserver(self, selector: #selector(operationChanged(_:)), name: .kGenerateRingOperationChanged, object: nil)
  89. NotificationCenter.default.addObserver(self, selector: #selector(operationChanged(_:)), name: .kGenerateRTROperationChanged, object: nil)
  90. NotificationCenter.default.addObserver(self, selector: #selector(operationChanged(_:)), name: .kGenerateTTROperationChanged, object: nil)
  91. NotificationCenter.default.addObserver(self, selector: #selector(updateListView), name: .kRingDataChanged, object: nil)
  92. }
  93. @objc func operationChanged(_ notification: Notification) {
  94. if let userInfo = notification.userInfo as? [String: Any],let state = userInfo["state"] as? ASProgressState {
  95. if state.reloadNewData {
  96. self.updateListView()
  97. }
  98. }
  99. }
  100. @objc func updateListView(){
  101. dePrint("dbHistory.getModelList拿数据前,\(dbHistory.listModels.count))")
  102. dbHistory.getModelList { [weak self] array in
  103. guard let self = self else { return }
  104. dePrint("dbHistory.getModelList拿数据后,\(dbHistory.listModels.count))")
  105. listModelArray = array
  106. updateView()
  107. }
  108. }
  109. func updateView() {
  110. collectionView.reloadData()
  111. pageNullView.isHidden = listModelArray.count > 0
  112. }
  113. }
  114. extension APMyRingVC: UICollectionViewDataSource ,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
  115. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  116. return 1
  117. }
  118. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  119. return listModelArray.count
  120. }
  121. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  122. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ASMyRingCell.cellID, for: indexPath) as! ASMyRingCell
  123. if let model = listModelArray.safeObj(At: indexPath.item){
  124. cell.setTargetVC(targetVC: self, indexPath: indexPath)
  125. cell.model = model
  126. }
  127. return cell
  128. }
  129. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  130. }
  131. }
  132. extension APMyRingVC{
  133. var dbHistory:ASDBHistory{
  134. return ASRMShared.ringDBHistory
  135. }
  136. }
  137. extension APMyRingVC: SwipeCollectionViewCellDelegate {
  138. func collectionView(_ collectionView: UICollectionView, editActionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
  139. guard orientation == .right else { return nil }
  140. if indexPath.section != 0 {
  141. return nil
  142. }
  143. if let model = listModelArray.safeObj(At: indexPath.item) {
  144. switch model.actionStatus {
  145. case .pending,.running:
  146. return nil
  147. default:break
  148. }
  149. }
  150. // 删除操作
  151. let deleteAction = SwipeAction(style: .destructive, title: nil) {[weak self] action, indexPath in
  152. guard let self = self else { return }
  153. showCustomAlert(message: "Are you sure to delete".localized, deleteHandler: { [weak self] in
  154. guard let self = self else { return }
  155. if let model = listModelArray.safeObj(At: indexPath.item) {
  156. dbHistory.deleteListModel(id: model.id)
  157. updateListView()
  158. TSBusinessAudioPlayer.shared.stop()
  159. }
  160. })
  161. }
  162. deleteAction.backgroundColor = "#E83E3E".uiColor
  163. deleteAction.image = UIImage(named: "delete_white")
  164. return [deleteAction]
  165. }
  166. func collectionView(_ collectionView: UICollectionView, editActionsOptionsForItemAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
  167. var options = SwipeOptions()
  168. options.transitionStyle = .border // 滑动动画样式
  169. return options
  170. }
  171. }