108 lines
3.2 KiB
Swift
108 lines
3.2 KiB
Swift
//
|
|
// NRSearchResultView.swift
|
|
// ReaderHive
|
|
//
|
|
// Created by 澜声世纪 on 2025/11/25.
|
|
//
|
|
|
|
import UIKit
|
|
import SnapKit
|
|
import LYEmptyView
|
|
import YYCategories
|
|
|
|
class NRSearchResultView: UIView {
|
|
|
|
weak var viewModel: NRSearchViewModel?
|
|
|
|
var searchText: String = ""
|
|
|
|
lazy var dataArr: [NRNovelModel] = []
|
|
|
|
lazy var collectionViewLayout: UICollectionViewFlowLayout = {
|
|
let layout = UICollectionViewFlowLayout()
|
|
layout.itemSize = .init(width: UIScreen.width, height: 90)
|
|
layout.minimumLineSpacing = 16
|
|
layout.minimumInteritemSpacing = 16
|
|
return layout
|
|
}()
|
|
|
|
lazy var collectionView: NRCollectionView = {
|
|
let collectionView = NRCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.keyboardDismissMode = .onDrag
|
|
collectionView.contentInset = .init(top: 10, left: 0, bottom: UIScreen.safeBottom + 10, right: 0)
|
|
collectionView.ly_emptyView = NREmpty.nr_emptyView()
|
|
collectionView.register(NRSearchResultCell.self, forCellWithReuseIdentifier: "cell")
|
|
return collectionView
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
nr_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func search(_ text: String) {
|
|
if text.isEmpty {
|
|
self.dataArr.removeAll()
|
|
self.collectionView.reloadData()
|
|
return
|
|
}
|
|
self.searchText = text
|
|
|
|
Task {
|
|
await requestDataArr(text: self.searchText)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension NRSearchResultView {
|
|
|
|
private func nr_setupUI() {
|
|
addSubview(collectionView)
|
|
|
|
collectionView.snp.makeConstraints { make in
|
|
make.left.bottom.right.equalToSuperview()
|
|
make.top.equalToSuperview().offset(10)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: UICollectionViewDelegate UICollectionViewDataSource
|
|
extension NRSearchResultView: UICollectionViewDelegate, UICollectionViewDataSource {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NRSearchResultCell
|
|
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 = self.dataArr[indexPath.row]
|
|
let vc = NRNovelDetailViewController()
|
|
vc.novelId = model.id ?? ""
|
|
self.viewController?.navigationController?.pushViewController(vc, animated: true)
|
|
}
|
|
}
|
|
|
|
extension NRSearchResultView {
|
|
|
|
private func requestDataArr(text: String) async {
|
|
guard let list = await NRHomeAPI.requestSearchNovel(text: text) else { return }
|
|
guard text == self.searchText else { return }
|
|
|
|
self.dataArr = list
|
|
self.collectionView.reloadData()
|
|
}
|
|
|
|
}
|