LNUserSearchHistoryView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // LNUserSearchHistoryView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/12.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. protocol LNUserSearchHistoryViewDelegate: AnyObject {
  11. func onUserSearchHistoryView(view: LNUserSearchHistoryView, didClick history: String)
  12. }
  13. class LNUserSearchHistoryView: UIView {
  14. private var history: [String] = LNUserDefaults[.userSearchHistory, []]
  15. private let historyStackView = LNAutoFillStackView()
  16. private let roomViews = [LNUserSearchRoomCardView(), LNUserSearchRoomCardView()]
  17. weak var delegate: LNUserSearchHistoryViewDelegate?
  18. override init(frame: CGRect) {
  19. super.init(frame: frame)
  20. setupViews()
  21. reload()
  22. }
  23. func addRecord(_ keyword: String) {
  24. let fixed = keyword.trimmingCharacters(in: .whitespacesAndNewlines)
  25. guard !fixed.isEmpty else { return }
  26. if history.first == keyword {
  27. return
  28. }
  29. history.removeAll { $0 == fixed }
  30. history.insert(fixed, at: 0)
  31. if history.count >= 10 {
  32. history.removeLast()
  33. }
  34. LNUserDefaults[.userSearchHistory] = history
  35. reload()
  36. }
  37. required init?(coder: NSCoder) {
  38. fatalError("init(coder:) has not been implemented")
  39. }
  40. }
  41. extension LNUserSearchHistoryView {
  42. private func reload() {
  43. var itemViews: [UIView] = []
  44. history.forEach { item in
  45. let container = UIView()
  46. container.layer.cornerRadius = 11
  47. container.layer.borderColor = UIColor.fill_4.cgColor
  48. container.layer.borderWidth = 1
  49. container.onTap { [weak self] in
  50. guard let self else { return }
  51. delegate?.onUserSearchHistoryView(view: self, didClick: item)
  52. }
  53. container.snp.makeConstraints { make in
  54. make.height.equalTo(22)
  55. }
  56. let title = UILabel()
  57. title.text = item
  58. title.font = .body_xs
  59. title.textColor = .text_3
  60. container.addSubview(title)
  61. title.snp.makeConstraints { make in
  62. make.centerY.equalToSuperview()
  63. make.horizontalEdges.equalToSuperview().inset(8)
  64. }
  65. container.layoutIfNeeded()
  66. itemViews.append(container)
  67. }
  68. historyStackView.update(itemViews)
  69. }
  70. private func setupViews() {
  71. let stackView = UIStackView()
  72. stackView.axis = .vertical
  73. stackView.spacing = 22
  74. addSubview(stackView)
  75. stackView.snp.makeConstraints { make in
  76. make.edges.equalToSuperview()
  77. }
  78. stackView.addArrangedSubview(buildHistory())
  79. // stackView.addArrangedSubview(buildHotRoom())
  80. }
  81. private func buildHistory() -> UIView {
  82. let container = UIView()
  83. let titleLabel = UILabel()
  84. titleLabel.text = .init(key: "A00242")
  85. titleLabel.font = .heading_h4
  86. titleLabel.textColor = .text_5
  87. container.addSubview(titleLabel)
  88. titleLabel.snp.makeConstraints { make in
  89. make.horizontalEdges.equalToSuperview().inset(16)
  90. make.top.equalToSuperview().offset(12)
  91. }
  92. historyStackView.itemSpacing = 9
  93. historyStackView.spacing = 10
  94. container.addSubview(historyStackView)
  95. historyStackView.snp.makeConstraints { make in
  96. make.horizontalEdges.equalToSuperview().inset(16)
  97. make.top.equalTo(titleLabel.snp.bottom).offset(10)
  98. make.bottom.equalToSuperview()
  99. make.height.equalTo(0).priority(.low)
  100. }
  101. return container
  102. }
  103. private func buildHotRoom() -> UIView {
  104. let container = UIView()
  105. let titleLabel = UILabel()
  106. titleLabel.text = .init(key: "A00368")
  107. titleLabel.font = .heading_h5
  108. titleLabel.textColor = .text_5
  109. container.addSubview(titleLabel)
  110. titleLabel.snp.makeConstraints { make in
  111. make.leading.equalToSuperview().offset(16)
  112. make.top.equalToSuperview()
  113. }
  114. let stackView = UIStackView()
  115. stackView.axis = .horizontal
  116. stackView.spacing = 13
  117. stackView.distribution = .fillEqually
  118. container.addSubview(stackView)
  119. stackView.snp.makeConstraints { make in
  120. make.horizontalEdges.equalToSuperview().inset(16)
  121. make.top.equalTo(titleLabel.snp.bottom).offset(16)
  122. make.bottom.equalToSuperview()
  123. }
  124. for roomView in roomViews {
  125. stackView.addArrangedSubview(roomView)
  126. }
  127. return container
  128. }
  129. }