LNIMChatVoiceMessageCell.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // LNIMChatVoiceMessageCell.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/5.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNIMChatVoiceMessageCell: LNIMChatBaseMessageCell {
  11. private let voiceView = UIStackView()
  12. private let controlIc = UIImageView()
  13. private let waveBg = UIImageView()
  14. private let indicator = UIView()
  15. private let waveBlack = UIImageView()
  16. private let waveProgress = UIView()
  17. private let durationLabel = UILabel()
  18. private let speedView = UIView()
  19. private let speedLabel = UILabel()
  20. private var curItem: LNIMMessageData?
  21. private var isPlaying: Bool {
  22. if let content = curItem?.content,
  23. content.lowercased().starts(with: "http") {
  24. return LNVoicePlayer.shared.playingUrl == content
  25. }
  26. guard let playingUrl = LNVoicePlayer.shared.playingUrl else {
  27. return false
  28. }
  29. return playingUrl == curItem?.imMessage.soundElem?.uuid
  30. }
  31. override func update(_ data: LNIMMessageData, viewModel: LNIMChatViewModel) {
  32. super.update(data, viewModel: viewModel)
  33. curItem = data
  34. if data.imMessage.isSelf {
  35. waveBg.image = .icImChatMyVoice
  36. waveBlack.image = .icImChatMyVoiceBlack
  37. } else {
  38. waveBg.image = .icImChatPeerVoice
  39. waveBlack.image = .icImChatPeerVoiceBlack
  40. }
  41. if isPlaying {
  42. updatePlayUI()
  43. } else {
  44. updateStopUI()
  45. }
  46. LNEventDeliver.addObserver(self)
  47. }
  48. private func updatePlayUI(animated: Bool = false) {
  49. guard let curItem else { return }
  50. controlIc.image = .icImChatVoicePause
  51. let currentTime = LNVoicePlayer.shared.currentTime + 0.25
  52. let duration = LNVoicePlayer.shared.duration
  53. durationLabel.text = .init(format: "%02d:%02d", Int(currentTime) / 60, Int(currentTime) % 60)
  54. if curItem.imMessage.isSelf {
  55. indicator.backgroundColor = .text_3
  56. } else {
  57. indicator.backgroundColor = .primary_3
  58. }
  59. waveProgress.snp.remakeConstraints { make in
  60. make.leading.verticalEdges.equalTo(waveBg)
  61. if duration == 0 || currentTime == 0 {
  62. make.width.equalTo(0)
  63. } else {
  64. make.width.equalTo(waveBg).multipliedBy(currentTime / Double(duration))
  65. }
  66. }
  67. if speedView.superview == nil {
  68. voiceView.addArrangedSubview(speedView)
  69. }
  70. speedLabel.text = "\(Int(LNVoicePlayer.shared.curSpeed))x"
  71. if animated {
  72. UIView.animate(withDuration: 0.25) { [weak self] in
  73. guard let self else { return }
  74. waveProgress.superview?.layoutIfNeeded()
  75. }
  76. }
  77. }
  78. private func updateStopUI() {
  79. guard let curItem else { return }
  80. controlIc.image = .icImChatVoicePlay
  81. durationLabel.text = .init(format: "%02d:%02d", curItem.voiceDuration / 60, curItem.voiceDuration % 60)
  82. if curItem.imMessage.isSelf {
  83. indicator.backgroundColor = .text_3
  84. } else {
  85. indicator.backgroundColor = .init(hex: "#1789FF")
  86. }
  87. waveProgress.snp.remakeConstraints { make in
  88. make.leading.verticalEdges.equalTo(waveBg)
  89. make.width.equalTo(0)
  90. }
  91. voiceView.removeArrangedSubview(speedView)
  92. speedView.removeFromSuperview()
  93. }
  94. override func setupViews() {
  95. LNEventDeliver.addObserver(self)
  96. super.setupViews()
  97. voiceView.axis = .horizontal
  98. voiceView.spacing = 8
  99. container.addSubview(voiceView)
  100. voiceView.snp.makeConstraints { make in
  101. make.edges.equalToSuperview()
  102. }
  103. controlIc.isUserInteractionEnabled = false
  104. voiceView.addArrangedSubview(controlIc)
  105. voiceView.addArrangedSubview(waveBg)
  106. waveProgress.clipsToBounds = true
  107. waveBg.addSubview(waveProgress)
  108. waveProgress.snp.makeConstraints { make in
  109. make.leading.verticalEdges.equalToSuperview()
  110. make.width.equalTo(0)
  111. }
  112. waveProgress.addSubview(waveBlack)
  113. waveBlack.snp.makeConstraints { make in
  114. make.leading.verticalEdges.equalToSuperview()
  115. }
  116. indicator.backgroundColor = .text_3
  117. indicator.layer.cornerRadius = 5
  118. waveBg.addSubview(indicator)
  119. indicator.snp.makeConstraints { make in
  120. make.centerX.equalTo(waveProgress.snp.trailing)
  121. make.centerY.equalToSuperview()
  122. make.width.height.equalTo(10)
  123. }
  124. durationLabel.font = .body_xs
  125. durationLabel.textColor = .text_3
  126. container.addSubview(durationLabel)
  127. durationLabel.snp.makeConstraints { make in
  128. make.leading.equalTo(waveBg)
  129. make.top.equalTo(container.snp.bottom).offset(4)
  130. }
  131. speedView.backgroundColor = .text_5.withAlphaComponent(0.4)
  132. speedView.layer.cornerRadius = 14
  133. speedView.snp.makeConstraints { make in
  134. make.height.equalTo(28)
  135. }
  136. speedLabel.font = .body_m
  137. speedLabel.textColor = .text_1
  138. speedView.addSubview(speedLabel)
  139. speedLabel.snp.makeConstraints { make in
  140. make.center.equalToSuperview()
  141. make.leading.greaterThanOrEqualToSuperview().offset(17.5)
  142. }
  143. speedView.onTap { [weak self] in
  144. guard self != nil else { return }
  145. if LNVoicePlayer.shared.curSpeed == 1.0 {
  146. LNVoicePlayer.shared.setSpeed(speed: 2.0)
  147. } else {
  148. LNVoicePlayer.shared.setSpeed(speed: 1.0)
  149. }
  150. }
  151. container.onTap { [weak self] in
  152. guard let self else { return }
  153. guard let curItem else { return }
  154. if let content = curItem.content,
  155. content.lowercased().starts(with: "http") {
  156. if LNVoicePlayer.shared.playingUrl == content {
  157. LNVoicePlayer.shared.stop()
  158. } else {
  159. LNVoicePlayer.shared.play(content)
  160. }
  161. return
  162. }
  163. if LNVoicePlayer.shared.playingUrl == curItem.imMessage.soundElem?.uuid {
  164. LNVoicePlayer.shared.stop()
  165. return
  166. }
  167. LNVoicePlayer.shared.playVoiceMessage(message: curItem)
  168. }
  169. }
  170. }
  171. extension LNIMChatVoiceMessageCell: LNVoicePlayerNotify {
  172. func onAudioStartPlay(path: String) {
  173. guard isPlaying else { return }
  174. updatePlayUI(animated: true)
  175. }
  176. func onAudioUpdateDuration(path: String, cur: TimeInterval, total: TimeInterval) {
  177. guard isPlaying else { return }
  178. updatePlayUI(animated: true)
  179. }
  180. func onAudioStopPlay(path: String) {
  181. guard isPlaying else { return }
  182. updateStopUI()
  183. }
  184. }