181 lines
6.9 KiB
Swift
181 lines
6.9 KiB
Swift
//
|
|
// XSHomeNewViewController.swift
|
|
// XSeri
|
|
//
|
|
// Created by 长沙鸿瑶 on 2025/12/31.
|
|
//
|
|
|
|
import UIKit
|
|
import SnapKit
|
|
|
|
class XSHomeNewViewController: XSHomeChildViewController {
|
|
|
|
private lazy var dataArr: [XSShortModel] = []
|
|
private lazy var page: Int = 1
|
|
|
|
|
|
private lazy var layout: UICollectionViewCompositionalLayout = {
|
|
let layout = UICollectionViewCompositionalLayout { section, environment in
|
|
|
|
|
|
if section == 0 {
|
|
let headerItem = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(32)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .absolute(210), heightDimension: .absolute(360)), subitems: [item])
|
|
let layoutSection = NSCollectionLayoutSection(group: group)
|
|
layoutSection.interGroupSpacing = 15
|
|
layoutSection.orthogonalScrollingBehavior = .continuous
|
|
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
|
|
layoutSection.boundarySupplementaryItems = [headerItem]
|
|
return layoutSection
|
|
} else {
|
|
let headerItem = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(57)), elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)))
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(146)), subitems: [item])
|
|
|
|
let layoutSection = NSCollectionLayoutSection(group: group)
|
|
layoutSection.interGroupSpacing = 12
|
|
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
|
|
layoutSection.boundarySupplementaryItems = [headerItem]
|
|
return layoutSection
|
|
}
|
|
}
|
|
return layout
|
|
}()
|
|
|
|
private lazy var collectionView: XSCollectionView = {
|
|
let collectionView = XSCollectionView(frame: .zero, collectionViewLayout: layout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.contentInset = .init(top: 0, left: 0, bottom: XSScreen.customTabBarHeight + 10, right: 0)
|
|
collectionView.xs_addRefreshHeader { [weak self] in
|
|
self?.handleHeaderRefresh(nil)
|
|
}
|
|
collectionView.xs_addRefreshFooter(insetBottom: 0) { [weak self] in
|
|
self?.handleFooterRefresh(nil)
|
|
}
|
|
collectionView.register(XSHomeNewBigCell.self, forCellWithReuseIdentifier: "XSHomeNewBigCell")
|
|
collectionView.register(XSHomeNewCell.self, forCellWithReuseIdentifier: "XSHomeNewCell")
|
|
collectionView.register(XSHomeNewTitleView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "title")
|
|
return collectionView
|
|
}()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
xs_setupUI()
|
|
|
|
Task {
|
|
await requestDataArr(page: 1)
|
|
}
|
|
}
|
|
|
|
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
|
|
Task {
|
|
await requestDataArr(page: 1)
|
|
self.collectionView.xs_endHeaderRefreshing()
|
|
}
|
|
}
|
|
|
|
override func handleFooterRefresh(_ completer: (() -> Void)?) {
|
|
Task {
|
|
await requestDataArr(page: self.page + 1)
|
|
self.collectionView.xs_endFooterRefreshing()
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
extension XSHomeNewViewController {
|
|
|
|
private func xs_setupUI() {
|
|
view.addSubview(collectionView)
|
|
|
|
collectionView.snp.makeConstraints { make in
|
|
make.left.right.bottom.equalToSuperview()
|
|
make.top.equalToSuperview().offset(self.topHeight + 13)
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: UICollectionViewDelegate UICollectionViewDataSource
|
|
extension XSHomeNewViewController: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
var model: XSShortModel
|
|
if indexPath.section == 0 {
|
|
model = self.dataArr[indexPath.row]
|
|
} else {
|
|
model = self.dataArr[indexPath.row + 3]
|
|
}
|
|
|
|
|
|
if indexPath.section == 0 {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XSHomeNewBigCell", for: indexPath) as! XSHomeNewBigCell
|
|
cell.model = model
|
|
return cell
|
|
} else {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "XSHomeNewCell", for: indexPath) as! XSHomeNewCell
|
|
cell.model = model
|
|
return cell
|
|
}
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
let totalCount = self.dataArr.count
|
|
if section == 0 {
|
|
return min(3, totalCount)
|
|
} else {
|
|
return totalCount - 3
|
|
}
|
|
}
|
|
|
|
func numberOfSections(in collectionView: UICollectionView) -> Int {
|
|
if self.dataArr.count > 3 {
|
|
return 2
|
|
} else if self.dataArr.count > 0 {
|
|
return 1
|
|
} else {
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
|
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "title", for: indexPath) as! XSHomeNewTitleView
|
|
view.titleLabel.text = "New Releases".localized
|
|
return view
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
var model: XSShortModel
|
|
if indexPath.section == 0 {
|
|
model = self.dataArr[indexPath.row]
|
|
} else {
|
|
model = self.dataArr[indexPath.row + 3]
|
|
}
|
|
let vc = XSShortDetailViewController()
|
|
vc.shortId = model.short_play_id
|
|
self.navigationController?.pushViewController(vc, animated: true)
|
|
}
|
|
}
|
|
|
|
extension XSHomeNewViewController {
|
|
|
|
private func requestDataArr(page: Int) async {
|
|
guard let list = await XSHomeAPI.requestHomeNew(page: page) else { return }
|
|
if page == 1 {
|
|
self.dataArr.removeAll()
|
|
}
|
|
self.page = page
|
|
self.dataArr += list
|
|
|
|
self.collectionView.reloadData()
|
|
}
|
|
|
|
}
|