LNWalletViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // LNWalletViewController.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 pushToWallet() {
  12. let vc = LNWalletViewController()
  13. navigationController?.pushViewController(vc, animated: true)
  14. }
  15. }
  16. class LNWalletViewController: LNViewController {
  17. private let coinLabel = UILabel()
  18. private let diamondLabel = UILabel()
  19. private let beanLabel = UILabel()
  20. private let bannerView = UIView()
  21. private let bannerPagerView = LNCyclePager()
  22. private let bannerPageControl = LNCyclePageControl()
  23. private var bannerList: [LNBannerInfoVO] = []
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. setupViews()
  27. updateWalletInfo()
  28. loadBanner()
  29. LNEventDeliver.addObserver(self)
  30. LNPurchaseManager.shared.reloadWalletInfo()
  31. }
  32. }
  33. extension LNWalletViewController: LNPurchaseManagerNotify {
  34. func onUserWalletInfoChanged(info: LNUserWalletInfo) {
  35. updateWalletInfo()
  36. }
  37. }
  38. extension LNWalletViewController {
  39. private func updateWalletInfo() {
  40. diamondLabel.text = myWalletInfo.diamond.toDisplay
  41. coinLabel.text = myWalletInfo.coin.toDisplay
  42. beanLabel.text = myWalletInfo.bean.toDisplay
  43. }
  44. private func setupViews() {
  45. view.backgroundColor = .primary_1
  46. let topCover = UIImageView()
  47. topCover.image = .icHomeTopBg
  48. view.addSubview(topCover)
  49. topCover.snp.makeConstraints { make in
  50. make.leading.trailing.equalToSuperview()
  51. make.top.equalTo(fakeNaviBgView)
  52. }
  53. setupNavBar()
  54. let stackView = UIStackView()
  55. stackView.axis = .vertical
  56. stackView.spacing = 16
  57. view.addSubview(stackView)
  58. stackView.snp.makeConstraints { make in
  59. make.horizontalEdges.equalToSuperview().inset(16)
  60. make.top.equalToSuperview().offset(16)
  61. }
  62. stackView.addArrangedSubview(buildWallet())
  63. stackView.addArrangedSubview(buildIncome())
  64. stackView.addArrangedSubview(buildBanner())
  65. }
  66. private func loadBanner() {
  67. LNConfigManager.shared.getBannerList(adSlot: .walletAd) { [weak self] list in
  68. guard let self else { return }
  69. bannerList = list ?? []
  70. bannerView.isHidden = bannerList.isEmpty
  71. bannerPageControl.numberOfPages = bannerList.count
  72. bannerPageControl.currentPage = 0
  73. bannerPageControl.isHidden = bannerList.count <= 1
  74. bannerPagerView.reloadData()
  75. }
  76. }
  77. private func setupNavBar() {
  78. navigationBarColor = .clear
  79. title = .init(key: "A00215")
  80. let history = UIButton()
  81. history.setImage(.icWalletHistory, for: .normal)
  82. history.addAction(UIAction(handler: { [weak self] _ in
  83. guard let self else { return }
  84. view.pushToWebView(.init(url: .walletHistoryUrl))
  85. }), for: .touchUpInside)
  86. setRightButton(history)
  87. }
  88. private func buildWallet() -> UIView {
  89. let container = UIView()
  90. container.layer.cornerRadius = 12
  91. container.backgroundColor = .fill
  92. let titleView = UIView()
  93. container.addSubview(titleView)
  94. titleView.snp.makeConstraints { make in
  95. make.horizontalEdges.equalToSuperview()
  96. make.top.equalToSuperview()
  97. make.height.equalTo(40)
  98. }
  99. let titleLabel = UILabel()
  100. titleLabel.text = .init(key: "A00268")
  101. titleLabel.font = .heading_h3
  102. titleLabel.textColor = .text_5
  103. titleView.addSubview(titleLabel)
  104. titleLabel.snp.makeConstraints { make in
  105. make.leading.equalToSuperview().offset(16)
  106. make.centerY.equalToSuperview()
  107. }
  108. let coinView = buildCoinView()
  109. container.addSubview(coinView)
  110. coinView.snp.makeConstraints { make in
  111. make.horizontalEdges.equalToSuperview().inset(16)
  112. make.top.equalTo(titleView.snp.bottom)
  113. make.height.equalTo(64)
  114. }
  115. let diamondView = buildDiamondView()
  116. container.addSubview(diamondView)
  117. diamondView.snp.makeConstraints { make in
  118. make.horizontalEdges.equalToSuperview().inset(16)
  119. make.top.equalTo(coinView.snp.bottom).offset(10)
  120. make.height.equalTo(64)
  121. make.bottom.equalToSuperview().offset(-12)
  122. }
  123. return container
  124. }
  125. private func buildCoinView() -> UIView {
  126. let container = UIView()
  127. container.backgroundColor = .init(hex: "#FFC4000D")
  128. container.layer.cornerRadius = 12
  129. container.onTap { [weak self] in
  130. guard let self else { return }
  131. view.pushToCoinView()
  132. }
  133. let coin = UIImageView.coinImageView(true)
  134. container.addSubview(coin)
  135. coin.snp.makeConstraints { make in
  136. make.leading.equalToSuperview().offset(14)
  137. make.centerY.equalToSuperview()
  138. make.width.height.equalTo(42)
  139. }
  140. let textView = UIView()
  141. container.addSubview(textView)
  142. textView.snp.makeConstraints { make in
  143. make.leading.equalTo(coin.snp.trailing).offset(8)
  144. make.centerY.equalToSuperview()
  145. }
  146. coinLabel.text = "0"
  147. coinLabel.font = .heading_h1_5
  148. coinLabel.textColor = .text_5
  149. textView.addSubview(coinLabel)
  150. coinLabel.snp.makeConstraints { make in
  151. make.leading.top.equalToSuperview()
  152. make.trailing.equalToSuperview()
  153. }
  154. let descLabel = UILabel()
  155. descLabel.text = .init(key: "A00216")
  156. descLabel.font = .heading_h5
  157. descLabel.textColor = .text_5
  158. textView.addSubview(descLabel)
  159. descLabel.snp.makeConstraints { make in
  160. make.leading.trailing.bottom.equalToSuperview()
  161. make.top.equalTo(coinLabel.snp.bottom).offset(-5)
  162. }
  163. let jumpButton = UIButton()
  164. jumpButton.setBackgroundImage(.primary_8, for: .normal)
  165. jumpButton.layer.cornerRadius = 12
  166. jumpButton.clipsToBounds = true
  167. jumpButton.addAction(UIAction(handler: { [weak self] _ in
  168. guard let self else { return }
  169. view.pushToCoinView()
  170. }), for: .touchUpInside)
  171. container.addSubview(jumpButton)
  172. jumpButton.snp.makeConstraints { make in
  173. make.centerY.equalToSuperview()
  174. make.trailing.equalToSuperview().offset(-14)
  175. make.height.equalTo(24)
  176. }
  177. let contentView = UIView()
  178. contentView.isUserInteractionEnabled = false
  179. jumpButton.addSubview(contentView)
  180. contentView.snp.makeConstraints { make in
  181. make.center.equalToSuperview()
  182. make.leading.greaterThanOrEqualToSuperview().offset(8)
  183. }
  184. let jumpTitleLabel = UILabel()
  185. jumpTitleLabel.text = .init(key: "A00279")
  186. jumpTitleLabel.font = .heading_h5
  187. jumpTitleLabel.textColor = .text_1
  188. contentView.addSubview(jumpTitleLabel)
  189. jumpTitleLabel.snp.makeConstraints { make in
  190. make.leading.verticalEdges.equalToSuperview()
  191. }
  192. let arrow = UIImageView.arrowImageView(size: 10)
  193. arrow.tintColor = .white
  194. contentView.addSubview(arrow)
  195. arrow.snp.makeConstraints { make in
  196. make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
  197. make.centerY.equalToSuperview()
  198. make.trailing.equalToSuperview()
  199. }
  200. return container
  201. }
  202. private func buildDiamondView() -> UIView {
  203. let container = UIView()
  204. container.backgroundColor = .init(hex: "#008FFF0D")
  205. container.layer.cornerRadius = 12
  206. container.onTap { [weak self] in
  207. guard let self else { return }
  208. view.pushToDiamondView()
  209. }
  210. let diamond = UIImageView.diamondImageView(true)
  211. container.addSubview(diamond)
  212. diamond.snp.makeConstraints { make in
  213. make.leading.equalToSuperview().offset(14)
  214. make.centerY.equalToSuperview()
  215. make.width.height.equalTo(42)
  216. }
  217. let textView = UIView()
  218. container.addSubview(textView)
  219. textView.snp.makeConstraints { make in
  220. make.leading.equalTo(diamond.snp.trailing).offset(8)
  221. make.centerY.equalToSuperview()
  222. }
  223. diamondLabel.text = "0"
  224. diamondLabel.font = .heading_h1_5
  225. diamondLabel.textColor = .text_5
  226. textView.addSubview(diamondLabel)
  227. diamondLabel.snp.makeConstraints { make in
  228. make.leading.top.equalToSuperview()
  229. make.trailing.equalToSuperview()
  230. }
  231. let descLabel = UILabel()
  232. descLabel.text = .init(key: "A00217")
  233. descLabel.font = .heading_h5
  234. descLabel.textColor = .text_5
  235. textView.addSubview(descLabel)
  236. descLabel.snp.makeConstraints { make in
  237. make.leading.trailing.bottom.equalToSuperview()
  238. make.top.equalTo(diamondLabel.snp.bottom).offset(-5)
  239. }
  240. let jumpButton = UIButton()
  241. jumpButton.setBackgroundImage(.primary_8, for: .normal)
  242. jumpButton.layer.cornerRadius = 12
  243. jumpButton.clipsToBounds = true
  244. jumpButton.addAction(UIAction(handler: { [weak self] _ in
  245. guard let self else { return }
  246. view.pushToDiamondView()
  247. }), for: .touchUpInside)
  248. container.addSubview(jumpButton)
  249. jumpButton.snp.makeConstraints { make in
  250. make.centerY.equalToSuperview()
  251. make.trailing.equalToSuperview().offset(-14)
  252. make.height.equalTo(24)
  253. }
  254. let contentView = UIView()
  255. contentView.isUserInteractionEnabled = false
  256. jumpButton.addSubview(contentView)
  257. contentView.snp.makeConstraints { make in
  258. make.center.equalToSuperview()
  259. make.leading.greaterThanOrEqualToSuperview().offset(8)
  260. }
  261. let jumpTitleLabel = UILabel()
  262. jumpTitleLabel.text = .init(key: "A00279")
  263. jumpTitleLabel.font = .heading_h5
  264. jumpTitleLabel.textColor = .text_1
  265. contentView.addSubview(jumpTitleLabel)
  266. jumpTitleLabel.snp.makeConstraints { make in
  267. make.leading.verticalEdges.equalToSuperview()
  268. }
  269. let arrow = UIImageView.arrowImageView(size: 10)
  270. arrow.tintColor = .white
  271. contentView.addSubview(arrow)
  272. arrow.snp.makeConstraints { make in
  273. make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
  274. make.centerY.equalToSuperview()
  275. make.trailing.equalToSuperview()
  276. }
  277. return container
  278. }
  279. private func buildIncome() -> UIView {
  280. let container = UIView()
  281. container.layer.cornerRadius = 12
  282. container.backgroundColor = .fill
  283. let titleView = UIView()
  284. container.addSubview(titleView)
  285. titleView.snp.makeConstraints { make in
  286. make.horizontalEdges.equalToSuperview()
  287. make.top.equalToSuperview()
  288. make.height.equalTo(40)
  289. }
  290. let titleLabel = UILabel()
  291. titleLabel.text = .init(key: "A00276")
  292. titleLabel.font = .heading_h3
  293. titleLabel.textColor = .text_5
  294. titleView.addSubview(titleLabel)
  295. titleLabel.snp.makeConstraints { make in
  296. make.leading.equalToSuperview().offset(16)
  297. make.centerY.equalToSuperview()
  298. }
  299. let beanView = buildBeanView()
  300. container.addSubview(beanView)
  301. beanView.snp.makeConstraints { make in
  302. make.horizontalEdges.equalToSuperview().inset(16)
  303. make.top.equalTo(titleView.snp.bottom)
  304. make.height.equalTo(64)
  305. make.bottom.equalToSuperview().offset(-10)
  306. }
  307. return container
  308. }
  309. private func buildBeanView() -> UIView {
  310. let container = UIView()
  311. container.backgroundColor = .init(hex: "#FF73000D")
  312. container.layer.cornerRadius = 12
  313. container.onTap { [weak self] in
  314. guard let self else { return }
  315. view.pushToBeanView()
  316. }
  317. let bean = UIImageView.beanImageView()
  318. container.addSubview(bean)
  319. bean.snp.makeConstraints { make in
  320. make.leading.equalToSuperview().offset(14)
  321. make.centerY.equalToSuperview()
  322. make.width.height.equalTo(42)
  323. }
  324. let textView = UIView()
  325. container.addSubview(textView)
  326. textView.snp.makeConstraints { make in
  327. make.leading.equalTo(bean.snp.trailing).offset(8)
  328. make.centerY.equalToSuperview()
  329. }
  330. beanLabel.text = myWalletInfo.bean.toDisplay
  331. beanLabel.font = .heading_h1_5
  332. beanLabel.textColor = .text_5
  333. textView.addSubview(beanLabel)
  334. beanLabel.snp.makeConstraints { make in
  335. make.leading.bottom.equalToSuperview()
  336. make.trailing.equalToSuperview()
  337. }
  338. let descLabel = UILabel()
  339. descLabel.text = .init(key: "A00277")
  340. descLabel.font = .heading_h5
  341. descLabel.textColor = .text_5
  342. textView.addSubview(descLabel)
  343. descLabel.snp.makeConstraints { make in
  344. make.leading.trailing.top.equalToSuperview()
  345. make.bottom.equalTo(beanLabel.snp.top)
  346. }
  347. let jumpButton = UIButton()
  348. jumpButton.setBackgroundImage(.primary_8, for: .normal)
  349. jumpButton.layer.cornerRadius = 12
  350. jumpButton.clipsToBounds = true
  351. jumpButton.addAction(UIAction(handler: { [weak self] _ in
  352. guard let self else { return }
  353. view.pushToBeanView()
  354. }), for: .touchUpInside)
  355. container.addSubview(jumpButton)
  356. jumpButton.snp.makeConstraints { make in
  357. make.centerY.equalToSuperview()
  358. make.trailing.equalToSuperview().offset(-14)
  359. make.height.equalTo(24)
  360. }
  361. let contentView = UIView()
  362. jumpButton.addSubview(contentView)
  363. contentView.snp.makeConstraints { make in
  364. make.center.equalToSuperview()
  365. make.leading.greaterThanOrEqualToSuperview().offset(8)
  366. }
  367. let jumpTitleLabel = UILabel()
  368. jumpTitleLabel.text = .init(key: "A00390")
  369. jumpTitleLabel.font = .heading_h5
  370. jumpTitleLabel.textColor = .text_1
  371. contentView.addSubview(jumpTitleLabel)
  372. jumpTitleLabel.snp.makeConstraints { make in
  373. make.leading.verticalEdges.equalToSuperview()
  374. }
  375. let arrow = UIImageView.arrowImageView(size: 10)
  376. arrow.tintColor = .white
  377. contentView.addSubview(arrow)
  378. arrow.snp.makeConstraints { make in
  379. make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
  380. make.centerY.equalToSuperview()
  381. make.trailing.equalToSuperview()
  382. }
  383. return container
  384. }
  385. private func buildBanner() -> UIView {
  386. bannerView.isHidden = true
  387. bannerView.layer.cornerRadius = 16
  388. bannerView.layer.masksToBounds = true
  389. bannerView.snp.makeConstraints { make in
  390. make.height.equalTo(70)
  391. }
  392. bannerView.layer.borderWidth = 0.5
  393. bannerView.layer.borderColor = UIColor(hex: "#FFFFFF").cgColor
  394. bannerPagerView.layer.cornerRadius = 16
  395. bannerPagerView.layer.masksToBounds = true
  396. bannerPagerView.isInfiniteLoop = true
  397. bannerPagerView.autoScrollInterval = 3
  398. bannerPagerView.reloadDataNeedResetIndex = true
  399. bannerPagerView.bindPageControl(bannerPageControl)
  400. bannerPagerView.register(LNAdBannerImageCell.self, forCellWithReuseIdentifier: LNAdBannerImageCell.className)
  401. bannerPagerView.numberOfItemsProvider = { [weak self] in
  402. self?.bannerList.count ?? 0
  403. }
  404. bannerPagerView.cellProvider = { [weak self] pager, index in
  405. guard let self else { return UICollectionViewCell() }
  406. let cell = pager.dequeueReusableCell(
  407. withReuseIdentifier: LNAdBannerImageCell.className,
  408. for: index
  409. ) as! LNAdBannerImageCell
  410. cell.update(self.bannerList[index])
  411. return cell
  412. }
  413. bannerPagerView.layoutProvider = { [weak self] in
  414. let layout = LNCyclePagerViewLayout()
  415. layout.itemSize = self?.bannerPagerView.bounds.size ?? .zero
  416. layout.itemSpacing = 0
  417. layout.scrollDirection = .horizontal
  418. layout.layoutType = .normal
  419. return layout
  420. }
  421. bannerPagerView.didScrollFromIndexToIndex = { [weak self] _, toIndex in
  422. self?.bannerPageControl.currentPage = toIndex
  423. }
  424. bannerPagerView.didSelectItem = { [weak self] _, index in
  425. guard let self, index >= 0, index < self.bannerList.count else { return }
  426. let item = self.bannerList[index]
  427. guard !item.jump.isEmpty else { return }
  428. self.view.pushToWebView(LNJumpWebViewConfig.normalConfig(url: item.jump))
  429. }
  430. bannerView.addSubview(bannerPagerView)
  431. bannerPagerView.snp.makeConstraints { make in
  432. make.edges.equalToSuperview()
  433. }
  434. bannerPageControl.pageIndicatorSize = CGSize(width: 3, height: 3)
  435. bannerPageControl.currentPageIndicatorSize = CGSize(width: 3, height: 3)
  436. bannerPageControl.pageIndicatorTintColor = .init(hex: "#1D21294D")
  437. bannerPageControl.currentPageIndicatorTintColor = .white
  438. bannerPageControl.pageIndicatorSpacing = 3
  439. bannerPagerView.addSubview(bannerPageControl)
  440. bannerPageControl.snp.makeConstraints { make in
  441. make.centerX.equalToSuperview()
  442. make.bottom.equalToSuperview().offset(-6)
  443. make.height.equalTo(4)
  444. }
  445. return bannerView
  446. }
  447. }
  448. #if DEBUG
  449. import SwiftUI
  450. struct LNWalletViewControllerPreview: UIViewControllerRepresentable {
  451. func makeUIViewController(context: Context) -> some UIViewController {
  452. LNWalletViewController()
  453. }
  454. func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
  455. }
  456. #Preview(body: {
  457. LNWalletViewControllerPreview()
  458. })
  459. #endif