LNIMChatGameMateSkillCell.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // LNIMChatGameMateSkillCell.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/11.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. protocol LNIMChatGameMateSkillCellDelegate: AnyObject {
  11. func onIMChatGameMateSkillCell(cell: LNIMChatGameMateSkillCell, didClickOrder skill: LNGameMateSkillVO)
  12. }
  13. class LNIMChatGameMateSkillCell: UIView {
  14. private let background = UIImageView()
  15. private let gameIc = UIImageView()
  16. private let gameView = UIView()
  17. private let discountView = LNNewbieDiscountView()
  18. private let gameNameLabel = UILabel()
  19. private let gamePriceLabel = UILabel()
  20. private let toVoiceButton = UIButton()
  21. private var curItem: LNGameMateSkillVO?
  22. weak var delegate: LNIMChatGameMateSkillCellDelegate?
  23. override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. setupViews()
  26. LNEventDeliver.addObserver(self)
  27. }
  28. func update(_ skill: LNGameMateSkillVO) {
  29. gameView.isHidden = false
  30. gameIc.sd_setImage(with: URL(string: skill.icon))
  31. gameNameLabel.text = skill.name
  32. gamePriceLabel.text = "\(skill.price.toDisplay) / \(skill.unit)"
  33. curItem = skill
  34. updateDiscount()
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. }
  40. extension LNIMChatGameMateSkillCell: LNOrderManagerNotify {
  41. func onMyDiscountInfoChanged(info: LNOrderDiscountVO?) {
  42. updateDiscount()
  43. }
  44. private func updateDiscount() {
  45. // if let discount = LNOrderManager.shared.discountFor(curItem?.price ?? 0) {
  46. // discountView.isHidden = false
  47. // discountView.update(1 - discount)
  48. // } else {
  49. discountView.isHidden = true
  50. // }
  51. }
  52. }
  53. extension LNIMChatGameMateSkillCell {
  54. private func setupViews() {
  55. let container = UIView()
  56. container.backgroundColor = .fill
  57. container.layer.cornerRadius = 12
  58. container.layer.borderWidth = 1
  59. container.layer.borderColor = UIColor.fill.cgColor
  60. addSubview(container)
  61. container.snp.makeConstraints { make in
  62. make.horizontalEdges.equalToSuperview().inset(4)
  63. make.verticalEdges.equalToSuperview()
  64. }
  65. background.layer.cornerRadius = 11
  66. background.clipsToBounds = true
  67. background.image = .primary_6
  68. background.alpha = 0.8
  69. container.addSubview(background)
  70. background.snp.makeConstraints { make in
  71. make.edges.equalToSuperview().inset(1).priority(.medium)
  72. }
  73. gameIc.layer.cornerRadius = 25
  74. gameIc.backgroundColor = .fill.withAlphaComponent(0.7)
  75. gameIc.clipsToBounds = true
  76. container.addSubview(gameIc)
  77. gameIc.snp.makeConstraints { make in
  78. make.leading.equalToSuperview().offset(16)
  79. make.centerY.equalToSuperview()
  80. make.width.height.equalTo(50)
  81. }
  82. let gameView = buildGameView()
  83. container.addSubview(gameView)
  84. gameView.snp.makeConstraints { make in
  85. make.leading.equalTo(gameIc.snp.trailing).offset(8)
  86. make.centerY.equalToSuperview()
  87. make.trailing.equalToSuperview().offset(-16)
  88. }
  89. }
  90. private func buildGameView() -> UIView {
  91. gameView.isHidden = true
  92. let tapView = UIView()
  93. tapView.onTap { [weak self] in
  94. guard let self else { return }
  95. guard let curItem else { return }
  96. delegate?.onIMChatGameMateSkillCell(cell: self, didClickOrder: curItem)
  97. }
  98. gameView.addSubview(tapView)
  99. tapView.snp.makeConstraints { make in
  100. make.verticalEdges.equalToSuperview()
  101. make.trailing.equalToSuperview()
  102. make.width.equalTo(100)
  103. }
  104. let order = UIButton()
  105. order.setBackgroundImage(.primary_8, for: .normal)
  106. order.layer.cornerRadius = 12
  107. order.clipsToBounds = true
  108. order.addAction(UIAction(handler: { [weak self] _ in
  109. guard let self else { return }
  110. guard let curItem else { return }
  111. delegate?.onIMChatGameMateSkillCell(cell: self, didClickOrder: curItem)
  112. }), for: .touchUpInside)
  113. gameView.addSubview(order)
  114. order.snp.makeConstraints { make in
  115. make.centerY.equalToSuperview()
  116. make.trailing.equalToSuperview()
  117. make.height.equalTo(24)
  118. }
  119. let orderTitle = UILabel()
  120. orderTitle.text = .init(key: "A00041")
  121. orderTitle.font = .heading_h5
  122. orderTitle.textColor = .text_1
  123. orderTitle.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  124. orderTitle.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
  125. order.addSubview(orderTitle)
  126. orderTitle.snp.makeConstraints { make in
  127. make.centerY.equalToSuperview()
  128. make.horizontalEdges.equalToSuperview().inset(17)
  129. }
  130. let infoView = UIView()
  131. gameView.addSubview(infoView)
  132. infoView.snp.makeConstraints { make in
  133. make.verticalEdges.equalToSuperview()
  134. make.leading.equalToSuperview()
  135. make.trailing.lessThanOrEqualTo(order.snp.leading).offset(-16)
  136. }
  137. gameNameLabel.font = .heading_h4
  138. gameNameLabel.textColor = .text_5
  139. gameNameLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
  140. gameNameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  141. infoView.addSubview(gameNameLabel)
  142. gameNameLabel.snp.makeConstraints { make in
  143. make.horizontalEdges.equalToSuperview()
  144. make.top.equalToSuperview()
  145. }
  146. let priceView = UIStackView()
  147. priceView.spacing = 2
  148. infoView.addSubview(priceView)
  149. priceView.snp.makeConstraints { make in
  150. make.leading.equalToSuperview()
  151. make.trailing.lessThanOrEqualToSuperview()
  152. make.top.equalTo(gameNameLabel.snp.bottom).offset(1)
  153. make.bottom.equalToSuperview()
  154. }
  155. discountView.discountOnly = true
  156. priceView.addArrangedSubview(discountView)
  157. let coin = UIImageView.coinImageView()
  158. priceView.addArrangedSubview(coin)
  159. gamePriceLabel.font = .body_m
  160. gamePriceLabel.textColor = .text_5
  161. priceView.addArrangedSubview(gamePriceLabel)
  162. return gameView
  163. }
  164. }
  165. #if DEBUG
  166. import SwiftUI
  167. struct LNIMChatGameMateSkillCellPreview: UIViewRepresentable {
  168. func makeUIView(context: Context) -> some UIView {
  169. let container = UIView()
  170. container.backgroundColor = .lightGray
  171. let view = LNIMChatGameMateSkillCell()
  172. container.addSubview(view)
  173. view.snp.makeConstraints { make in
  174. make.horizontalEdges.equalToSuperview()
  175. make.centerY.equalToSuperview()
  176. make.height.equalTo(72)
  177. }
  178. return container
  179. }
  180. func updateUIView(_ uiView: UIViewType, context: Context) { }
  181. }
  182. #Preview(body: {
  183. LNIMChatGameMateSkillCellPreview()
  184. })
  185. #endif