//
|
// Home_Style_3_TCell.swift
|
// XQMuse
|
//
|
// Created by 无故事王国 on 2024/8/12.
|
//
|
|
import UIKit
|
import JQTools
|
import RxSwift
|
|
class Home_Style_3_TCell: UITableViewCell {
|
|
@IBOutlet weak var collectionView: UICollectionView!
|
var meditationModels = [MeditationModel]()
|
private var showType:DisplayType!
|
private let disposeBag = DisposeBag()
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
selectionStyle = .none
|
|
collectionView.collectionViewLayout = TestLeftRightCollectionViewFlowLayout(width: JQ_ScreenW, height: 213.5)
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.isPagingEnabled = false
|
collectionView.bounces = false
|
collectionView.showsHorizontalScrollIndicator = false
|
collectionView.register(UINib(nibName: "HomeRelaxBannerCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeRelaxBannerCCell")
|
collectionView.contentInset = UIEdgeInsets(top: 0, left: 100 , bottom: 0, right: 100)
|
|
}
|
|
func setModels(_ items:[MeditationModel],showType: DisplayType){
|
self.showType = showType
|
self.meditationModels = items
|
|
// if items.count >= 3 {
|
|
// }
|
|
collectionView.reloadData()
|
|
DispatchQueue.main.asyncAfter(delay: 0.1) {
|
let centerIndex = ceil(Double(Int(1000 / items.count)))
|
self.collectionView.scrollToItem(at: IndexPath(row: Int(centerIndex), section: 0), at: .centeredHorizontally, animated: false)
|
}
|
}
|
}
|
|
extension Home_Style_3_TCell:UICollectionViewDelegate & UICollectionViewDataSource{
|
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
print("--->\(collectionView.visibleCells)")
|
|
var showAtCell:UICollectionViewCell?
|
if collectionView.visibleCells.count == 2{
|
showAtCell = collectionView.visibleCells.last
|
}
|
|
if collectionView.visibleCells.count == 3{
|
showAtCell = collectionView.visibleCells[1]
|
}
|
|
if let cell = showAtCell,let indexPath = collectionView.indexPath(for: cell){
|
let realIndex = indexPath.row % 1000
|
collectionView.scrollToItem(at: IndexPath(row: realIndex, section: 0), at: .centeredHorizontally, animated: false)
|
|
}
|
|
// collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return meditationModels.count * 1000
|
}
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeRelaxBannerCCell", for: indexPath) as! HomeRelaxBannerCCell
|
let realIndex = indexPath.item % meditationModels.count
|
cell.setMeditationModel(meditationModels[realIndex],showType: showType)
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
let index = indexPath.item % meditationModels.count
|
|
let item = meditationModels[index]
|
|
if (item.chargeType == .payment || item.chargeType == .vipFree || item.id == 0) && (UserViewModel.getLoginInfo()?.token.isEmpty ?? true){
|
sceneDelegate?.checkisLoginState()
|
return
|
}
|
|
Services.getMeditationDetail(id: item.id).subscribe(onNext: {[weak self]data in
|
guard let weakSelf = self else { return }
|
if let m = data.data{
|
let isVip = m.isVip == .yes
|
if m.chargeType == .free || (isVip && m.chargeType == .vipFree) || (m.chargeType == .payment && m.isBuy == .yes){
|
let vc = HomeItemDetailVC(model: m)
|
JQ_currentViewController().jq_push(vc: vc)
|
return
|
}
|
|
if m.chargeType == .payment && m.isBuy == .no{
|
guard sceneDelegate!.checkisLoginState() else{return}
|
let vc = PaymentOrderVC(museItemModel: m,type: .muse,showType: weakSelf.showType)
|
JQ_currentViewController().jq_push(vc: vc)
|
return
|
}
|
|
if m.chargeType == .vipFree{
|
let vc = VIPCenterVC()
|
JQ_currentViewController().jq_push(vc: vc)
|
return
|
}
|
}
|
}).disposed(by: self.disposeBag)
|
}
|
}
|
|
extension Home_Style_3_TCell:UICollectionViewDelegateFlowLayout{
|
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
|
return CGSize(width: 148.25, height: 213.45)
|
}
|
}
|