ReaderHive/ReaderHive/Class/Home/V/NRSearchRecordView.swift
2026-01-27 15:55:28 +08:00

137 lines
4.3 KiB
Swift

//
// NRSearchRecordView.swift
// ReaderHive
//
// Created by on 2025/11/25.
//
import UIKit
import collection_view_layouts
import SnapKit
import YYCategories
class NRSearchRecordView: UIView {
var didSearch: ((_ text: String) -> Void)?
var didDelete: (() -> Void)?
var dataArr: [String] = [] {
didSet {
self.collectionView.reloadData()
}
}
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = .font(ofSize: 16, weight: .semibold)
label.textColor = .black
label.text = "reader_searches".localized
return label
}()
private lazy var deleteButton: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "delete_icon_01"), for: .normal)
button.addAction(UIAction(handler: { [weak self] _ in
self?.didDelete?()
}), for: .touchUpInside)
return button
}()
private lazy var collectionViewLayout: TagsLayout = {
let layout = TagsLayout()
layout.delegate = self
layout.contentPadding = ItemsPadding(horizontal: 16, vertical: 0)
layout.cellsPadding = ItemsPadding(horizontal: 12, vertical: 12)
return layout
}()
private lazy var collectionView: NRCollectionView = {
let collectionView = NRCollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.addObserver(self, forKeyPath: "contentSize", context: nil)
collectionView.register(NRSearchRecordCell.self, forCellWithReuseIdentifier: "cell")
return collectionView
}()
deinit {
self.collectionView.removeObserver(self, forKeyPath: "contentSize")
}
override init(frame: CGRect) {
super.init(frame: frame)
fa_setupLayout()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "contentSize" {
let height = self.collectionView.contentSize.height
self.collectionView.snp.updateConstraints { make in
make.height.equalTo(height + 1)
}
}
}
}
extension NRSearchRecordView {
private func fa_setupLayout() {
addSubview(titleLabel)
addSubview(deleteButton)
addSubview(collectionView)
titleLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(16)
make.centerY.equalTo(deleteButton)
}
deleteButton.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-16)
make.top.equalToSuperview()
}
collectionView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(36)
make.left.equalToSuperview()
make.right.equalToSuperview()
make.bottom.equalToSuperview()
make.height.equalTo(1)
}
}
}
//MARK: UICollectionViewDataSource UICollectionViewDataSource
extension NRSearchRecordView: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! NRSearchRecordCell
cell.textLabel.text = dataArr[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.didSearch?(dataArr[indexPath.row])
}
}
//MARK: LayoutDelegate
extension NRSearchRecordView: LayoutDelegate {
func cellSize(indexPath: IndexPath) -> CGSize {
let text = dataArr[indexPath.row]
let size = text.size(NRSearchRecordCell.TextFont, .init(width: UIScreen.width, height: 24))
return .init(width: size.width + 16, height: 20)
}
}