257 lines
9.0 KiB
Swift
257 lines
9.0 KiB
Swift
//
|
|
// NRStoreCoinsView.swift
|
|
// ReaderHive
|
|
//
|
|
// Created by 澜声世纪 on 2025/12/10.
|
|
//
|
|
|
|
import UIKit
|
|
import SnapKit
|
|
import HWPanModal
|
|
|
|
class NRStoreCoinsView: UIView {
|
|
|
|
var shortPlayId: String?
|
|
var videoId: String?
|
|
|
|
var buyFinishHandle: (() -> Void)?
|
|
|
|
var isShowTitle = false {
|
|
didSet {
|
|
updateLayout()
|
|
}
|
|
}
|
|
|
|
private lazy var dataArr: [[NRPayItem]] = []
|
|
|
|
private var selectedIndexPath: IndexPath?
|
|
|
|
private lazy var collectionViewLayout: UICollectionViewCompositionalLayout = {
|
|
let config = UICollectionViewCompositionalLayoutConfiguration()
|
|
config.interSectionSpacing = 12
|
|
|
|
let layout = UICollectionViewCompositionalLayout { [weak self] section, environment 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(environment: environment)
|
|
} else {
|
|
return self.smallLayoutSection(environment: environment)
|
|
}
|
|
}
|
|
layout.configuration = config
|
|
|
|
return layout
|
|
}()
|
|
|
|
private lazy var collectionView: NRCollectionView = {
|
|
let collectionView = NRCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.clipsToBounds = false
|
|
collectionView.isScrollEnabled = false
|
|
collectionView.register(NRStoreCoinsBigCell.self, forCellWithReuseIdentifier: "NRStoreCoinsBigCell")
|
|
collectionView.register(NRStoreCoinsSmallCell.self, forCellWithReuseIdentifier: "NRStoreCoinsSmallCell")
|
|
collectionView.register(NRStoreCoinsPackCell.self, forCellWithReuseIdentifier: "NRStoreCoinsPackCell")
|
|
collectionView.addObserver(self, forKeyPath: "contentSize", context: nil)
|
|
return collectionView
|
|
}()
|
|
|
|
private lazy var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .font(ofSize: 16, weight: .semibold)
|
|
label.textColor = .black
|
|
label.text = "purchase_coins".localized
|
|
return label
|
|
}()
|
|
|
|
deinit {
|
|
collectionView.removeObserver(self, forKeyPath: "contentSize")
|
|
}
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
nr_setupUI()
|
|
updateLayout()
|
|
}
|
|
|
|
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" {
|
|
updateLayout()
|
|
}
|
|
}
|
|
|
|
func setDataArr(_ arr: [NRPayItem]) {
|
|
self.dataArr.removeAll()
|
|
var bigArr: [NRPayItem] = []
|
|
var smallArr: [NRPayItem] = []
|
|
var coinPackArr: [NRPayItem] = []
|
|
|
|
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()
|
|
}
|
|
|
|
private func updateLayout() {
|
|
titleLabel.isHidden = !self.isShowTitle
|
|
|
|
let height = self.collectionView.contentSize.height + 1
|
|
|
|
if self.isShowTitle {
|
|
self.collectionView.snp.remakeConstraints { make in
|
|
make.top.equalTo(titleLabel.snp.bottom).offset(12)
|
|
make.left.right.bottom.equalToSuperview()
|
|
make.height.equalTo(height)
|
|
}
|
|
} else {
|
|
self.collectionView.snp.remakeConstraints { make in
|
|
make.top.equalToSuperview().offset(8)
|
|
make.left.right.bottom.equalToSuperview()
|
|
make.height.equalTo(height)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension NRStoreCoinsView {
|
|
|
|
private func nr_setupUI() {
|
|
addSubview(titleLabel)
|
|
addSubview(collectionView)
|
|
|
|
titleLabel.snp.makeConstraints { make in
|
|
make.left.equalToSuperview().offset(16)
|
|
make.top.equalToSuperview()
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extension NRStoreCoinsView {
|
|
|
|
private func bigLayoutSection(environment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection {
|
|
let containerWidth = environment.container.effectiveContentSize.width
|
|
let itemWidth = floor((containerWidth - 32 - 15) / 2)
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .absolute(itemWidth), heightDimension: .fractionalHeight(1)))
|
|
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(100)), subitems: [item])
|
|
group.interItemSpacing = .fixed(15)
|
|
|
|
let layoutSection = NSCollectionLayoutSection(group: group)
|
|
layoutSection.interGroupSpacing = 12
|
|
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
|
|
return layoutSection
|
|
}
|
|
|
|
private func smallLayoutSection(environment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection {
|
|
let containerWidth = environment.container.effectiveContentSize.width
|
|
let itemWidth = floor((containerWidth - 32 - 30) / 3)
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .absolute(itemWidth), heightDimension: .fractionalHeight(1)))
|
|
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(84)), subitems: [item])
|
|
group.interItemSpacing = .fixed(15)
|
|
|
|
let layoutSection = NSCollectionLayoutSection(group: group)
|
|
layoutSection.interGroupSpacing = 12
|
|
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 = 12
|
|
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
|
|
return layoutSection
|
|
}
|
|
}
|
|
|
|
//MARK: UICollectionViewDelegate UICollectionViewDataSource
|
|
extension NRStoreCoinsView: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let model = dataArr[indexPath.section][indexPath.row]
|
|
|
|
var identifier = "NRStoreCoinsBigCell"
|
|
if model.buy_type == .subCoins {
|
|
identifier = "NRStoreCoinsPackCell"
|
|
} else if model.size == .small {
|
|
identifier = "NRStoreCoinsSmallCell"
|
|
}
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! NRStoreCoinsCell
|
|
cell.model = model
|
|
cell.nr_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 = NRCoinsPackConfirmView()
|
|
view.shortPlayId = self.shortPlayId
|
|
view.videoId = self.videoId
|
|
view.model = model
|
|
view.buyFinishHandle = { [weak self] in
|
|
guard let self = self else { return }
|
|
self.buyFinishHandle?()
|
|
}
|
|
view.present(in: nil)
|
|
} else {
|
|
NRIapManager.manager.start(model: model, shortPlayId: self.shortPlayId, videoId: self.videoId) { [weak self] finish in
|
|
guard let self = self else { return }
|
|
if finish {
|
|
self.buyFinishHandle?()
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|