LNGameMateListCell.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. //
  2. // LNGameMateListCell.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/16.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import Combine
  11. class LNGameMateListCell: UITableViewCell {
  12. private let avatar = UIImageView()
  13. private let onlineView = LNOnlineView()
  14. private let playButton = UIButton()
  15. private let voiceLabel = UILabel()
  16. private let voiceWaveView = LNVoiceWaveView()
  17. private let priceLabel = UILabel()
  18. private let unitLabel = UILabel()
  19. private let saleView = UIView()
  20. private let discountView = LNNewbieDiscountView()
  21. private let nameLabel = UILabel()
  22. private let genderView = LNGenderView()
  23. private let newbieView = UIView()
  24. private let scoreView = UIView()
  25. private let scoreLabel = UILabel()
  26. private let orderCountLabel = UILabel()
  27. private let gameIc = UIImageView()
  28. private let gameNameLanel = UILabel()
  29. private let bioLabel = UILabel()
  30. private let maxShow = 3
  31. private let photoView = UIStackView()
  32. private var photos: [UIImageView] = []
  33. private var curItem: LNGameMateListItemVO?
  34. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  35. super.init(style: style, reuseIdentifier: reuseIdentifier)
  36. setupViews()
  37. LNEventDeliver.addObserver(self)
  38. }
  39. func update(_ item: LNGameMateListItemVO) {
  40. avatar.showAvatar(item.avatar)
  41. onlineView.isHidden = !item.online
  42. priceLabel.text = item.price.toDisplay
  43. unitLabel.text = "/\(item.unit)"
  44. nameLabel.text = item.nickname
  45. genderView.update(item.gender, item.age)
  46. scoreLabel.text = "\(item.star)"
  47. orderCountLabel.text = .init(key: "A00037", item.orderCount)
  48. gameIc.sd_setImage(with: URL(string: item.categoryIcon))
  49. gameNameLanel.text = item.categoryName
  50. bioLabel.text = item.summary
  51. bioLabel.superview?.isHidden = item.summary.isEmpty
  52. newbieView.isHidden = !item.newcomer
  53. if item.images.isEmpty {
  54. photoView.isHidden = true
  55. bioLabel.numberOfLines = 4
  56. } else {
  57. for index in 0..<maxShow {
  58. if index < item.images.count {
  59. photos[index].sd_setImage(with: URL(string: item.images[index]))
  60. } else {
  61. photos[index].sd_setImage(with: nil)
  62. }
  63. }
  64. photoView.isHidden = false
  65. bioLabel.numberOfLines = 1
  66. }
  67. if let curItem {
  68. LNVoiceResourceManager.shared.cancelLoadingAsset(urlStr: curItem.voiceBar)
  69. }
  70. curItem = item
  71. if LNVoicePlayer.shared.playingUrl == curItem?.voiceBar {
  72. voiceWaveView.startAnimate()
  73. } else {
  74. voiceWaveView.stopAnimate()
  75. }
  76. playButton.isHidden = true
  77. if !item.voiceBar.isEmpty {
  78. LNVoiceResourceManager.shared.getRemoteAudioDuration(urlStr: item.voiceBar)
  79. { [weak self] duration, error in
  80. guard let self else { return }
  81. guard let duration, error == nil else { return }
  82. guard curItem?.id == item.id else { return }
  83. voiceLabel.text = duration.durationDisplay
  84. playButton.isHidden = false
  85. }
  86. }
  87. updateDiscount()
  88. }
  89. required init?(coder: NSCoder) {
  90. fatalError("init(coder:) has not been implemented")
  91. }
  92. }
  93. extension LNGameMateListCell: LNVoicePlayerNotify {
  94. func onAudioStartPlay(path: String) {
  95. guard path == curItem?.voiceBar else { return }
  96. voiceWaveView.startAnimate()
  97. }
  98. func onAudioStopPlay(path: String) {
  99. guard path == curItem?.voiceBar else { return }
  100. voiceWaveView.stopAnimate()
  101. let url = curItem?.voiceBar
  102. LNVoiceResourceManager.shared.getRemoteAudioDuration(urlStr: curItem?.voiceBar)
  103. { [weak self] duration, err in
  104. guard let self else { return }
  105. guard let duration, err == nil else { return }
  106. guard curItem?.voiceBar == url else { return }
  107. voiceLabel.text = duration.durationDisplay
  108. }
  109. }
  110. func onAudioUpdateDuration(path: String, cur: TimeInterval, total: TimeInterval) {
  111. guard path == curItem?.voiceBar else { return }
  112. voiceLabel.text = (total - cur).durationDisplay
  113. }
  114. }
  115. extension LNGameMateListCell: LNOrderManagerNotify {
  116. func onMyDiscountInfoChanged(info: LNOrderDiscountVO?) {
  117. updateDiscount()
  118. }
  119. private func updateDiscount() {
  120. guard let curItem else { return }
  121. if curItem.userNo.isMyUid == false,
  122. let discount = LNOrderManager.shared.discountFor(curItem.price) {
  123. saleView.isHidden = false
  124. discountView.update(1 - discount)
  125. priceLabel.textColor = .init(hex: "#FF6F32")
  126. priceLabel.text = (curItem.price * discount).toDisplay
  127. } else {
  128. saleView.isHidden = true
  129. priceLabel.textColor = .text_5
  130. priceLabel.text = curItem.price.toDisplay
  131. }
  132. }
  133. }
  134. extension LNGameMateListCell {
  135. private func setupViews() {
  136. backgroundColor = .clear
  137. let container = UIView()
  138. container.onTap { [weak self] in
  139. guard let self else { return }
  140. guard let curItem else { return }
  141. pushToSkillDetail(curItem.id)
  142. }
  143. contentView.addSubview(container)
  144. container.snp.makeConstraints { make in
  145. make.leading.equalToSuperview().offset(16)
  146. make.trailing.equalToSuperview().offset(-16)
  147. make.top.equalToSuperview()
  148. make.bottom.equalToSuperview().offset(-10).priority(.medium)
  149. }
  150. let bg = UIImageView()
  151. bg.image = .icGameMateListCellBg.resizableImage(
  152. withCapInsets: .init(top: 48, left: 53, bottom: 12, right: 17), resizingMode: .stretch
  153. )
  154. container.addSubview(bg)
  155. bg.snp.makeConstraints { make in
  156. make.edges.equalToSuperview()
  157. }
  158. let avatar = buildAvatar()
  159. container.addSubview(avatar)
  160. avatar.snp.makeConstraints { make in
  161. make.leading.equalToSuperview().offset(13)
  162. make.top.equalToSuperview().offset(12)
  163. make.bottom.lessThanOrEqualToSuperview().offset(-12)
  164. }
  165. let stackView = UIStackView()
  166. stackView.axis = .vertical
  167. stackView.spacing = 6
  168. container.addSubview(stackView)
  169. stackView.snp.makeConstraints { make in
  170. make.leading.equalTo(avatar.snp.trailing).offset(14)
  171. make.trailing.equalToSuperview().offset(-12)
  172. make.top.equalToSuperview().offset(10)
  173. make.bottom.lessThanOrEqualToSuperview().offset(-10)
  174. }
  175. stackView.addArrangedSubview(buildTextViews())
  176. stackView.addArrangedSubview(buildPhotos())
  177. }
  178. private func buildAvatar() -> UIView {
  179. let container = UIView()
  180. avatar.layer.cornerRadius = 38
  181. avatar.clipsToBounds = true
  182. avatar.contentMode = .scaleAspectFill
  183. container.addSubview(avatar)
  184. avatar.snp.makeConstraints { make in
  185. make.width.height.equalTo(76)
  186. make.top.equalToSuperview()
  187. make.horizontalEdges.equalToSuperview()
  188. }
  189. onlineView.offset = 5
  190. container.addSubview(onlineView)
  191. onlineView.snp.makeConstraints { make in
  192. make.edges.equalTo(avatar).inset(-2)
  193. }
  194. playButton.setBackgroundImage(.primary_7, for: .normal)
  195. playButton.layer.cornerRadius = 11
  196. playButton.clipsToBounds = true
  197. playButton.addAction(UIAction(handler: { [weak self] _ in
  198. guard let self else { return }
  199. guard let curItem, !curItem.voiceBar.isEmpty else { return }
  200. if LNVoicePlayer.shared.playingUrl == curItem.voiceBar {
  201. LNVoicePlayer.shared.stop()
  202. } else {
  203. LNVoicePlayer.shared.play(curItem.voiceBar)
  204. }
  205. }), for: .touchUpInside)
  206. container.addSubview(playButton)
  207. playButton.snp.makeConstraints { make in
  208. make.centerX.equalToSuperview()
  209. make.bottom.equalTo(avatar).offset(10)
  210. make.width.equalTo(64)
  211. make.height.equalTo(22)
  212. }
  213. let voice = UIView()
  214. voice.isUserInteractionEnabled = false
  215. playButton.addSubview(voice)
  216. voice.snp.makeConstraints { make in
  217. make.center.equalToSuperview()
  218. }
  219. voiceWaveView.build()
  220. voice.addSubview(voiceWaveView)
  221. voiceWaveView.snp.makeConstraints { make in
  222. make.leading.centerY.equalToSuperview()
  223. make.width.equalTo(19)
  224. make.height.equalTo(11)
  225. }
  226. voiceLabel.font = .heading_h5
  227. voiceLabel.textColor = .text_1
  228. voice.addSubview(voiceLabel)
  229. voiceLabel.snp.makeConstraints { make in
  230. make.verticalEdges.equalToSuperview()
  231. make.trailing.equalToSuperview()
  232. make.leading.equalTo(voiceWaveView.snp.trailing).offset(4)
  233. }
  234. let price = UIView()
  235. container.addSubview(price)
  236. price.snp.makeConstraints { make in
  237. make.centerX.equalToSuperview()
  238. make.top.equalTo(playButton.snp.bottom).offset(2)
  239. }
  240. let coin = UIImageView.coinImageView()
  241. price.addSubview(coin)
  242. coin.snp.makeConstraints { make in
  243. make.leading.centerY.equalToSuperview()
  244. make.width.height.equalTo(18)
  245. }
  246. priceLabel.font = .heading_h4
  247. priceLabel.textColor = .text_5
  248. priceLabel.setContentHuggingPriority(.required, for: .vertical)
  249. priceLabel.setContentCompressionResistancePriority(.required, for: .vertical)
  250. price.addSubview(priceLabel)
  251. priceLabel.snp.makeConstraints { make in
  252. make.verticalEdges.equalToSuperview()
  253. make.leading.equalTo(coin.snp.trailing)
  254. }
  255. unitLabel.font = .body_s
  256. unitLabel.textColor = .text_3
  257. price.addSubview(unitLabel)
  258. unitLabel.snp.makeConstraints { make in
  259. make.centerY.trailing.equalToSuperview()
  260. make.leading.equalTo(priceLabel.snp.trailing).offset(1)
  261. }
  262. let saleView = buildSaleView()
  263. container.addSubview(saleView)
  264. saleView.snp.makeConstraints { make in
  265. make.centerX.equalToSuperview()
  266. make.top.equalTo(price.snp.bottom)
  267. make.bottom.equalToSuperview()
  268. }
  269. return container
  270. }
  271. private func buildBaseInfo() -> UIView {
  272. let container = UIView()
  273. container.isUserInteractionEnabled = false
  274. nameLabel.font = .heading_h4
  275. nameLabel.textColor = .text_5
  276. nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  277. container.addSubview(nameLabel)
  278. nameLabel.snp.makeConstraints { make in
  279. make.leading.equalToSuperview()
  280. make.top.equalToSuperview()
  281. }
  282. container.addSubview(genderView)
  283. genderView.snp.makeConstraints { make in
  284. make.leading.equalTo(nameLabel.snp.trailing).offset(4)
  285. make.centerY.equalTo(nameLabel)
  286. make.trailing.lessThanOrEqualToSuperview()
  287. }
  288. container.addSubview(scoreView)
  289. scoreView.snp.makeConstraints { make in
  290. make.leading.bottom.equalToSuperview()
  291. make.top.equalTo(nameLabel.snp.bottom).offset(3)
  292. make.height.equalTo(15)
  293. }
  294. let starIc = UIImageView()
  295. starIc.image = .icStarFill
  296. scoreView.addSubview(starIc)
  297. starIc.snp.makeConstraints { make in
  298. make.leading.equalToSuperview()
  299. make.width.height.equalTo(14)
  300. make.centerY.equalToSuperview()
  301. }
  302. scoreLabel.font = .heading_h5
  303. scoreLabel.textColor = .text_5
  304. scoreView.addSubview(scoreLabel)
  305. scoreLabel.snp.makeConstraints { make in
  306. make.leading.equalTo(starIc.snp.trailing).offset(3)
  307. make.centerY.equalToSuperview()
  308. }
  309. orderCountLabel.font = .body_xs
  310. orderCountLabel.textColor = .text_3
  311. scoreView.addSubview(orderCountLabel)
  312. orderCountLabel.snp.makeConstraints { make in
  313. make.leading.equalTo(scoreLabel.snp.trailing).offset(3)
  314. make.centerY.equalToSuperview()
  315. make.trailing.equalToSuperview()
  316. }
  317. return container
  318. }
  319. private func buildNewbieView() -> UIView {
  320. newbieView.layer.cornerRadius = 7.5
  321. newbieView.clipsToBounds = true
  322. newbieView.snp.makeConstraints { make in
  323. make.height.equalTo(15)
  324. }
  325. let holder = UIView()
  326. newbieView.addSubview(holder)
  327. holder.snp.makeConstraints { make in
  328. make.leading.equalToSuperview()
  329. make.verticalEdges.equalToSuperview()
  330. }
  331. let gradientLayer = CAGradientLayer()
  332. gradientLayer.colors = [
  333. UIColor(hex: "#FFD35B").cgColor,
  334. UIColor(hex: "#FFFFD35B").cgColor
  335. ]
  336. gradientLayer.startPoint = .init(x: 0, y: 0)
  337. gradientLayer.endPoint = .init(x: 1, y: 0)
  338. holder.layer.addSublayer(gradientLayer)
  339. holder.publisher(for: \.bounds).removeDuplicates().sink
  340. { [weak gradientLayer] newValue in
  341. guard let gradientLayer else { return }
  342. gradientLayer.frame = newValue
  343. }.store(in: &cancellables)
  344. let label = UILabel()
  345. label.font = .body_xs
  346. label.textColor = .init(hex: "#DE9D2E")
  347. label.text = .init(key: "A00310")
  348. holder.addSubview(label)
  349. label.snp.makeConstraints { make in
  350. make.leading.equalToSuperview().offset(6)
  351. make.trailing.equalToSuperview().offset(-14)
  352. make.centerY.equalToSuperview()
  353. }
  354. return newbieView
  355. }
  356. private func buildGameView() -> UIView {
  357. let container = UIView()
  358. container.isUserInteractionEnabled = false
  359. let background = UIView()
  360. background.backgroundColor = .fill_1
  361. background.layer.cornerRadius = 10
  362. container.addSubview(background)
  363. background.snp.makeConstraints { make in
  364. make.verticalEdges.equalToSuperview()
  365. make.leading.equalToSuperview()
  366. }
  367. let border = UIImageView()
  368. border.image = .primary_7
  369. border.layer.cornerRadius = 8
  370. border.clipsToBounds = true
  371. background.addSubview(border)
  372. border.snp.makeConstraints { make in
  373. make.verticalEdges.equalToSuperview().inset(2)
  374. make.leading.equalToSuperview().offset(2)
  375. make.width.height.equalTo(16)
  376. }
  377. gameIc.backgroundColor = .white
  378. gameIc.layer.cornerRadius = 7
  379. background.addSubview(gameIc)
  380. gameIc.snp.makeConstraints { make in
  381. make.center.equalTo(border)
  382. make.width.height.equalTo(14)
  383. }
  384. gameNameLanel.font = .body_xs
  385. gameNameLanel.textColor = .text_4
  386. gameNameLanel.setContentHuggingPriority(.required, for: .horizontal)
  387. gameNameLanel.setContentCompressionResistancePriority(.required, for: .horizontal)
  388. background.addSubview(gameNameLanel)
  389. gameNameLanel.snp.makeConstraints { make in
  390. make.centerY.equalToSuperview()
  391. make.trailing.equalToSuperview().offset(-6)
  392. make.leading.equalTo(border.snp.trailing).offset(2)
  393. }
  394. return container
  395. }
  396. private func buildBio() -> UIView {
  397. let container = UIView()
  398. container.isUserInteractionEnabled = false
  399. let bg = UIImageView()
  400. bg.image = .icGameMateListCellBubble.resizableImage(
  401. withCapInsets: .init(top: 16, left: 20, bottom: 12, right: 15), resizingMode: .stretch
  402. )
  403. container.addSubview(bg)
  404. bg.snp.makeConstraints { make in
  405. make.edges.equalToSuperview()
  406. make.height.greaterThanOrEqualTo(29)
  407. }
  408. bioLabel.font = .body_xs
  409. bioLabel.textColor = .text_6
  410. container.addSubview(bioLabel)
  411. bioLabel.snp.makeConstraints { make in
  412. make.horizontalEdges.equalToSuperview().inset(8)
  413. make.top.equalToSuperview().offset(9)
  414. make.bottom.equalToSuperview().offset(-6)
  415. }
  416. return container
  417. }
  418. private func buildTextViews() -> UIView {
  419. let stackView = UIStackView()
  420. stackView.axis = .vertical
  421. stackView.spacing = 2
  422. stackView.addArrangedSubview(buildBaseInfo())
  423. stackView.addArrangedSubview(buildNewbieView())
  424. stackView.addArrangedSubview(buildGameView())
  425. stackView.addArrangedSubview(buildBio())
  426. return stackView
  427. }
  428. private func buildPhotos() -> UIView {
  429. photoView.axis = .horizontal
  430. photoView.spacing = 6
  431. photoView.distribution = .fillEqually
  432. photoView.alignment = .leading
  433. for _ in 0..<maxShow {
  434. let icon = UIImageView()
  435. icon.layer.cornerRadius = 12
  436. icon.clipsToBounds = true
  437. icon.contentMode = .scaleAspectFill
  438. photoView.addArrangedSubview(icon)
  439. icon.snp.makeConstraints { make in
  440. make.height.equalTo(icon.snp.width)
  441. }
  442. photos.append(icon)
  443. }
  444. return photoView
  445. }
  446. private func buildSaleView() -> UIView {
  447. let triangle = UIImageView()
  448. triangle.image = .icTriangleDown.withRenderingMode(.alwaysTemplate)
  449. triangle.tintColor = .init(hex: "#FF6F32")
  450. triangle.transform = .init(scaleX: 1, y: -1)
  451. saleView.addSubview(triangle)
  452. triangle.snp.makeConstraints { make in
  453. make.centerX.equalToSuperview()
  454. make.top.equalToSuperview()
  455. make.width.equalTo(10)
  456. make.height.equalTo(4)
  457. }
  458. discountView.discountOnly = true
  459. saleView.addSubview(discountView)
  460. discountView.snp.makeConstraints { make in
  461. make.horizontalEdges.equalToSuperview()
  462. make.bottom.equalToSuperview()
  463. make.top.equalTo(triangle.snp.bottom)
  464. }
  465. return saleView
  466. }
  467. }
  468. #if DEBUG
  469. import SwiftUI
  470. struct LNGameMateListCellPreview: UIViewRepresentable {
  471. func makeUIView(context: Context) -> some UIView {
  472. let container = UIView()
  473. container.backgroundColor = .lightGray
  474. let view = LNGameMateListCell()
  475. container.addSubview(view)
  476. view.snp.makeConstraints { make in
  477. make.leading.trailing.centerY.equalToSuperview()
  478. make.height.equalTo(187)
  479. }
  480. return container
  481. }
  482. func updateUIView(_ uiView: UIViewType, context: Context) { }
  483. }
  484. #Preview(body: {
  485. LNGameMateListCellPreview()
  486. })
  487. #endif