// // NRExploreNovelGenresViewController.swift // ReaderHive // // Created by 长沙鸿瑶 on 2025/11/26. // import UIKit import SnapKit class NRExploreNovelGenresViewController: NRViewController { lazy var dataArr: [NRCategoryModel] = [] lazy var collectionViewLayout: UICollectionViewCompositionalLayout = { let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1))) let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(48)), subitems: [item]) group.interItemSpacing = .fixed(8) let section = NSCollectionLayoutSection(group: group) section.interGroupSpacing = 8 section.contentInsets = .init(top: 0, leading: 12, bottom: 0, trailing: 12) let configuration = UICollectionViewCompositionalLayoutConfiguration() let layout = UICollectionViewCompositionalLayout(section: section, configuration: configuration) return layout }() lazy var collectionView: NRCollectionView = { let collectionView = NRCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout) collectionView.delegate = self collectionView.dataSource = self collectionView.contentInset = .init(top: 0, left: 0, bottom: 10, right: 0) collectionView.register(NRExploreNovelGenresCell.self, forCellWithReuseIdentifier: "cell") return collectionView }() override func viewDidLoad() { super.viewDidLoad() self.backgroundImageView.isHidden = true self.view.backgroundColor = .clear nr_setupUI() Task { await requestDataArr() } } } extension NRExploreNovelGenresViewController { private func nr_setupUI() { view.addSubview(collectionView) collectionView.snp.makeConstraints { make in make.left.right.bottom.equalToSuperview() make.top.equalToSuperview().offset(16) } } } //MARK: UICollectionViewDelegate UICollectionViewDataSource extension NRExploreNovelGenresViewController: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NRExploreNovelGenresCell cell.textLabel.text = dataArr[indexPath.row].name return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataArr.count } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let vc = NRNovelGenresViewController() vc.model = self.dataArr[indexPath.row] self.navigationController?.pushViewController(vc, animated: true) } } extension NRExploreNovelGenresViewController { private func requestDataArr() async { guard let list = await NRHomeAPI.requestCategoryList() else { return } self.dataArr = list self.collectionView.reloadData() } }