Fableon/Fableon/Object/Class/Home/C/FAGenresListViewController.swift
2025-10-23 16:54:27 +08:00

130 lines
4.3 KiB
Swift

//
// FAGenresListViewController.swift
// Fableon
//
// Created by on 2025/10/16.
//
import UIKit
class FAGenresListViewController: FAViewController {
var model: FACategoryModel?
private var dataArr: [FAShortPlayModel] = []
private var page = 1
private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
let width = floor((UIScreen.width - 32 - 16) / 3)
let height = 145 / 109 * width + 41
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 12
layout.minimumInteritemSpacing = 8
layout.itemSize = .init(width: width, height: height)
layout.sectionInset = .init(top: 0, left: 16, bottom: 0, right: 16)
return layout
}()
private lazy var collectionView: FACollectionView = {
let collectionView = FACollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.contentInset = .init(top: 20, left: 0, bottom: UIScreen.safeBottom + 10, right: 0)
collectionView.register(UINib(nibName: "FAGenresListCell", bundle: nil), forCellWithReuseIdentifier: "cell")
collectionView.fa_addRefreshHeader(insetTop: collectionView.contentInset.top) { [weak self] in
self?.handleHeaderRefresh(nil)
}
collectionView.fa_addRefreshFooter(insetBottom: 0) { [weak self] in
self?.handleFooterRefresh(nil)
}
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.title = model?.category_name
fa_setupLayout()
requestDataArr(page: 1, completer: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
fa_setNavigationStyle()
}
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
requestDataArr(page: 1) { [weak self] in
self?.collectionView.fa_endHeaderRefreshing()
}
}
override func handleFooterRefresh(_ completer: (() -> Void)?) {
requestDataArr(page: page + 1) { [weak self] in
self?.collectionView.fa_endFooterRefreshing()
}
}
}
extension FAGenresListViewController {
private func fa_setupLayout() {
view.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.top.equalToSuperview().offset(UIScreen.navBarHeight)
}
}
}
//MARK: UICollectionViewDelegate UICollectionViewDataSource
extension FAGenresListViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FAGenresListCell
cell.model = dataArr[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let model = dataArr[indexPath.row]
let vc = FAPlayerDetailViewController()
vc.shortPlayId = model.short_play_id
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension FAGenresListViewController {
private func requestDataArr(page: Int, completer: (() -> Void)?) {
guard let id = self.model?.id else { return }
FAAPI.requestCategoryVideoList(id: id, page: page) { [weak self] listModel in
guard let self = self else { return }
if let list = listModel?.list {
if page == 1 {
self.dataArr.removeAll()
}
self.dataArr += list
self.collectionView.reloadData()
self.page = page
}
completer?()
self.collectionView.fa_updateNoMoreDataState(listModel?.hasNextPage)
}
}
}