ReaderHive/ReaderHive/Class/Home/C/NRHomeNovelNewViewController.swift
2025-12-17 18:04:38 +08:00

143 lines
4.7 KiB
Swift

//
// NRHomeNovelNewViewController.swift
// ReaderHive
//
// Created by on 2025/12/4.
//
import UIKit
import SnapKit
import LYEmptyView
class NRHomeNovelNewViewController: NRViewController {
private lazy var dataArr: [NRNovelModel] = []
private lazy var page = 1
lazy var collectionViewLayout: UICollectionViewCompositionalLayout = {
let itemWidth = (UIScreen.width - 32 - 40) / 3
let itemHeight = 150 / 100 * itemWidth + 68
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1 / 3), heightDimension: .fractionalHeight(1)))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .fractionalWidth(1), heightDimension: .absolute(itemHeight)), subitems: [item])
group.interItemSpacing = .fixed(20)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = 18
section.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
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.ly_emptyView = NREmpty.nr_emptyView()
collectionView.contentInset = .init(top: 10, left: 0, bottom: UIScreen.safeBottom + 10, right: 0)
collectionView.nr_addRefreshHeader(insetTop: collectionView.contentInset.top) { [weak self] in
self?.handleHeaderRefresh(nil)
}
// collectionView.nr_addRefreshFooter(insetBottom: collectionView.contentInset.bottom) { [weak self] in
// self?.handleFooterRefresh(nil)
// }
collectionView.register(NRNovelGenresCell.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.edgesForExtendedLayout = .top
self.backgroundImageView.isHidden = true
self.title = "New".localized
configNavigationBack("arrow_left_icon_05")
nr_setupUI()
Task {
await requestDataArr(page: 1)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.nr_setNavigationStyle(titleColor: UINavigationBar.titleBlackColor)
}
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
Task {
await requestDataArr(page: 1)
self.collectionView.nr_endHeaderRefreshing()
}
}
override func handleFooterRefresh(_ completer: (() -> Void)?) {
Task {
await requestDataArr(page: self.page + 1)
self.collectionView.nr_endFooterRefreshing()
}
}
}
extension NRHomeNovelNewViewController {
private func nr_setupUI() {
view.addSubview(collectionView)
collectionView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.top.equalToSuperview().offset(UIScreen.navBarHeight)
}
}
}
//MARK: UICollectionViewDelegate UICollectionViewDataSource
extension NRHomeNovelNewViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NRNovelGenresCell
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 model = dataArr[indexPath.row]
let vc = NRNovelDetailViewController()
vc.novelId = model.id ?? ""
self.navigationController?.pushViewController(vc, animated: true)
}
}
extension NRHomeNovelNewViewController {
private func requestDataArr(page: Int) async {
guard let list = await NRHomeAPI.requestNewData() else { return }
if page == 1 {
self.dataArr.removeAll()
}
self.dataArr += list
self.page = page
self.collectionView.reloadData()
}
}