189 lines
6.0 KiB
Swift
189 lines
6.0 KiB
Swift
//
|
|
// NRHomeNovelListViewController.swift
|
|
// ReaderHive
|
|
//
|
|
// Created by 澜声世纪 on 2025/11/21.
|
|
//
|
|
|
|
import UIKit
|
|
import SnapKit
|
|
|
|
|
|
class NRHomeNovelListViewController: NRViewController {
|
|
|
|
lazy var dataArr: [NRNovelModel] = []
|
|
lazy var page = 1
|
|
|
|
lazy var collectionViewLayout: NRWaterfallFlowLayout = {
|
|
let layout = NRWaterfallFlowLayout()
|
|
layout.delegate = self
|
|
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.nr_addRefreshFooter { [weak self] in
|
|
self?.handleFooterRefresh(nil)
|
|
}
|
|
collectionView.register(NRHomeNovelListCell.self, forCellWithReuseIdentifier: "cell")
|
|
collectionView.register(NRHomeNovelListTextCell.self, forCellWithReuseIdentifier: "textCell")
|
|
return collectionView
|
|
}()
|
|
|
|
lazy var listTitleView: NRHomeNovelHeaderContentView = {
|
|
let view = NRHomeNovelHeaderContentView()
|
|
view.titleLabel.text = "reader_home_title6".localized
|
|
return view
|
|
}()
|
|
|
|
@MainActor deinit {
|
|
NotificationCenter.default.removeObserver(self)
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
self.view.backgroundColor = .clear
|
|
self.backgroundImageView.isHidden = true
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(networkStatusDidChangeNotification), name: NRNetworkReachableManager.networkStatusDidChangeNotification, object: nil)
|
|
|
|
nr_setupUI()
|
|
|
|
Task {
|
|
await requestDataArr(page: 1)
|
|
}
|
|
}
|
|
|
|
|
|
override func listScrollView() -> UIScrollView {
|
|
return collectionView
|
|
}
|
|
|
|
override func handleHeaderRefresh(_ completer: (() -> Void)?) {
|
|
Task {
|
|
await requestDataArr(page: 1)
|
|
completer?()
|
|
}
|
|
}
|
|
|
|
override func handleFooterRefresh(_ completer: (() -> Void)?) {
|
|
Task {
|
|
await requestDataArr(page: self.page + 1)
|
|
self.collectionView.nr_endFooterRefreshing()
|
|
completer?()
|
|
}
|
|
}
|
|
|
|
@objc private func networkStatusDidChangeNotification() {
|
|
if NRNetworkReachableManager.manager.isReachable == true, self.dataArr.isEmpty {
|
|
Task {
|
|
await requestDataArr(page: 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension NRHomeNovelListViewController {
|
|
|
|
private func nr_setupUI() {
|
|
view.addSubview(listTitleView)
|
|
view.addSubview(collectionView)
|
|
|
|
|
|
listTitleView.snp.makeConstraints { make in
|
|
make.left.right.top.equalToSuperview()
|
|
}
|
|
|
|
collectionView.snp.makeConstraints { make in
|
|
make.left.right.bottom.equalToSuperview()
|
|
// make.top.equalTo(listTitleView.snp.bottom)
|
|
make.top.equalToSuperview().offset(36)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: UICollectionViewDelegate UICollectionViewDataSource
|
|
extension NRHomeNovelListViewController: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let model = dataArr[indexPath.row]
|
|
let cellType = self.getCellType(indexPath: indexPath)
|
|
if cellType == 1 {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "textCell", for: indexPath) as! NRHomeNovelListTextCell
|
|
cell.model = model
|
|
return cell
|
|
} else {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NRHomeNovelListCell
|
|
cell.model = model
|
|
return cell
|
|
}
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
return self.dataArr.count
|
|
}
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
self.didScrollCallback?(scrollView)
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
let vc = NRNovelDetailViewController()
|
|
vc.novelId = self.dataArr[indexPath.row].id ?? ""
|
|
self.navigationController?.pushViewController(vc, animated: true)
|
|
}
|
|
|
|
func getCellType(indexPath: IndexPath) -> Int {
|
|
let num = indexPath.row + 1
|
|
if num % 3 == 2 {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
}
|
|
|
|
//MARK: NRWaterfallMutiSectionDelegate
|
|
extension NRHomeNovelListViewController: NRWaterfallMutiSectionDelegate {
|
|
func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
|
|
let cellType = self.getCellType(indexPath: indexPath)
|
|
if cellType == 1 {
|
|
return NRHomeNovelListTextCell.contentHeight
|
|
} else {
|
|
let coverHeight = NRHomeNovelListCell.coverHeight
|
|
return coverHeight + 86
|
|
}
|
|
}
|
|
|
|
func lineSpacing(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, section: Int) -> CGFloat {
|
|
return 16
|
|
}
|
|
|
|
func interitemSpacing(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, section: Int) -> CGFloat {
|
|
return 13
|
|
}
|
|
|
|
func insetForSection(collectionView collection: UICollectionView, layout: NRWaterfallFlowLayout, section: Int) -> UIEdgeInsets {
|
|
return .init(top: 0, left: 15, bottom: 0, right: 15)
|
|
}
|
|
}
|
|
|
|
extension NRHomeNovelListViewController {
|
|
|
|
private func requestDataArr(page: Int) async {
|
|
guard let list = await NRHomeAPI.requestHomeNewData(page: page) else { return }
|
|
|
|
if page == 1 {
|
|
self.dataArr.removeAll()
|
|
}
|
|
self.dataArr += list
|
|
self.page = page
|
|
self.collectionView.reloadData()
|
|
}
|
|
|
|
}
|