Fableon/Fableon/Class/Home/C/FAGenresViewController.swift
2025-10-23 15:53:39 +08:00

105 lines
3.2 KiB
Swift

//
// FAGenresViewController.swift
// Fableon
//
// Created by 鸿 on 2025/10/16.
//
import UIKit
class FAGenresViewController: FAViewController {
private lazy var bgColorArr: [UIColor] = [
._5_CA_8_FF_0_5,
.A_783_FF_0_5,
.FF_5977_0_5,
.FE_8_CCA_0_5,
.FCB_072_0_5
]
private lazy var dataArr: [FACategoryModel] = []
private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 10
layout.itemSize = .init(width: UIScreen.width - 32, height: 140)
return layout
}()
private lazy var collectionView: FACollectionView = {
let collectionView = FACollectionView(frame: .zero, collectionViewLayout: self.collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.contentInset = .init(top: 24, left: 0, bottom: UIScreen.safeBottom + 10, right: 0)
collectionView.register(UINib(nibName: "FAGenresCell", bundle: nil), forCellWithReuseIdentifier: "cell")
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Genres".localized
fa_setupLayout()
requestDataArr()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.fa_setNavigationStyle()
}
}
extension FAGenresViewController {
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 FAGenresViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! FAGenresCell
cell.bgView.backgroundColor = bgColorArr[indexPath.row % 5]
cell.model = self.dataArr[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dataArr.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = FAGenresListViewController()
vc.model = dataArr[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension FAGenresViewController {
private func requestDataArr() {
FAAPI.requestGenresData { [weak self] list in
guard let self = self else { return }
guard let list = list else { return }
self.dataArr = list
self.collectionView.reloadData()
}
}
}