LNRoomGiftBottomView.swift 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // LNRoomGiftBottomView.swift
  3. // Gami
  4. //
  5. // Created by OneeChan on 2026/3/23.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. protocol LNRoomGiftBottomViewDelegate: AnyObject {
  11. func onRoomGiftBottomViewDidTapBalance(_ view: LNRoomGiftBottomView)
  12. func onRoomGiftBottomViewDidTapSend(_ view: LNRoomGiftBottomView)
  13. }
  14. class LNRoomGiftBottomView: UIView {
  15. private let balanceLabel = UILabel()
  16. private let sendButton = UIButton(type: .system)
  17. private let countView = UIView()
  18. private let minusButton = UIButton()
  19. private let countLabel = UILabel()
  20. private var countLabelWidth: Constraint?
  21. private(set) var curCount: Int = 0 {
  22. didSet {
  23. countLabel.text = "\(curCount)"
  24. minusButton.isEnabled = curCount > 1
  25. minusButton.alpha = curCount > 1 ? 1.0 : 0.4
  26. if curCount > 99 {
  27. countLabelWidth?.update(offset: 35)
  28. } else if curCount > 9 {
  29. countLabelWidth?.update(offset: 25)
  30. } else {
  31. countLabelWidth?.update(offset: 15)
  32. }
  33. }
  34. }
  35. var enable: Bool = false {
  36. didSet {
  37. sendButton.isEnabled = enable
  38. sendButton.alpha = enable ? 1.0 : 0.4
  39. countView.isHidden = !enable
  40. if !enable {
  41. curCount = 1
  42. }
  43. }
  44. }
  45. weak var delegate: LNRoomGiftBottomViewDelegate?
  46. override init(frame: CGRect) {
  47. super.init(frame: frame)
  48. setupViews()
  49. LNEventDeliver.addObserver(self)
  50. runOnMain { [weak self] in
  51. guard let self else { return }
  52. curCount = 1
  53. enable = false
  54. }
  55. }
  56. required init?(coder: NSCoder) {
  57. fatalError("init(coder:) has not been implemented")
  58. }
  59. }
  60. extension LNRoomGiftBottomView: LNPurchaseManagerNotify {
  61. func onUserWalletInfoChanged(info: LNUserWalletInfo) {
  62. balanceLabel.text = myWalletInfo.diamond.toDisplay
  63. }
  64. }
  65. private extension LNRoomGiftBottomView {
  66. func setupViews() {
  67. snp.makeConstraints { make in
  68. make.height.equalTo(54)
  69. }
  70. let balanceView = UIView()
  71. balanceView.onTap { [weak self] in
  72. guard let self else { return }
  73. delegate?.onRoomGiftBottomViewDidTapBalance(self)
  74. }
  75. addSubview(balanceView)
  76. balanceView.snp.makeConstraints { make in
  77. make.leading.equalToSuperview().offset(10)
  78. make.verticalEdges.equalToSuperview()
  79. }
  80. let diamond = UIImageView.diamondImageView(true)
  81. balanceView.addSubview(diamond)
  82. diamond.snp.makeConstraints { make in
  83. make.leading.equalToSuperview()
  84. make.centerY.equalToSuperview().offset(-1)
  85. make.width.height.equalTo(18)
  86. }
  87. balanceLabel.font = .heading_h3
  88. balanceLabel.textColor = .text_1
  89. balanceLabel.text = myWalletInfo.diamond.toDisplay
  90. balanceView.addSubview(balanceLabel)
  91. balanceLabel.snp.makeConstraints { make in
  92. make.leading.equalTo(diamond.snp.trailing).offset(2)
  93. make.centerY.equalToSuperview()
  94. }
  95. let arrow = UIImageView.arrowImageView(size: 12, weight: .semibold)
  96. arrow.tintColor = .text_1
  97. balanceView.addSubview(arrow)
  98. arrow.snp.makeConstraints { make in
  99. make.leading.equalTo(balanceLabel.snp.trailing).offset(4)
  100. make.trailing.equalToSuperview()
  101. make.centerY.equalToSuperview()
  102. }
  103. countView.layer.cornerRadius = 15
  104. countView.backgroundColor = .fill.withAlphaComponent(0.1)
  105. addSubview(countView)
  106. countView.snp.makeConstraints { make in
  107. make.top.equalToSuperview().offset(10)
  108. make.trailing.equalToSuperview().offset(-10)
  109. make.height.equalTo(30)
  110. }
  111. sendButton.setTitle(.init(key: "A00305"), for: .normal)
  112. sendButton.setTitleColor(.text_1, for: .normal)
  113. sendButton.titleLabel?.font = .heading_h5
  114. sendButton.layer.cornerRadius = 15
  115. sendButton.clipsToBounds = true
  116. sendButton.setBackgroundImage(.primary_8, for: .normal)
  117. sendButton.contentEdgeInsets = .init(top: 0, left: 12, bottom: 0, right: 12)
  118. sendButton.addAction(UIAction(handler: { [weak self] _ in
  119. guard let self else { return }
  120. delegate?.onRoomGiftBottomViewDidTapSend(self)
  121. }), for: .touchUpInside)
  122. addSubview(sendButton)
  123. sendButton.snp.makeConstraints { make in
  124. make.trailing.equalTo(countView)
  125. make.centerY.equalTo(countView)
  126. make.height.equalTo(countView)
  127. }
  128. let config = UIImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  129. minusButton.setImage(.init(systemName: "minus", withConfiguration: config), for: .normal)
  130. minusButton.backgroundColor = .fill.withAlphaComponent(0.6)
  131. minusButton.layer.cornerRadius = 12
  132. minusButton.tintColor = .fill_7
  133. minusButton.addAction(UIAction(handler: { [weak self] _ in
  134. guard let self else { return }
  135. curCount -= 1
  136. }), for: .touchUpInside)
  137. countView.addSubview(minusButton)
  138. minusButton.snp.makeConstraints { make in
  139. make.width.height.equalTo(24)
  140. make.centerY.equalToSuperview()
  141. make.leading.equalToSuperview().offset(3)
  142. }
  143. countLabel.font = .body_m
  144. countLabel.textColor = .text_1
  145. countLabel.textAlignment = .center
  146. countView.addSubview(countLabel)
  147. countLabel.snp.makeConstraints { make in
  148. make.centerY.equalToSuperview()
  149. make.leading.equalTo(minusButton.snp.trailing).offset(6)
  150. countLabelWidth = make.width.equalTo(15).constraint
  151. }
  152. let fakeView = UIView()
  153. countView.addSubview(fakeView)
  154. fakeView.snp.makeConstraints { make in
  155. make.verticalEdges.equalToSuperview()
  156. make.trailing.equalToSuperview()
  157. make.width.equalTo(sendButton)
  158. }
  159. let addButton = UIButton()
  160. addButton.setImage(.init(systemName: "plus", withConfiguration: config), for: .normal)
  161. addButton.backgroundColor = .fill.withAlphaComponent(0.6)
  162. addButton.layer.cornerRadius = 12
  163. addButton.tintColor = .fill_7
  164. addButton.addAction(UIAction(handler: { [weak self] _ in
  165. guard let self else { return }
  166. curCount += 1
  167. }), for: .touchUpInside)
  168. countView.addSubview(addButton)
  169. addButton.snp.makeConstraints { make in
  170. make.width.height.equalTo(24)
  171. make.centerY.equalToSuperview()
  172. make.leading.equalTo(countLabel.snp.trailing).offset(6)
  173. make.trailing.equalTo(fakeView.snp.leading).offset(-12)
  174. }
  175. }
  176. }