Fableon/Fableon/Object/Class/Store/V/FAStoreCoinsView.swift
湖北秦九 008d5b55b8 1
2025-11-21 09:12:46 +08:00

217 lines
7.8 KiB
Swift

//
// FAStoreCoinsView.swift
// Fableon
//
// Created by on 2025/10/24.
//
import UIKit
class FAStoreCoinsView: UIView {
var shortPlayId: String?
var videoId: String?
var buyFinishHandle: (() -> Void)?
private lazy var dataArr: [[FAPayItem]] = []
private var selectedIndexPath: IndexPath?
private lazy var collectionViewLayout: UICollectionViewCompositionalLayout = {
let config = UICollectionViewCompositionalLayoutConfiguration()
config.interSectionSpacing = 10
let layout = UICollectionViewCompositionalLayout { [weak self] section, _ in
guard let self = self else { return nil}
guard let model = dataArr[section].first else { return nil }
if model.buy_type == .subCoins {
return self.coinsBigLayoutSection()
} else if model.size == .big {
return self.bigLayoutSection()
} else {
return self.smallLayoutSection()
}
}
layout.configuration = config
return layout
}()
private lazy var collectionView: FACollectionView = {
let collectionView = FACollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.clipsToBounds = false
collectionView.isScrollEnabled = false
collectionView.register(FAStoreCoinsBigCell.self, forCellWithReuseIdentifier: "FAStoreCoinsBigCell")
collectionView.register(FAStoreCoinsSmallCell.self, forCellWithReuseIdentifier: "FAStoreCoinsSmallCell")
collectionView.register(FAStoreCoinsPackCell.self, forCellWithReuseIdentifier: "FAStoreCoinsPackCell")
collectionView.addObserver(self, forKeyPath: "contentSize", context: nil)
return collectionView
}()
deinit {
collectionView.removeObserver(self, forKeyPath: "contentSize")
}
override init(frame: CGRect) {
super.init(frame: frame)
fa_setupLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "contentSize" {
let height = self.collectionView.contentSize.height + 1
self.collectionView.snp.updateConstraints { make in
make.height.equalTo(height)
}
}
}
func setDataArr(_ arr: [FAPayItem]) {
self.dataArr.removeAll()
var bigArr: [FAPayItem] = []
var smallArr: [FAPayItem] = []
var coinPackArr: [FAPayItem] = []
arr.forEach {
if $0.buy_type == .subCoins {
coinPackArr.append($0)
} else if $0.size == .big {
bigArr.append($0)
} else {
smallArr.append($0)
}
}
if bigArr.count > 0 {
self.dataArr.append(bigArr)
}
if coinPackArr.count > 0 {
self.dataArr.append(coinPackArr)
}
if smallArr.count > 0 {
self.dataArr.append(smallArr)
}
self.collectionView.reloadData()
}
}
extension FAStoreCoinsView {
private func fa_setupLayout() {
addSubview(collectionView)
collectionView.snp.makeConstraints { make in
// make.edges.equalToSuperview()
make.top.equalToSuperview().offset(8)
make.left.right.bottom.equalToSuperview()
make.height.equalTo(1)
}
}
}
extension FAStoreCoinsView {
private func bigLayoutSection() -> NSCollectionLayoutSection {
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1)))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(100)), subitems: [item])
group.interItemSpacing = .fixed(1)
let layoutSection = NSCollectionLayoutSection(group: group)
layoutSection.interGroupSpacing = 10
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
return layoutSection
}
private func smallLayoutSection() -> NSCollectionLayoutSection {
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1 / 3), heightDimension: .fractionalHeight(1)))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(121)), subitems: [item])
group.interItemSpacing = .fixed(8)
let layoutSection = NSCollectionLayoutSection(group: group)
layoutSection.interGroupSpacing = 10
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
return layoutSection
}
private func coinsBigLayoutSection() -> NSCollectionLayoutSection {
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(84)), subitems: [item])
let layoutSection = NSCollectionLayoutSection(group: group)
layoutSection.interGroupSpacing = 10
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
return layoutSection
}
}
//MARK: UICollectionViewDelegate UICollectionViewDataSource
extension FAStoreCoinsView: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let model = dataArr[indexPath.section][indexPath.row]
var identifier = "FAStoreCoinsBigCell"
if model.buy_type == .subCoins {
identifier = "FAStoreCoinsPackCell"
} else if model.size == .small {
identifier = "FAStoreCoinsSmallCell"
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! FAStoreCoinsCell
cell.model = model
cell.fa_isSelected = selectedIndexPath == indexPath
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArr[section].count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return dataArr.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let model = dataArr[indexPath.section][indexPath.row]
self.selectedIndexPath = indexPath
collectionView.reloadData()
if model.buy_type == .subCoins {
let view = FACoinPackConfirmView()
view.shortPlayId = self.shortPlayId
view.videoId = self.videoId
view.model = model
view.buyFinishHandle = { [weak self] in
guard let self = self else { return }
FALogin.manager.requestUserInfo(completer: nil)
self.buyFinishHandle?()
}
view.present(in: nil)
} else {
FAIapManager.manager.start(model: model, shortPlayId: self.shortPlayId, videoId: self.videoId) { [weak self] finish in
guard let self = self else { return }
if finish {
FALogin.manager.requestUserInfo(completer: nil)
self.buyFinishHandle?()
}
}
}
}
}