LNPurchaseProductView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // LNPurchaseProductView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/30.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. protocol LNPurchaseProductViewDelegate: NSObject {
  11. func onPurchaseProductView(view: LNPurchaseProductView, didSelect goods: LNPurchaseGoodsVO?)
  12. }
  13. class LNPurchaseProductView: UIView {
  14. private let productsView = LNMultiLineStackView()
  15. private var itemViews: [LNPurchaseItemView] = []
  16. var curSelected: LNPurchaseGoodsVO? {
  17. itemViews.first { $0.isSelected }?.goods
  18. }
  19. weak var delegate: LNPurchaseProductViewDelegate?
  20. override init(frame: CGRect) {
  21. super.init(frame: frame)
  22. setupViews()
  23. }
  24. func loadList(_ type: LNCurrencyType) {
  25. LNPurchaseManager.shared.loadGoodsList(currencyType: type) { [weak self] list in
  26. guard let self else { return }
  27. guard let list else { return }
  28. itemViews.removeAll()
  29. list.forEach {
  30. let itemView = LNPurchaseItemView()
  31. itemView.currencyIc.image = switch type {
  32. case .coin: .icCoin
  33. case .diamond: .icDiamond
  34. case .bean: .icBean
  35. }
  36. itemView.valueLabel.text = "\($0.coinRechargeAmount.currencyDisplay)"
  37. itemView.moneyLabel.text = "\($0.currency) \($0.amount.cashDisplay)"
  38. itemView.goods = $0
  39. itemView.onTap { [weak self, weak itemView] in
  40. guard let self, let itemView else { return }
  41. updateSelection(newSelection: itemView)
  42. }
  43. self.itemViews.append(itemView)
  44. }
  45. productsView.update(itemViews)
  46. updateSelection(newSelection: nil)
  47. }
  48. }
  49. required init?(coder: NSCoder) {
  50. fatalError("init(coder:) has not been implemented")
  51. }
  52. }
  53. extension LNPurchaseProductView {
  54. private func updateSelection(newSelection: LNPurchaseItemView?) {
  55. itemViews.forEach {
  56. $0.isSelected = newSelection == $0
  57. }
  58. delegate?.onPurchaseProductView(view: self, didSelect: newSelection?.goods)
  59. }
  60. private func setupViews() {
  61. productsView.columns = 3
  62. productsView.itemSpacing = 10
  63. productsView.itemDistribution = .fillEqually
  64. productsView.spacing = 10
  65. addSubview(productsView)
  66. productsView.snp.makeConstraints { make in
  67. make.edges.equalToSuperview()
  68. }
  69. }
  70. }
  71. private class LNPurchaseItemView: UIView {
  72. let currencyIc = UIImageView()
  73. let valueLabel = UILabel()
  74. let moneyLabel = UILabel()
  75. var goods: LNPurchaseGoodsVO?
  76. var isSelected: Bool = false {
  77. didSet {
  78. bg.isHidden = !isSelected
  79. }
  80. }
  81. private let bg = UIImageView()
  82. override init(frame: CGRect) {
  83. super.init(frame: frame)
  84. backgroundColor = .fill_1
  85. layer.cornerRadius = 12
  86. clipsToBounds = true
  87. snp.makeConstraints { make in
  88. make.height.equalTo(76)
  89. }
  90. bg.image = .primary_7
  91. addSubview(bg)
  92. bg.snp.makeConstraints { make in
  93. make.edges.equalToSuperview()
  94. }
  95. let cover = UIView()
  96. cover.backgroundColor = .fill_1
  97. cover.layer.cornerRadius = 11
  98. bg.addSubview(cover)
  99. cover.snp.makeConstraints { make in
  100. make.edges.equalToSuperview().inset(1)
  101. }
  102. let container = UIView()
  103. addSubview(container)
  104. container.snp.makeConstraints { make in
  105. make.center.equalToSuperview()
  106. }
  107. let coinView = UIView()
  108. container.addSubview(coinView)
  109. coinView.snp.makeConstraints { make in
  110. make.centerX.top.equalToSuperview()
  111. }
  112. coinView.addSubview(currencyIc)
  113. currencyIc.snp.makeConstraints { make in
  114. make.leading.centerY.equalToSuperview()
  115. make.width.height.equalTo(18)
  116. }
  117. valueLabel.font = .heading_h2
  118. valueLabel.textColor = .text_5
  119. coinView.addSubview(valueLabel)
  120. valueLabel.snp.makeConstraints { make in
  121. make.verticalEdges.equalToSuperview()
  122. make.trailing.equalToSuperview()
  123. make.leading.equalTo(currencyIc.snp.trailing).offset(2)
  124. }
  125. moneyLabel.font = .body_s
  126. moneyLabel.textColor = .text_4
  127. container.addSubview(moneyLabel)
  128. moneyLabel.snp.makeConstraints { make in
  129. make.horizontalEdges.equalToSuperview()
  130. make.top.equalTo(coinView.snp.bottom).offset(4)
  131. make.bottom.equalToSuperview()
  132. }
  133. }
  134. required init?(coder: NSCoder) {
  135. fatalError("init(coder:) has not been implemented")
  136. }
  137. }