| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- //
- // LNUserSearchHistoryView.swift
- // Lanu
- //
- // Created by OneeChan on 2025/12/12.
- //
- import Foundation
- import UIKit
- import SnapKit
- protocol LNUserSearchHistoryViewDelegate: AnyObject {
- func onUserSearchHistoryView(view: LNUserSearchHistoryView, didClick history: String)
- }
- class LNUserSearchHistoryView: UIView {
- private var history: [String] = LNUserDefaults[.userSearchHistory, []]
- private let historyStackView = LNAutoFillStackView()
-
- private let roomViews = [LNUserSearchRoomCardView(), LNUserSearchRoomCardView()]
-
- weak var delegate: LNUserSearchHistoryViewDelegate?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
- reload()
- }
-
- func addRecord(_ keyword: String) {
- let fixed = keyword.trimmingCharacters(in: .whitespacesAndNewlines)
- guard !fixed.isEmpty else { return }
-
- if history.first == keyword {
- return
- }
-
- history.removeAll { $0 == fixed }
- history.insert(fixed, at: 0)
- if history.count >= 10 {
- history.removeLast()
- }
-
- LNUserDefaults[.userSearchHistory] = history
- reload()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNUserSearchHistoryView {
- private func reload() {
- var itemViews: [UIView] = []
- history.forEach { item in
- let container = UIView()
- container.layer.cornerRadius = 11
- container.layer.borderColor = UIColor.fill_4.cgColor
- container.layer.borderWidth = 1
- container.onTap { [weak self] in
- guard let self else { return }
- delegate?.onUserSearchHistoryView(view: self, didClick: item)
- }
- container.snp.makeConstraints { make in
- make.height.equalTo(22)
- }
-
- let title = UILabel()
- title.text = item
- title.font = .body_xs
- title.textColor = .text_3
- container.addSubview(title)
- title.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.horizontalEdges.equalToSuperview().inset(8)
- }
- container.layoutIfNeeded()
- itemViews.append(container)
- }
- historyStackView.update(itemViews)
- }
-
- private func setupViews() {
- let stackView = UIStackView()
- stackView.axis = .vertical
- stackView.spacing = 22
- addSubview(stackView)
- stackView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
-
- stackView.addArrangedSubview(buildHistory())
- // stackView.addArrangedSubview(buildHotRoom())
- }
-
- private func buildHistory() -> UIView {
- let container = UIView()
-
- let titleLabel = UILabel()
- titleLabel.text = .init(key: "A00242")
- titleLabel.font = .heading_h4
- titleLabel.textColor = .text_5
- container.addSubview(titleLabel)
- titleLabel.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalToSuperview().offset(12)
- }
-
- historyStackView.itemSpacing = 9
- historyStackView.spacing = 10
- container.addSubview(historyStackView)
- historyStackView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalTo(titleLabel.snp.bottom).offset(10)
- make.bottom.equalToSuperview()
- make.height.equalTo(0).priority(.low)
- }
-
- return container
- }
-
- private func buildHotRoom() -> UIView {
- let container = UIView()
-
- let titleLabel = UILabel()
- titleLabel.text = .init(key: "A00368")
- titleLabel.font = .heading_h5
- titleLabel.textColor = .text_5
- container.addSubview(titleLabel)
- titleLabel.snp.makeConstraints { make in
- make.leading.equalToSuperview().offset(16)
- make.top.equalToSuperview()
- }
-
- let stackView = UIStackView()
- stackView.axis = .horizontal
- stackView.spacing = 13
- stackView.distribution = .fillEqually
- container.addSubview(stackView)
- stackView.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview().inset(16)
- make.top.equalTo(titleLabel.snp.bottom).offset(16)
- make.bottom.equalToSuperview()
- }
-
- for roomView in roomViews {
- stackView.addArrangedSubview(roomView)
- }
-
- return container
- }
- }
|