LNWalletViewController.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. showNavigationBar = false
  23. setupViews()
  24. updateWalletInfo()
  25. LNEventDeliver.addObserver(self)
  26. LNPurchaseManager.shared.reloadWalletInfo()
  27. }
  28. }
  29. extension LNWalletViewController: LNPurchaseManagerNotify {
  30. func onUserWalletInfoChanged(info: LNUserWalletInfo) {
  31. updateWalletInfo()
  32. }
  33. }
  34. extension LNWalletViewController {
  35. private func updateWalletInfo() {
  36. diamondLabel.text = myWalletInfo.diamond.toDisplay
  37. coinLabel.text = myWalletInfo.coin.toDisplay
  38. beanLabel.text = myWalletInfo.bean.toDisplay
  39. }
  40. private func setupViews() {
  41. view.backgroundColor = .primary_1
  42. let topCover = UIImageView()
  43. topCover.image = .init(named: "ic_home_top_bg")
  44. view.addSubview(topCover)
  45. topCover.snp.makeConstraints { make in
  46. make.top.leading.trailing.equalToSuperview()
  47. }
  48. let naviBar = buildFakeNavBar()
  49. view.addSubview(naviBar)
  50. naviBar.snp.makeConstraints { make in
  51. make.directionalHorizontalEdges.equalToSuperview()
  52. make.top.equalToSuperview()
  53. }
  54. let stackView = UIStackView()
  55. stackView.axis = .vertical
  56. stackView.spacing = 16
  57. view.addSubview(stackView)
  58. stackView.snp.makeConstraints { make in
  59. make.directionalHorizontalEdges.equalToSuperview().inset(16)
  60. make.top.equalTo(naviBar.snp.bottom).offset(16)
  61. }
  62. stackView.addArrangedSubview(buildWallet())
  63. if myUserInfo.playmate {
  64. stackView.addArrangedSubview(buildIncome())
  65. }
  66. }
  67. private func buildFakeNavBar() -> UIView {
  68. let fakeBar = UIView()
  69. fakeBar.snp.makeConstraints { make in
  70. make.height.equalTo((navigationController?.navigationBar.bounds.height ?? 44) + UIView.statusBarHeight)
  71. }
  72. let barView = UIView()
  73. fakeBar.addSubview(barView)
  74. barView.snp.makeConstraints { make in
  75. make.directionalHorizontalEdges.equalToSuperview()
  76. make.bottom.equalToSuperview()
  77. make.height.equalTo(navigationController?.navigationBar.bounds.height ?? 44)
  78. }
  79. let closeButton = UIButton(type: .system)
  80. closeButton.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
  81. closeButton.tintColor = .text_5
  82. closeButton.addAction(UIAction(handler: { [weak self] _ in
  83. guard let self else { return }
  84. navigationController?.popViewController(animated: true)
  85. }), for: .touchUpInside)
  86. closeButton.translatesAutoresizingMaskIntoConstraints = false
  87. barView.addSubview(closeButton)
  88. closeButton.snp.makeConstraints { make in
  89. make.leading.equalToSuperview().offset(16)
  90. make.centerY.equalToSuperview()
  91. make.width.height.equalTo(24)
  92. }
  93. let titleLabel = UILabel()
  94. titleLabel.text = .init(key: "A00215")
  95. titleLabel.font = .heading_h2
  96. titleLabel.textColor = .text_5
  97. barView.addSubview(titleLabel)
  98. titleLabel.snp.makeConstraints { make in
  99. make.center.equalToSuperview()
  100. }
  101. let history = UIButton()
  102. history.setImage(.init(named: "ic_wallet_history"), for: .normal)
  103. history.addAction(UIAction(handler: { [weak self] _ in
  104. guard let self else { return }
  105. view.pushToWebView(.init(url: .walletHistoryUrl, showNavigationBar: false))
  106. }), for: .touchUpInside)
  107. barView.addSubview(history)
  108. history.snp.makeConstraints { make in
  109. make.trailing.equalToSuperview().offset(-16)
  110. make.centerY.equalToSuperview()
  111. make.width.height.equalTo(30)
  112. }
  113. return fakeBar
  114. }
  115. private func buildWallet() -> UIView {
  116. let container = UIView()
  117. container.layer.cornerRadius = 12
  118. container.backgroundColor = .fill
  119. let titleView = UIView()
  120. container.addSubview(titleView)
  121. titleView.snp.makeConstraints { make in
  122. make.directionalHorizontalEdges.equalToSuperview()
  123. make.top.equalToSuperview()
  124. make.height.equalTo(40)
  125. }
  126. let titleLabel = UILabel()
  127. titleLabel.text = .init(key: "A00268")
  128. titleLabel.font = .heading_h3
  129. titleLabel.textColor = .text_5
  130. titleView.addSubview(titleLabel)
  131. titleLabel.snp.makeConstraints { make in
  132. make.leading.equalToSuperview().offset(16)
  133. make.centerY.equalToSuperview()
  134. }
  135. let coinView = buildCoinView()
  136. container.addSubview(coinView)
  137. coinView.snp.makeConstraints { make in
  138. make.directionalHorizontalEdges.equalToSuperview().inset(16)
  139. make.top.equalTo(titleView.snp.bottom)
  140. make.height.equalTo(64)
  141. }
  142. let diamondView = buildDiamondView()
  143. container.addSubview(diamondView)
  144. diamondView.snp.makeConstraints { make in
  145. make.directionalHorizontalEdges.equalToSuperview().inset(16)
  146. make.top.equalTo(coinView.snp.bottom).offset(10)
  147. make.height.equalTo(64)
  148. make.bottom.equalToSuperview().offset(-12)
  149. }
  150. return container
  151. }
  152. private func buildCoinView() -> UIView {
  153. let container = UIView()
  154. container.backgroundColor = .init(hex: "#FFC4000D")
  155. container.layer.cornerRadius = 12
  156. container.onTap { [weak self] in
  157. guard let self else { return }
  158. view.pushToCoinView()
  159. }
  160. let coin = UIImageView.coinImageView(true)
  161. container.addSubview(coin)
  162. coin.snp.makeConstraints { make in
  163. make.leading.equalToSuperview().offset(14)
  164. make.centerY.equalToSuperview()
  165. make.width.height.equalTo(42)
  166. }
  167. let textView = UIView()
  168. container.addSubview(textView)
  169. textView.snp.makeConstraints { make in
  170. make.leading.equalTo(coin.snp.trailing).offset(8)
  171. make.centerY.equalToSuperview()
  172. }
  173. coinLabel.text = "0"
  174. coinLabel.font = .heading_h1_5
  175. coinLabel.textColor = .text_5
  176. textView.addSubview(coinLabel)
  177. coinLabel.snp.makeConstraints { make in
  178. make.leading.top.equalToSuperview()
  179. make.trailing.equalToSuperview()
  180. }
  181. let descLabel = UILabel()
  182. descLabel.text = .init(key: "A00216")
  183. descLabel.font = .heading_h5
  184. descLabel.textColor = .text_5
  185. textView.addSubview(descLabel)
  186. descLabel.snp.makeConstraints { make in
  187. make.leading.trailing.bottom.equalToSuperview()
  188. make.top.equalTo(coinLabel.snp.bottom).offset(-5)
  189. }
  190. let jumpButton = UIButton()
  191. jumpButton.setBackgroundImage(.primary_8, for: .normal)
  192. jumpButton.layer.cornerRadius = 12
  193. jumpButton.clipsToBounds = true
  194. jumpButton.addAction(UIAction(handler: { [weak self] _ in
  195. guard let self else { return }
  196. view.pushToCoinView()
  197. }), for: .touchUpInside)
  198. container.addSubview(jumpButton)
  199. jumpButton.snp.makeConstraints { make in
  200. make.centerY.equalToSuperview()
  201. make.trailing.equalToSuperview().offset(-14)
  202. make.height.equalTo(24)
  203. }
  204. let contentView = UIView()
  205. contentView.isUserInteractionEnabled = false
  206. jumpButton.addSubview(contentView)
  207. contentView.snp.makeConstraints { make in
  208. make.center.equalToSuperview()
  209. make.leading.greaterThanOrEqualToSuperview().offset(8)
  210. }
  211. let jumpTitleLabel = UILabel()
  212. jumpTitleLabel.text = .init(key: "A00279")
  213. jumpTitleLabel.font = .heading_h5
  214. jumpTitleLabel.textColor = .text_1
  215. contentView.addSubview(jumpTitleLabel)
  216. jumpTitleLabel.snp.makeConstraints { make in
  217. make.leading.verticalEdges.equalToSuperview()
  218. }
  219. let arrow = UIImageView.arrowImageView(size: 10)
  220. arrow.tintColor = .white
  221. contentView.addSubview(arrow)
  222. arrow.snp.makeConstraints { make in
  223. make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
  224. make.centerY.equalToSuperview()
  225. make.trailing.equalToSuperview()
  226. }
  227. return container
  228. }
  229. private func buildDiamondView() -> UIView {
  230. let container = UIView()
  231. container.backgroundColor = .init(hex: "#008FFF0D")
  232. container.layer.cornerRadius = 12
  233. container.onTap { [weak self] in
  234. guard let self else { return }
  235. view.pushToDiamondView()
  236. }
  237. let diamond = UIImageView.diamondImageView(true)
  238. container.addSubview(diamond)
  239. diamond.snp.makeConstraints { make in
  240. make.leading.equalToSuperview().offset(14)
  241. make.centerY.equalToSuperview()
  242. make.width.height.equalTo(42)
  243. }
  244. let textView = UIView()
  245. container.addSubview(textView)
  246. textView.snp.makeConstraints { make in
  247. make.leading.equalTo(diamond.snp.trailing).offset(8)
  248. make.centerY.equalToSuperview()
  249. }
  250. diamondLabel.text = "0"
  251. diamondLabel.font = .heading_h1_5
  252. diamondLabel.textColor = .text_5
  253. textView.addSubview(diamondLabel)
  254. diamondLabel.snp.makeConstraints { make in
  255. make.leading.top.equalToSuperview()
  256. make.trailing.equalToSuperview()
  257. }
  258. let descLabel = UILabel()
  259. descLabel.text = .init(key: "A00217")
  260. descLabel.font = .heading_h5
  261. descLabel.textColor = .text_5
  262. textView.addSubview(descLabel)
  263. descLabel.snp.makeConstraints { make in
  264. make.leading.trailing.bottom.equalToSuperview()
  265. make.top.equalTo(diamondLabel.snp.bottom).offset(-5)
  266. }
  267. let jumpButton = UIButton()
  268. jumpButton.setBackgroundImage(.primary_8, for: .normal)
  269. jumpButton.layer.cornerRadius = 12
  270. jumpButton.clipsToBounds = true
  271. jumpButton.addAction(UIAction(handler: { [weak self] _ in
  272. guard let self else { return }
  273. view.pushToDiamondView()
  274. }), for: .touchUpInside)
  275. container.addSubview(jumpButton)
  276. jumpButton.snp.makeConstraints { make in
  277. make.centerY.equalToSuperview()
  278. make.trailing.equalToSuperview().offset(-14)
  279. make.height.equalTo(24)
  280. }
  281. let contentView = UIView()
  282. contentView.isUserInteractionEnabled = false
  283. jumpButton.addSubview(contentView)
  284. contentView.snp.makeConstraints { make in
  285. make.center.equalToSuperview()
  286. make.leading.greaterThanOrEqualToSuperview().offset(8)
  287. }
  288. let jumpTitleLabel = UILabel()
  289. jumpTitleLabel.text = .init(key: "A00279")
  290. jumpTitleLabel.font = .heading_h5
  291. jumpTitleLabel.textColor = .text_1
  292. contentView.addSubview(jumpTitleLabel)
  293. jumpTitleLabel.snp.makeConstraints { make in
  294. make.leading.verticalEdges.equalToSuperview()
  295. }
  296. let arrow = UIImageView.arrowImageView(size: 10)
  297. arrow.tintColor = .white
  298. contentView.addSubview(arrow)
  299. arrow.snp.makeConstraints { make in
  300. make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
  301. make.centerY.equalToSuperview()
  302. make.trailing.equalToSuperview()
  303. }
  304. return container
  305. }
  306. private func buildIncome() -> UIView {
  307. let container = UIView()
  308. container.layer.cornerRadius = 12
  309. container.backgroundColor = .fill
  310. let titleView = UIView()
  311. container.addSubview(titleView)
  312. titleView.snp.makeConstraints { make in
  313. make.directionalHorizontalEdges.equalToSuperview()
  314. make.top.equalToSuperview()
  315. make.height.equalTo(40)
  316. }
  317. let titleLabel = UILabel()
  318. titleLabel.text = .init(key: "A00276")
  319. titleLabel.font = .heading_h3
  320. titleLabel.textColor = .text_5
  321. titleView.addSubview(titleLabel)
  322. titleLabel.snp.makeConstraints { make in
  323. make.leading.equalToSuperview().offset(16)
  324. make.centerY.equalToSuperview()
  325. }
  326. let beanView = buildBeanView()
  327. container.addSubview(beanView)
  328. beanView.snp.makeConstraints { make in
  329. make.directionalHorizontalEdges.equalToSuperview().inset(16)
  330. make.top.equalTo(titleView.snp.bottom)
  331. make.height.equalTo(64)
  332. make.bottom.equalToSuperview().offset(-10)
  333. }
  334. return container
  335. }
  336. private func buildBeanView() -> UIView {
  337. let container = UIView()
  338. container.backgroundColor = .init(hex: "#FF73000D")
  339. container.layer.cornerRadius = 12
  340. let bean = UIImageView.beanImageView()
  341. container.addSubview(bean)
  342. bean.snp.makeConstraints { make in
  343. make.leading.equalToSuperview().offset(14)
  344. make.centerY.equalToSuperview()
  345. make.width.height.equalTo(42)
  346. }
  347. let textView = UIView()
  348. container.addSubview(textView)
  349. textView.snp.makeConstraints { make in
  350. make.leading.equalTo(bean.snp.trailing).offset(8)
  351. make.centerY.equalToSuperview()
  352. }
  353. beanLabel.text = myWalletInfo.bean.toDisplay
  354. beanLabel.font = .heading_h1_5
  355. beanLabel.textColor = .text_5
  356. textView.addSubview(beanLabel)
  357. beanLabel.snp.makeConstraints { make in
  358. make.leading.bottom.equalToSuperview()
  359. make.trailing.equalToSuperview()
  360. }
  361. let descLabel = UILabel()
  362. descLabel.text = .init(key: "A00277")
  363. descLabel.font = .heading_h5
  364. descLabel.textColor = .text_5
  365. textView.addSubview(descLabel)
  366. descLabel.snp.makeConstraints { make in
  367. make.leading.trailing.top.equalToSuperview()
  368. make.bottom.equalTo(beanLabel.snp.top)
  369. }
  370. // let jumpButton = UIButton()
  371. // jumpButton.setBackgroundImage(.primary_8, for: .normal)
  372. // jumpButton.layer.cornerRadius = 12
  373. // jumpButton.clipsToBounds = true
  374. // container.addSubview(jumpButton)
  375. // jumpButton.snp.makeConstraints { make in
  376. // make.centerY.equalToSuperview()
  377. // make.trailing.equalToSuperview().offset(-14)
  378. // make.height.equalTo(24)
  379. // }
  380. //
  381. // let contentView = UIView()
  382. // jumpButton.addSubview(contentView)
  383. // contentView.snp.makeConstraints { make in
  384. // make.center.equalToSuperview()
  385. // make.leading.greaterThanOrEqualToSuperview().offset(8)
  386. // }
  387. // let jumpTitleLabel = UILabel()
  388. // jumpTitleLabel.text = .init(key: "Details")
  389. // jumpTitleLabel.font = .heading_h5
  390. // jumpTitleLabel.textColor = .text_1
  391. // contentView.addSubview(jumpTitleLabel)
  392. // jumpTitleLabel.snp.makeConstraints { make in
  393. // make.leading.verticalEdges.equalToSuperview()
  394. // }
  395. //
  396. // let arrow = UIImageView.arrowImageView(size: 10)
  397. // arrow.tintColor = .white
  398. // contentView.addSubview(arrow)
  399. // arrow.snp.makeConstraints { make in
  400. // make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(2)
  401. // make.centerY.equalToSuperview()
  402. // make.trailing.equalToSuperview()
  403. // }
  404. return container
  405. }
  406. }
  407. #if DEBUG
  408. import SwiftUI
  409. struct LNWalletViewControllerPreview: UIViewControllerRepresentable {
  410. func makeUIViewController(context: Context) -> some UIViewController {
  411. LNWalletViewController()
  412. }
  413. func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { }
  414. }
  415. #Preview(body: {
  416. LNWalletViewControllerPreview()
  417. })
  418. #endif