125 lines
4.0 KiB
Swift
125 lines
4.0 KiB
Swift
//
|
|
// VPSearchResultView.swift
|
|
// Veloria
|
|
//
|
|
// Created by 湖南秦九 on 2025/5/24.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class VPSearchResultView: UIView {
|
|
|
|
var viewModel: VPSearchViewModel?
|
|
|
|
private lazy var dataArr: [VPShortModel] = []
|
|
///当前数据对应的词
|
|
private lazy var currentDataText = ""
|
|
|
|
private(set) lazy var searchText: String = ""
|
|
|
|
|
|
|
|
//MARK: UI属性
|
|
private lazy var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.font = .fontMedium(ofSize: 13)
|
|
label.textColor = .colorFFFFFF()
|
|
label.text = "Search Results".localized
|
|
return label
|
|
}()
|
|
|
|
private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
|
|
let layout = UICollectionViewFlowLayout()
|
|
layout.itemSize = .init(width: UIScreen.width - 30, height: 108)
|
|
layout.sectionInset = .init(top: 0, left: 15, bottom: 0, right: 15)
|
|
layout.minimumLineSpacing = 10
|
|
return layout
|
|
}()
|
|
|
|
private lazy var collectionView: VPCollectionView = {
|
|
let collectionView = VPCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
|
|
collectionView.delegate = self
|
|
collectionView.dataSource = self
|
|
collectionView.contentInset = .init(top: 0, left: 0, bottom: UIScreen.tabbarSafeBottomMargin + 10, right: 0)
|
|
collectionView.register(VPSearchResultCell.self, forCellWithReuseIdentifier: "cell")
|
|
collectionView.vp_addNormalEmpty(image: UIImage(named: "empty_image_02"), title: "kEmptyTitle02".localized, des: "kEmptyDes02".localized)
|
|
collectionView.keyboardDismissMode = .onDrag
|
|
return collectionView
|
|
}()
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
vp_setupUI()
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func search(text: String) {
|
|
searchText = text
|
|
requestSearch(text: text)
|
|
}
|
|
|
|
}
|
|
|
|
extension VPSearchResultView {
|
|
|
|
private func vp_setupUI() {
|
|
addSubview(titleLabel)
|
|
addSubview(collectionView)
|
|
|
|
titleLabel.snp.makeConstraints { make in
|
|
make.left.equalToSuperview().offset(15)
|
|
make.top.equalToSuperview().offset(20)
|
|
}
|
|
|
|
collectionView.snp.makeConstraints { make in
|
|
make.left.right.bottom.equalToSuperview()
|
|
make.top.equalTo(titleLabel.snp.bottom).offset(10)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
//MARK: -------------- UICollectionViewDataSource UICollectionViewDelegate --------------
|
|
extension VPSearchResultView: UICollectionViewDataSource, UICollectionViewDelegate {
|
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! VPSearchResultCell
|
|
cell.searchText = self.currentDataText
|
|
cell.model = 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 = VPDetailPlayerViewController()
|
|
vc.shortPlayId = model.short_play_id
|
|
self.viewController?.navigationController?.pushViewController(vc, animated: true)
|
|
|
|
self.viewModel?.addHistory(text: self.searchText)
|
|
}
|
|
}
|
|
|
|
extension VPSearchResultView {
|
|
|
|
private func requestSearch(text: String) {
|
|
VPHomeAPI.requestSearch(text: text) { [weak self] list in
|
|
guard let self = self else { return }
|
|
if self.searchText != text { return; }
|
|
|
|
if let list = list {
|
|
self.dataArr = list
|
|
self.currentDataText = text
|
|
self.collectionView.reloadData()
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|