O2SearchHistoryView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // O2SearchHistoryView.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2021/5/24.
  6. // Copyright © 2021 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. protocol O2SearchHistoryDelegate {
  11. func clickToSearchTag(tag: String)
  12. }
  13. class O2SearchHistoryView: UIView {
  14. var delegate: O2SearchHistoryDelegate?
  15. var tagsListView: UICollectionView?
  16. var deleteView: UIView?
  17. var historyList: [String] = []
  18. override init(frame: CGRect) {
  19. super.init(frame: frame)
  20. self.deleteView = UIView(frame: CGRect(x: 0, y: 10, width: self.width, height: 30))
  21. self.addSubview(self.deleteView!)
  22. let layout = UICollectionViewFlowLayout()
  23. layout.minimumLineSpacing = 5
  24. layout.minimumInteritemSpacing = 5
  25. self.tagsListView = UICollectionView(frame: CGRect(x: 15, y: 50, width: self.width - 30, height: 250), collectionViewLayout: layout)
  26. self.tagsListView?.delegate = self
  27. self.tagsListView?.dataSource = self
  28. self.tagsListView?.backgroundColor = .clear
  29. self.tagsListView?.register(O2SearchHistoryCell.self, forCellWithReuseIdentifier: "O2SearchHistoryCell")
  30. self.addSubview(self.tagsListView!)
  31. let delIcon = UIImageView(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
  32. delIcon.image = UIImage(named: "icon_delete_sign")
  33. let delLabel = UILabel(frame: .zero)
  34. let text = L10n.Search.deleteAllSearchHistory
  35. let fontSize: CGFloat = 12.0
  36. let textSize = text.getSize(with: fontSize)
  37. delLabel.text = text
  38. delLabel.font = delLabel.font.withSize(fontSize)
  39. delLabel.textColor = UIColor(hex: "#999999")
  40. let deleteContentWidth = textSize.width + 22 + 10 // 文字宽度 图片宽度22 间隔10
  41. let firstleft = (width - deleteContentWidth) / 2
  42. let secondLeft = firstleft + 22 + 10
  43. self.deleteView?.addSubview(delIcon)
  44. delIcon.snp.makeConstraints { (maker) in
  45. maker.left.equalTo(self.deleteView!.snp.left).offset(firstleft)
  46. maker.top.equalTo(self.deleteView!.snp.top)
  47. }
  48. self.deleteView?.addSubview(delLabel)
  49. delLabel.snp.makeConstraints { (maker) in
  50. maker.left.equalTo(self.deleteView!.snp.left).offset(secondLeft)
  51. maker.centerY.equalTo(delIcon.snp.centerY)
  52. // maker.top.equalTo(self.deleteView!.snp.top)
  53. }
  54. // 点击
  55. self.deleteView?.addTapGesture(action: { (tap) in
  56. DDLogDebug("tap gesture")
  57. self.clickDeleteAllHistory()
  58. })
  59. self.historyList = O2UserDefaults.shared.searchHistory
  60. self.tagsListView?.reloadData()
  61. }
  62. required init?(coder: NSCoder) {
  63. fatalError("init(coder:) has not been implemented")
  64. }
  65. @objc private func clickDeleteAllHistory() {
  66. DDLogDebug("点击了全部删除")
  67. self.historyList = []
  68. O2UserDefaults.shared.searchHistory = []
  69. self.tagsListView?.reloadData()
  70. }
  71. func addSearchHistory(key: String) {
  72. let history = O2UserDefaults.shared.searchHistory
  73. var new: [String] = []
  74. if !history.contains(key) {
  75. new.append(key)
  76. }
  77. history.forEach { (s) in
  78. new.append(s)
  79. }
  80. O2UserDefaults.shared.searchHistory = new
  81. self.historyList = new
  82. self.tagsListView?.reloadData()
  83. }
  84. }
  85. extension O2SearchHistoryView: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
  86. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  87. return self.historyList.count
  88. }
  89. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  90. let size = self.historyList[indexPath.row].getSize(with: 16.0)
  91. return CGSize(width: size.width + 20, height: 28)
  92. }
  93. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  94. if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "O2SearchHistoryCell", for: indexPath) as? O2SearchHistoryCell {
  95. cell.setTitle(title: self.historyList[indexPath.row])
  96. return cell
  97. }
  98. return UICollectionViewCell()
  99. }
  100. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  101. let tag = self.historyList[indexPath.row]
  102. DDLogDebug("点击了 \(tag)")
  103. if self.delegate != nil {
  104. self.delegate?.clickToSearchTag(tag: tag)
  105. }
  106. }
  107. }
  108. class O2SearchHistoryLayout: UICollectionViewFlowLayout {
  109. override init() {
  110. super.init()
  111. itemSize = CGSize(width: 100, height: 30)
  112. scrollDirection = .vertical
  113. minimumLineSpacing = 8
  114. minimumInteritemSpacing = 8
  115. sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  116. }
  117. required init?(coder aDecoder: NSCoder) {
  118. fatalError("init(coder:) has not been implemented")
  119. }
  120. }