LNCoinViewController.swift 9.2 KB

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