LNPurchasePanel.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // LNPurchasePanel.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/30.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNPurchasePanel: LNPopupView {
  11. private let productsView = LNPurchaseProductView()
  12. private let currencyLabel = UILabel()
  13. private let currencyIc = UIImageView()
  14. private let rechargeButton = UIButton()
  15. override init(frame: CGRect) {
  16. super.init(frame: frame)
  17. setupViews()
  18. LNEventDeliver.addObserver(self)
  19. }
  20. func update(_ type: LNCurrencyType) {
  21. switch type {
  22. case .coin:
  23. currencyIc.image = .icCoin
  24. currencyLabel.text = myWalletInfo.coin.toDisplay
  25. case .diamond:
  26. currencyIc.image = .icDiamond
  27. currencyLabel.text = myWalletInfo.diamond.toDisplay
  28. case .bean:
  29. currencyIc.image = .icBean
  30. currencyLabel.text = myWalletInfo.bean.toDisplay
  31. }
  32. productsView.loadList(type)
  33. }
  34. required init?(coder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. }
  38. extension LNPurchasePanel: LNPurchaseManagerNotify {
  39. func onUserPurchaseResult(err: LNPurchaseError?) {
  40. dismissLoading()
  41. if let err {
  42. showToast(err.errorDesc)
  43. } else {
  44. dismiss()
  45. }
  46. }
  47. }
  48. extension LNPurchasePanel: LNPurchaseProductViewDelegate {
  49. func onPurchaseProductView(view: LNPurchaseProductView, didSelect goods: LNPurchaseGoodsVO?) {
  50. if rechargeButton.isEnabled, goods == nil {
  51. rechargeButton.isEnabled = false
  52. rechargeButton.setBackgroundImage(nil, for: .normal)
  53. } else if !rechargeButton.isEnabled, goods != nil {
  54. rechargeButton.isEnabled = true
  55. rechargeButton.setBackgroundImage(.primary_8, for: .normal)
  56. }
  57. }
  58. }
  59. extension LNPurchasePanel {
  60. private func setupViews() {
  61. container.backgroundColor = .primary_1
  62. let titleLabel = UILabel()
  63. titleLabel.text = .init(key: "A00279")
  64. titleLabel.font = .heading_h3
  65. titleLabel.textColor = .text_5
  66. container.addSubview(titleLabel)
  67. titleLabel.snp.makeConstraints { make in
  68. make.centerX.equalToSuperview()
  69. make.top.equalToSuperview().offset(14)
  70. }
  71. let product = buildProducts()
  72. container.addSubview(product)
  73. product.snp.makeConstraints { make in
  74. make.horizontalEdges.equalToSuperview().inset(12)
  75. make.top.equalTo(titleLabel.snp.bottom).offset(18)
  76. }
  77. let currency = buildCurrency()
  78. container.addSubview(currency)
  79. currency.snp.makeConstraints { make in
  80. make.horizontalEdges.equalToSuperview().inset(12)
  81. make.top.equalTo(product.snp.bottom).offset(12)
  82. }
  83. rechargeButton.setTitle(.init(key: "A00269"), for: .normal)
  84. rechargeButton.setTitleColor(.text_1, for: .normal)
  85. rechargeButton.titleLabel?.font = .heading_h3
  86. rechargeButton.layer.cornerRadius = 23.5
  87. rechargeButton.clipsToBounds = true
  88. rechargeButton.backgroundColor = .text_2
  89. rechargeButton.addAction(UIAction(handler: { [weak self] _ in
  90. guard let self else { return }
  91. guard let goods = productsView.curSelected else { return }
  92. showLoading()
  93. RechargeManager.shared.startRecharge(goods: goods)
  94. }), for: .touchUpInside)
  95. container.addSubview(rechargeButton)
  96. rechargeButton.snp.makeConstraints { make in
  97. make.horizontalEdges.equalToSuperview().inset(12)
  98. make.top.equalTo(currency.snp.bottom).offset(37)
  99. make.bottom.equalToSuperview().offset(commonBottomInset)
  100. make.height.equalTo(47)
  101. }
  102. }
  103. private func buildProducts() -> UIView {
  104. let container = UIView()
  105. container.backgroundColor = .fill
  106. container.layer.cornerRadius = 12
  107. productsView.delegate = self
  108. container.addSubview(productsView)
  109. productsView.snp.makeConstraints { make in
  110. make.horizontalEdges.equalToSuperview().inset(12)
  111. make.verticalEdges.equalToSuperview().inset(10)
  112. }
  113. return container
  114. }
  115. private func buildCurrency() -> UIView {
  116. let container = UIView()
  117. container.backgroundColor = .fill
  118. container.layer.cornerRadius = 12
  119. container.snp.makeConstraints { make in
  120. make.height.equalTo(44)
  121. }
  122. let ic = UIImageView(image: .icWalletWithBg)
  123. container.addSubview(ic)
  124. ic.snp.makeConstraints { make in
  125. make.leading.equalToSuperview().offset(12)
  126. make.centerY.equalToSuperview()
  127. make.width.height.equalTo(24)
  128. }
  129. let descLabel = UILabel()
  130. descLabel.text = .init(key: "A00278")
  131. descLabel.font = .heading_h5
  132. descLabel.textColor = .text_5
  133. container.addSubview(descLabel)
  134. descLabel.snp.makeConstraints { make in
  135. make.leading.equalTo(ic.snp.trailing).offset(6)
  136. make.centerY.equalToSuperview()
  137. }
  138. currencyLabel.font = .body_m
  139. currencyLabel.textColor = .text_3
  140. container.addSubview(currencyLabel)
  141. currencyLabel.snp.makeConstraints { make in
  142. make.trailing.equalToSuperview().offset(-12)
  143. make.centerY.equalToSuperview()
  144. }
  145. container.addSubview(currencyIc)
  146. currencyIc.snp.makeConstraints { make in
  147. make.centerY.equalToSuperview()
  148. make.trailing.equalTo(currencyLabel.snp.leading).offset(-1)
  149. make.width.height.equalTo(16)
  150. }
  151. return container
  152. }
  153. }