MoviaBox/Thimra/Class/Home/View/SPSearchAssociativeView.swift
2025-04-22 17:30:04 +08:00

102 lines
2.9 KiB
Swift

//
// SPSearchAssociativeView.swift
// Thimra
//
// Created by on 2025/4/18.
//
import UIKit
class SPSearchAssociativeView: UIView {
var viewModel: SPSearchViewModel?
private(set) lazy var searchText: String = ""
private lazy var dataArr: [SPShortModel] = []
private lazy var tableView: SPTableView = {
let tableView = SPTableView(frame: .zero, style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = 60
tableView.separatorColor = .color1E1F2C()
tableView.separatorInset = .init(top: 0, left: 16, bottom: 0, right: 16)
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: kSPTabbarSafeBottomMargin, right: 0)
tableView.keyboardDismissMode = .onDrag
SPSearchAssociativeCell.registerCell(tableView: tableView)
return tableView
}()
override init(frame: CGRect) {
super.init(frame: frame)
_setupUI()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func search(text: String) {
self.searchText = text
self.requestSearch(text: text)
}
}
extension SPSearchAssociativeView {
private func _setupUI() {
addSubview(tableView)
tableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
}
//MARK: -------------- UITableViewDelegate & UITableViewDataSource --------------
extension SPSearchAssociativeView: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = SPSearchAssociativeCell.dequeueReusableCell(tableView: tableView, indexPath: indexPath)
cell.searchText = self.searchText
cell.model = dataArr[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArr.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let model = self.dataArr[indexPath.row]
self.viewModel?.addSearchHistory(text: self.searchText)
let vc = SPPlayerDetailViewController()
vc.shortPlayId = model.short_play_id
self.viewController?.navigationController?.pushViewController(vc, animated: true)
}
}
extension SPSearchAssociativeView {
private func requestSearch(text: String) {
SPHomeAPI.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.tableView.reloadData()
}
}
}
}