// // SPCoinRechargeView.swift // MoviaBox // // Created by 佳尔 on 2025/4/28. // import UIKit class SPCoinRechargeView: UIView { override var intrinsicContentSize: CGSize { return CGSize(width: kSPScreenWidth, height: 20 + 14 + 125) } private lazy var currentIndexPath: IndexPath = .init(row: 0, section: 0) ///充值成功回调 var rechargeFinishHandle: (() -> Void)? var dataArr: [SPPayTemplateItem]? { didSet { self.collectionView.reloadData() } } var userInfo: SPUserInfo? { didSet { coinLabel.text = "\(userInfo?.coin_left_total ?? 0)" } } //MARK: UI属性 private lazy var coinNameLabel: UILabel = { let label = UILabel() label.font = .fontRegular(ofSize: 14) label.textColor = .colorFFFFFF(alpha: 0.7) label.text = "Coins Balance:" return label }() private lazy var coinIconImageView: UIImageView = { let imageView = UIImageView(image: UIImage(named: "coin_icon_04")) return imageView }() private lazy var coinLabel: UILabel = { let label = UILabel() label.font = .fontRegular(ofSize: 14) label.textColor = .colorFFFFFF() return label }() private lazy var collectionViewLayout: UICollectionViewFlowLayout = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal layout.minimumLineSpacing = 8 layout.minimumInteritemSpacing = 8 layout.sectionInset = .init(top: 0, left: 16, bottom: 0, right: 16) layout.itemSize = CGSize(width: 120, height: 125) return layout }() private lazy var collectionView: SPCollectionView = { let collectionView = SPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) collectionView.delegate = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false SPCoinRechargeCell.registerCell(collectionView: collectionView) return collectionView }() override init(frame: CGRect) { super.init(frame: frame) _setupUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension SPCoinRechargeView { private func _setupUI() { addSubview(coinNameLabel) addSubview(coinIconImageView) addSubview(coinLabel) addSubview(collectionView) coinNameLabel.snp.makeConstraints { make in make.left.equalToSuperview().offset(16) make.top.equalToSuperview() make.height.equalTo(20) } coinIconImageView.snp.makeConstraints { make in make.centerY.equalTo(coinNameLabel) make.left.equalTo(coinNameLabel.snp.right).offset(4) } coinLabel.snp.makeConstraints { make in make.centerY.equalTo(coinNameLabel) make.left.equalTo(coinIconImageView.snp.right).offset(4) } collectionView.snp.makeConstraints { make in make.left.right.bottom.equalToSuperview() make.height.equalTo(125) } } } //MARK: -------------- UICollectionViewDelegate & UICollectionViewDataSource -------------- extension SPCoinRechargeView: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = SPCoinRechargeCell.dequeueReusableCell(collectionView: collectionView, indexPath: indexPath) cell.sp_isSelected = indexPath == currentIndexPath cell.model = dataArr?[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataArr?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { currentIndexPath = indexPath collectionView.reloadData() guard let model = self.dataArr?[indexPath.row] else { return } SPIAPManager.manager.startRecharge(model: model) { [weak self] finish in if finish { self?.rechargeFinishHandle?() } } } }