TSDiscoverListCell.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // TSDiscoverListCell.swift
  3. // AIRingtone
  4. //
  5. // Created by 100Years on 2025/3/17.
  6. //
  7. class TSDiscoverListCell: TSBaseCollectionCell {
  8. static let cellID = "TSDiscoverListCell"
  9. lazy var setRingBtn: TSUIExpandedTouchButton = {
  10. let setRingBtn = TSUIExpandedTouchButton()
  11. setRingBtn.setImage(UIImage(named: "ai_setRing_icon"), for: .normal)
  12. return setRingBtn
  13. }()
  14. lazy var ringView: TSRingToneCellView = {
  15. let ringToneView = TSRingToneCellView()
  16. ringToneView.backgroundColor = .clear
  17. ringToneView.playBtn.addTarget(self, action: #selector(clickPlay), for: .touchUpInside)
  18. return ringToneView
  19. }()
  20. lazy var exampleView: UIView = {
  21. let exampleView = UIView()
  22. exampleView.backgroundColor = "#7E57F4".uiColor
  23. let textLabel = UILabel.createLabel(
  24. text: "Example".localized,
  25. font: .font(size: 12),
  26. textColor: .white
  27. )
  28. exampleView.addSubview(textLabel)
  29. textLabel.snp.makeConstraints { make in
  30. make.top.edges.equalTo(UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6))
  31. }
  32. kDelayMainShort {
  33. exampleView.makeCorner([.topLeft,.bottomLeft], radius: 10)
  34. }
  35. // exampleView.isHidden = true
  36. return exampleView
  37. }()
  38. var ringModel:TSRingModel?{
  39. didSet{
  40. guard let ringModel = ringModel else { return }
  41. ringView.timeLab.text = Float(ringModel.duration).floatToMinuteSecond()
  42. ringView.nameLab.text = ringModel.title
  43. ringView.setCoverImageView(urlString: "")
  44. exampleView.isHidden = true
  45. }
  46. }
  47. override var isSelected: Bool{
  48. didSet{
  49. ringView.isloading = isSelected
  50. if isSelected, self.ringView.isPlay == false{
  51. TSBusinessAudioPlayer.shared.playUrlString( ringModel?.audioUrl)
  52. }else{
  53. TSBusinessAudioPlayer.shared.stop()
  54. backgroundColor = .cardColor
  55. }
  56. }
  57. }
  58. override func creatUI() {
  59. cornerRadius = 16.0
  60. backgroundColor = .cardColor
  61. contentView.addSubview(exampleView)
  62. exampleView.snp.makeConstraints { make in
  63. make.trailing.equalToSuperview()
  64. make.top.equalTo(0)
  65. make.height.equalTo(20)
  66. }
  67. contentView.addSubview(ringView)
  68. ringView.snp.makeConstraints { make in
  69. make.edges.equalToSuperview()
  70. }
  71. contentView.addSubview(setRingBtn)
  72. setRingBtn.snp.makeConstraints { make in
  73. make.centerY.equalToSuperview()
  74. make.trailing.equalTo(-16)
  75. make.width.height.equalTo(20)
  76. }
  77. NotificationCenter.default.addObserver(forName: .kBusinessAudioStateChange, object: nil, queue: nil) { notification in
  78. if let userInfo = notification.userInfo as? [String: TSBusinessAudioPlayer.PlayerState], let state = userInfo["PlayerState"] {
  79. kExecuteOnMainThread {
  80. self.changePlayerState(state: state)
  81. }
  82. }
  83. }
  84. }
  85. override func dealThings() {
  86. }
  87. override func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
  88. super.renderView(with: object, component: component, attributes: attributes)
  89. if let itemModel = object as? TSColVVMItemModel{
  90. if let dataModel = itemModel.dataModel as? TSRingModel{
  91. ringModel = dataModel
  92. // if let ringModel = ringModel {
  93. // if ringModel.audioPlayerFileTool == nil {
  94. // ringModel.audioPlayerFileTool = TSAudioPlayerFileTool(urlString: ringModel.audioUrl)
  95. // }
  96. // }
  97. }
  98. }
  99. }
  100. deinit {
  101. NotificationCenter.default.removeObserver(self)
  102. }
  103. }
  104. extension TSDiscoverListCell{
  105. @objc func clickPlay(){
  106. }
  107. func changePlayerState(state:TSBusinessAudioPlayer.PlayerState){
  108. if self.isSelected == false {
  109. self.ringView.isPlay = false
  110. return
  111. }
  112. switch state {
  113. case .loading(let progress):
  114. if progress == 0.0 {
  115. self.ringView.isloading = true
  116. }else if progress == 1.0 {
  117. self.ringView.isloading = false
  118. }
  119. case .play:
  120. self.ringView.isPlay = true
  121. backgroundColor = "#3C213F".uiColor
  122. case .stop:
  123. self.ringView.isPlay = false
  124. backgroundColor = .cardColor
  125. default:
  126. break
  127. }
  128. }
  129. }