LNCoinViewController.swift 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // LNCoinViewController.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/24.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. extension UIView {
  11. func pushToCoinView() {
  12. let vc = LNCoinViewController()
  13. navigationController?.pushViewController(vc, animated: true)
  14. }
  15. }
  16. class LNCoinViewController: LNViewController {
  17. private let valueLabel = UILabel()
  18. private let rechargeView = LNPurchaseProductView()
  19. private let rechargeButton = UIButton()
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. setupViews()
  23. rechargeView.loadList(.coin)
  24. onUserWalletInfoChanged(info: myWalletInfo)
  25. LNEventDeliver.addObserver(self)
  26. }
  27. }
  28. extension LNCoinViewController: LNPurchaseManagerNotify {
  29. func onUserWalletInfoChanged(info: LNUserWalletInfo) {
  30. valueLabel.text = info.coin.toDisplay
  31. }
  32. func onUserPurchaseResult(err: LNPurchaseError?) {
  33. dismissLoading()
  34. if let err {
  35. showToast(err.errorDesc)
  36. return
  37. }
  38. }
  39. }
  40. extension LNCoinViewController: LNPurchaseProductViewDelegate {
  41. func onPurchaseProductView(view: LNPurchaseProductView, didSelect goods: LNPurchaseGoodsVO?) {
  42. if rechargeButton.isEnabled, goods == nil {
  43. rechargeButton.isEnabled = false
  44. rechargeButton.setBackgroundImage(nil, for: .normal)
  45. } else if !rechargeButton.isEnabled, goods != nil {
  46. rechargeButton.isEnabled = true
  47. rechargeButton.setBackgroundImage(.primary_8, for: .normal)
  48. }
  49. }
  50. }
  51. extension LNCoinViewController {
  52. private func setupViews() {
  53. navigationBarColor = .primary_1
  54. view.backgroundColor = .primary_1
  55. title = .init(key: "A00215")
  56. let bg = buildValueView()
  57. view.addSubview(bg)
  58. bg.snp.makeConstraints { make in
  59. make.horizontalEdges.equalToSuperview().inset(15)
  60. make.top.equalToSuperview().offset(8)
  61. }
  62. let set = buildRechargeSet()
  63. view.addSubview(set)
  64. set.snp.makeConstraints { make in
  65. make.horizontalEdges.equalToSuperview()
  66. make.bottom.equalToSuperview()
  67. make.top.equalTo(bg.snp.bottom).offset(-31)
  68. }
  69. }
  70. private func buildValueView() -> UIView {
  71. let bg = UIImageView()
  72. bg.image = .icCoinBg
  73. bg.isUserInteractionEnabled = true
  74. let titleLabel = UILabel()
  75. titleLabel.text = .init(key: "A00268")
  76. titleLabel.font = .heading_h4
  77. titleLabel.textColor = .text_5
  78. bg.addSubview(titleLabel)
  79. titleLabel.snp.makeConstraints { make in
  80. make.leading.equalToSuperview().offset(30)
  81. make.top.equalToSuperview().offset(38)
  82. }
  83. let valueView = UIView()
  84. bg.addSubview(valueView)
  85. valueView.snp.makeConstraints { make in
  86. make.leading.equalToSuperview().offset(30)
  87. make.top.equalTo(titleLabel.snp.bottom).offset(6)
  88. }
  89. let coin = UIImageView.coinImageView()
  90. valueView.addSubview(coin)
  91. coin.snp.makeConstraints { make in
  92. make.leading.equalToSuperview()
  93. make.centerY.equalToSuperview()
  94. make.width.height.equalTo(24)
  95. }
  96. valueLabel.text = "0"
  97. valueLabel.font = .heading_h1
  98. valueLabel.textColor = .text_5
  99. valueView.addSubview(valueLabel)
  100. valueLabel.snp.makeConstraints { make in
  101. make.verticalEdges.equalToSuperview()
  102. make.trailing.equalToSuperview()
  103. make.leading.equalTo(coin.snp.trailing).offset(2)
  104. }
  105. let jumpButton = UIButton()
  106. jumpButton.addAction(UIAction(handler: { _ in
  107. let panel = LNExchangePanel(exchangeType: .coinToDiamond)
  108. panel.popup()
  109. }), for: .touchUpInside)
  110. bg.addSubview(jumpButton)
  111. jumpButton.snp.makeConstraints { make in
  112. make.top.equalToSuperview()
  113. make.trailing.equalToSuperview()
  114. make.width.equalTo(189)
  115. make.height.equalTo(30)
  116. }
  117. let contentView = UIView()
  118. contentView.isUserInteractionEnabled = false
  119. jumpButton.addSubview(contentView)
  120. contentView.snp.makeConstraints { make in
  121. make.center.equalToSuperview()
  122. }
  123. let jumpTitleLabel = UILabel()
  124. jumpTitleLabel.text = .init(key: "A00264")
  125. jumpTitleLabel.font = .heading_h4
  126. jumpTitleLabel.textColor = .text_1
  127. jumpTitleLabel.textAlignment = .center
  128. contentView.addSubview(jumpTitleLabel)
  129. jumpTitleLabel.snp.makeConstraints { make in
  130. make.leading.equalToSuperview()
  131. make.verticalEdges.equalToSuperview()
  132. }
  133. let arrow = UIImageView.arrowImageView(size: 14)
  134. arrow.tintColor = .white
  135. contentView.addSubview(arrow)
  136. arrow.snp.makeConstraints { make in
  137. make.centerY.equalToSuperview()
  138. make.trailing.equalToSuperview()
  139. make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(4)
  140. }
  141. return bg
  142. }
  143. private func buildRechargeSet() -> UIView {
  144. let container = UIView()
  145. container.backgroundColor = .fill
  146. container.layer.cornerRadius = 20
  147. container.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
  148. let scrollView = UIScrollView()
  149. scrollView.showsVerticalScrollIndicator = false
  150. scrollView.showsHorizontalScrollIndicator = false
  151. container.addSubview(scrollView)
  152. scrollView.snp.makeConstraints { make in
  153. make.horizontalEdges.equalToSuperview().inset(16)
  154. make.top.equalToSuperview().offset(20)
  155. make.bottom.equalToSuperview().offset(-view.safeBottomInset - 10)
  156. }
  157. let fakeView = UIView()
  158. scrollView.addSubview(fakeView)
  159. fakeView.snp.makeConstraints { make in
  160. make.horizontalEdges.equalToSuperview()
  161. make.top.width.equalToSuperview()
  162. make.height.equalTo(0)
  163. }
  164. rechargeView.delegate = self
  165. scrollView.addSubview(rechargeView)
  166. rechargeView.snp.makeConstraints { make in
  167. make.horizontalEdges.equalToSuperview()
  168. make.top.equalToSuperview()
  169. }
  170. rechargeButton.backgroundColor = .text_2
  171. rechargeButton.layer.cornerRadius = 23.5
  172. rechargeButton.setBackgroundImage(.primary_8, for: .normal)
  173. rechargeButton.setTitle(.init(key: "A00269"), for: .normal)
  174. rechargeButton.setTitleColor(.text_1, for: .normal)
  175. rechargeButton.titleLabel?.font = .heading_h3
  176. rechargeButton.clipsToBounds = true
  177. rechargeButton.addAction(UIAction(handler: { [weak self] _ in
  178. guard let self else { return }
  179. guard let goods = rechargeView.curSelected else { return }
  180. // LNPurchaseManager.shared.purchaseProduct(goods: goods)
  181. showLoading()
  182. RechargeManager.shared.startRecharge(goods: goods)
  183. }), for: .touchUpInside)
  184. scrollView.addSubview(rechargeButton)
  185. rechargeButton.snp.makeConstraints { make in
  186. make.horizontalEdges.equalToSuperview()
  187. make.top.equalTo(rechargeView.snp.bottom).offset(20)
  188. make.height.equalTo(47)
  189. }
  190. let introduce = buildDescView()
  191. scrollView.addSubview(introduce)
  192. introduce.snp.makeConstraints { make in
  193. make.horizontalEdges.equalToSuperview()
  194. make.top.equalTo(rechargeButton.snp.bottom).offset(20)
  195. make.bottom.equalToSuperview()
  196. }
  197. return container
  198. }
  199. private func buildDescView() -> UIView {
  200. let container = UIView()
  201. let titleLabel = UILabel()
  202. titleLabel.text = .init(key: "A00270")
  203. titleLabel.font = .heading_h5
  204. titleLabel.textColor = .text_5
  205. container.addSubview(titleLabel)
  206. titleLabel.snp.makeConstraints { make in
  207. make.leading.top.equalToSuperview()
  208. }
  209. let textView = LNAutoSizeTextView()
  210. textView.backgroundColor = .clear
  211. textView.text = .init(key: "A00271")
  212. textView.font = .body_s
  213. textView.textColor = .text_4
  214. textView.linkTextAttributes = [.foregroundColor: UIColor.primary_5]
  215. textView.isEditable = false
  216. container.addSubview(textView)
  217. textView.snp.makeConstraints { make in
  218. make.horizontalEdges.equalToSuperview().inset(-4)
  219. make.top.equalTo(titleLabel.snp.bottom).offset(-7)
  220. make.bottom.equalToSuperview()
  221. }
  222. return container
  223. }
  224. }
  225. #if DEBUG
  226. import SwiftUI
  227. struct LNCoinViewControllerPreview: UIViewControllerRepresentable {
  228. func makeUIViewController(context: Context) -> some UIViewController {
  229. LNCoinViewController()
  230. }
  231. func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
  232. }
  233. #Preview(body: {
  234. LNCoinViewControllerPreview()
  235. })
  236. #endif