// // LNDiamondViewController.swift // Lanu // // Created by OneeChan on 2025/12/25. // import Foundation import UIKit import SnapKit extension UIView { func pushToDiamondView() { let vc = LNDiamondViewController() navigationController?.pushViewController(vc, animated: true) } } class LNDiamondViewController: LNViewController { private let valueLabel = UILabel() private let rechargeView = LNPurchaseProductView() private let rechargeButton = UIButton() override func viewDidLoad() { super.viewDidLoad() setupViews() rechargeView.loadList(.diamond) onUserWalletInfoChanged(info: myWalletInfo) LNEventDeliver.addObserver(self) } } extension LNDiamondViewController: LNPurchaseManagerNotify { func onUserWalletInfoChanged(info: LNUserWalletInfo) { valueLabel.text = info.diamond.toDisplay } func onUserPurchaseResult(err: LNPurchaseError?) { dismissLoading() if let err { showToast(err.errorDesc) return } } } extension LNDiamondViewController: LNPurchaseProductViewDelegate { func onPurchaseProductView(view: LNPurchaseProductView, didSelect goods: LNPurchaseGoodsVO?) { if rechargeButton.isEnabled, goods == nil { rechargeButton.isEnabled = false rechargeButton.setBackgroundImage(nil, for: .normal) } else if !rechargeButton.isEnabled, goods != nil { rechargeButton.isEnabled = true rechargeButton.setBackgroundImage(.primary_8, for: .normal) } } } extension LNDiamondViewController { private func setupViews() { navigationBarColor = .primary_1 view.backgroundColor = .primary_1 title = .init(key: "A00215") let bg = buildValueView() view.addSubview(bg) bg.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview().inset(15) make.top.equalToSuperview().offset(8) } let set = buildRechargeSet() view.addSubview(set) set.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.bottom.equalToSuperview() make.top.equalTo(bg.snp.bottom).offset(-31) } } private func buildValueView() -> UIView { let bg = UIImageView() bg.image = .icDiamondBg bg.isUserInteractionEnabled = true let titleLabel = UILabel() titleLabel.text = .init(key: "A00268") titleLabel.font = .heading_h4 titleLabel.textColor = .text_5 bg.addSubview(titleLabel) titleLabel.snp.makeConstraints { make in make.leading.equalToSuperview().offset(30) make.top.equalToSuperview().offset(38) } let valueView = UIView() bg.addSubview(valueView) valueView.snp.makeConstraints { make in make.leading.equalToSuperview().offset(30) make.top.equalTo(titleLabel.snp.bottom).offset(6) } let diamond = UIImageView.diamondImageView() valueView.addSubview(diamond) diamond.snp.makeConstraints { make in make.leading.equalToSuperview() make.centerY.equalToSuperview() make.width.height.equalTo(24) } valueLabel.text = "0" valueLabel.font = .heading_h1 valueLabel.textColor = .text_5 valueView.addSubview(valueLabel) valueLabel.snp.makeConstraints { make in make.verticalEdges.equalToSuperview() make.trailing.equalToSuperview() make.leading.equalTo(diamond.snp.trailing).offset(2) } let jumpButton = UIButton() jumpButton.addAction(UIAction(handler: { _ in let panel = LNExchangePanel(exchangeType: .diamondToCoin) panel.popup() }), for: .touchUpInside) bg.addSubview(jumpButton) jumpButton.snp.makeConstraints { make in make.top.equalToSuperview() make.trailing.equalToSuperview() make.width.equalTo(165) make.height.equalTo(30) } let contentView = UIView() contentView.isUserInteractionEnabled = false jumpButton.addSubview(contentView) contentView.snp.makeConstraints { make in make.center.equalToSuperview() } let jumpTitleLabel = UILabel() jumpTitleLabel.text = .init(key: "A00273") jumpTitleLabel.font = .heading_h4 jumpTitleLabel.textColor = .text_1 jumpTitleLabel.textAlignment = .center contentView.addSubview(jumpTitleLabel) jumpTitleLabel.snp.makeConstraints { make in make.leading.equalToSuperview() make.verticalEdges.equalToSuperview() } let arrow = UIImageView.arrowImageView(size: 14) arrow.tintColor = .white contentView.addSubview(arrow) arrow.snp.makeConstraints { make in make.centerY.equalToSuperview() make.trailing.equalToSuperview() make.leading.equalTo(jumpTitleLabel.snp.trailing).offset(4) } return bg } private func buildRechargeSet() -> UIView { let container = UIView() container.backgroundColor = .fill container.layer.cornerRadius = 20 container.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner] let scrollView = UIScrollView() scrollView.showsVerticalScrollIndicator = false scrollView.showsHorizontalScrollIndicator = false container.addSubview(scrollView) scrollView.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview().inset(16) make.top.equalToSuperview().offset(20) make.bottom.equalToSuperview().offset(-view.safeBottomInset - 10) } let fakeView = UIView() scrollView.addSubview(fakeView) fakeView.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.top.width.equalToSuperview() make.height.equalTo(0) } rechargeView.delegate = self scrollView.addSubview(rechargeView) rechargeView.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.top.equalToSuperview() } rechargeButton.backgroundColor = .text_2 rechargeButton.layer.cornerRadius = 23.5 rechargeButton.setBackgroundImage(.primary_8, for: .normal) rechargeButton.setTitle(.init(key: "A00269"), for: .normal) rechargeButton.setTitleColor(.text_1, for: .normal) rechargeButton.titleLabel?.font = .heading_h3 rechargeButton.clipsToBounds = true rechargeButton.addAction(UIAction(handler: { [weak self] _ in guard let self else { return } guard let goods = rechargeView.curSelected else { return } // LNPurchaseManager.shared.purchaseProduct(goods: goods) showLoading() RechargeManager.shared.startRecharge(goods: goods) }), for: .touchUpInside) scrollView.addSubview(rechargeButton) rechargeButton.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.top.equalTo(rechargeView.snp.bottom).offset(20) make.height.equalTo(47) } let introduce = buildDescView() scrollView.addSubview(introduce) introduce.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.top.equalTo(rechargeButton.snp.bottom).offset(20) make.bottom.equalToSuperview() } return container } private func buildDescView() -> UIView { let container = UIView() let titleLabel = UILabel() titleLabel.text = .init(key: "A00270") titleLabel.font = .heading_h5 titleLabel.textColor = .text_5 container.addSubview(titleLabel) titleLabel.snp.makeConstraints { make in make.leading.top.equalToSuperview() } let textView = LNAutoSizeTextView() textView.backgroundColor = .clear textView.text = .init(key: "A00271") textView.font = .body_s textView.textColor = .text_4 textView.linkTextAttributes = [.foregroundColor: UIColor.primary_5] textView.isEditable = false container.addSubview(textView) textView.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview().inset(-4) make.top.equalTo(titleLabel.snp.bottom).offset(-7) make.bottom.equalToSuperview() } return container } } private class LNDiamondRechargeItemView: UIView { let diamondLabel = UILabel() let moneyLabel = UILabel() var goods: LNPurchaseGoodsVO? var isSelected: Bool = false { didSet { bg.isHidden = !isSelected } } private let bg = UIImageView() override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .fill_1 layer.cornerRadius = 12 clipsToBounds = true snp.makeConstraints { make in make.height.equalTo(76) } bg.image = .primary_7 addSubview(bg) bg.snp.makeConstraints { make in make.edges.equalToSuperview() } let cover = UIView() cover.backgroundColor = .fill_1 cover.layer.cornerRadius = 11 bg.addSubview(cover) cover.snp.makeConstraints { make in make.edges.equalToSuperview().inset(1) } let container = UIView() addSubview(container) container.snp.makeConstraints { make in make.center.equalToSuperview() } let diamondView = UIView() container.addSubview(diamondView) diamondView.snp.makeConstraints { make in make.centerX.top.equalToSuperview() } let diamond = UIImageView.diamondImageView() diamondView.addSubview(diamond) diamond.snp.makeConstraints { make in make.leading.centerY.equalToSuperview() make.width.height.equalTo(18) } diamondLabel.font = .heading_h2 diamondLabel.textColor = .text_5 diamondView.addSubview(diamondLabel) diamondLabel.snp.makeConstraints { make in make.verticalEdges.equalToSuperview() make.trailing.equalToSuperview() make.leading.equalTo(diamond.snp.trailing).offset(2) } moneyLabel.font = .body_s moneyLabel.textColor = .text_4 container.addSubview(moneyLabel) moneyLabel.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.top.equalTo(diamondView.snp.bottom).offset(4) make.bottom.equalToSuperview() } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } #if DEBUG import SwiftUI struct LNDiamondViewControllerPreview: UIViewControllerRepresentable { func makeUIViewController(context: Context) -> some UIViewController { LNDiamondViewController() } func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { } } #Preview(body: { LNDiamondViewControllerPreview() }) #endif