LNDiamondViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // LNDiamondViewController.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/25.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. extension UIView {
  11. func pushToDiamondView() {
  12. let vc = LNDiamondViewController()
  13. navigationController?.pushViewController(vc, animated: true)
  14. }
  15. }
  16. class LNDiamondViewController: 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(.diamond)
  24. onUserWalletInfoChanged(info: myWalletInfo)
  25. LNEventDeliver.addObserver(self)
  26. }
  27. }
  28. extension LNDiamondViewController: LNPurchaseManagerNotify {
  29. func onUserWalletInfoChanged(info: LNUserWalletInfo) {
  30. valueLabel.text = "\(info.diamond)"
  31. }
  32. func onUserPurchaseResult(err: LNPurchaseError?) {
  33. dismissLoading()
  34. if let err {
  35. showToast(err.errorDesc)
  36. return
  37. }
  38. }
  39. }
  40. extension LNDiamondViewController: 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 LNDiamondViewController {
  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 = .icDiamondBg
  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 diamond = UIImageView.diamondImageView()
  90. valueView.addSubview(diamond)
  91. diamond.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(diamond.snp.trailing).offset(2)
  104. }
  105. let jumpButton = UIButton()
  106. jumpButton.addAction(UIAction(handler: { _ in
  107. let panel = LNExchangePanel(exchangeType: .diamondToCoin)
  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(165)
  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: "A00273")
  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. private class LNDiamondRechargeItemView: UIView {
  226. let diamondLabel = UILabel()
  227. let moneyLabel = UILabel()
  228. var goods: LNPurchaseGoodsVO?
  229. var isSelected: Bool = false {
  230. didSet {
  231. bg.isHidden = !isSelected
  232. }
  233. }
  234. private let bg = UIImageView()
  235. override init(frame: CGRect) {
  236. super.init(frame: frame)
  237. backgroundColor = .fill_1
  238. layer.cornerRadius = 12
  239. clipsToBounds = true
  240. snp.makeConstraints { make in
  241. make.height.equalTo(76)
  242. }
  243. bg.image = .primary_7
  244. addSubview(bg)
  245. bg.snp.makeConstraints { make in
  246. make.edges.equalToSuperview()
  247. }
  248. let cover = UIView()
  249. cover.backgroundColor = .fill_1
  250. cover.layer.cornerRadius = 11
  251. bg.addSubview(cover)
  252. cover.snp.makeConstraints { make in
  253. make.edges.equalToSuperview().inset(1)
  254. }
  255. let container = UIView()
  256. addSubview(container)
  257. container.snp.makeConstraints { make in
  258. make.center.equalToSuperview()
  259. }
  260. let diamondView = UIView()
  261. container.addSubview(diamondView)
  262. diamondView.snp.makeConstraints { make in
  263. make.centerX.top.equalToSuperview()
  264. }
  265. let diamond = UIImageView.diamondImageView()
  266. diamondView.addSubview(diamond)
  267. diamond.snp.makeConstraints { make in
  268. make.leading.centerY.equalToSuperview()
  269. make.width.height.equalTo(18)
  270. }
  271. diamondLabel.font = .heading_h2
  272. diamondLabel.textColor = .text_5
  273. diamondView.addSubview(diamondLabel)
  274. diamondLabel.snp.makeConstraints { make in
  275. make.verticalEdges.equalToSuperview()
  276. make.trailing.equalToSuperview()
  277. make.leading.equalTo(diamond.snp.trailing).offset(2)
  278. }
  279. moneyLabel.font = .body_s
  280. moneyLabel.textColor = .text_4
  281. container.addSubview(moneyLabel)
  282. moneyLabel.snp.makeConstraints { make in
  283. make.horizontalEdges.equalToSuperview()
  284. make.top.equalTo(diamondView.snp.bottom).offset(4)
  285. make.bottom.equalToSuperview()
  286. }
  287. }
  288. required init?(coder: NSCoder) {
  289. fatalError("init(coder:) has not been implemented")
  290. }
  291. }
  292. #if DEBUG
  293. import SwiftUI
  294. struct LNDiamondViewControllerPreview: UIViewControllerRepresentable {
  295. func makeUIViewController(context: Context) -> some UIViewController {
  296. LNDiamondViewController()
  297. }
  298. func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
  299. }
  300. #Preview(body: {
  301. LNDiamondViewControllerPreview()
  302. })
  303. #endif