杨锴
2024-08-28 611f271e03e9ff2b5c32a9bbb2e3eb719c178df5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
//  SearchVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/13.
//
 
import UIKit
import JQTools
import RxSwift
import RxDataSources
 
class SearchCache{
 
                static var array = NSMutableArray()
                static let cacheSearchPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("search")
 
                static func readList()->NSMutableArray{
                                if !FileManager.default.fileExists(atPath: cacheSearchPath.absoluteString){
                                                try? FileManager.default.createDirectory(at: cacheSearchPath, withIntermediateDirectories: true)
                                }
 
                                let searchPlistPath = cacheSearchPath.appendingPathComponent("search.plist")
                                if let tempArray = NSMutableArray(contentsOfFile: searchPlistPath.droppedScheme()!.absoluteString){
                                                array = tempArray
                                }
                                return array
                }
 
                static func writeToList(_ content:String){
                                if array.contains(content){
                                                var exchangeIndex:Int?
                                                for (index,c) in array.enumerated(){
                                                                if (c as! String) == content{
                                                                                exchangeIndex = index;break
                                                                }
                                                }
                                                if exchangeIndex != nil{
                                                                array.exchangeObject(at: 0, withObjectAt: exchangeIndex!)
                                                }
                                }else{
                                                array.add(content)
                                }
                }
 
                static func writeToPath(){
                                let searchPlistPath = cacheSearchPath.appendingPathComponent("search.plist")
                                array.write(to: searchPlistPath, atomically: true)
                }
 
                static func deleteAtIndex(_ index:Int){
                                array.removeObject(at: index)
                }
}
 
class SearchVC: BaseVC {
                @IBOutlet weak var cacheSearchCollectionView: UICollectionView!
                @IBOutlet weak var cacheSearchHei: NSLayoutConstraint!
                @IBOutlet weak var tf_search: UITextField!
                @IBOutlet weak var tableView: UITableView!
                
                override func viewDidLoad() {
                                super.viewDidLoad()
                                title = "心泉·疗愈"
 
                                _ = SearchCache.readList()
                                cacheSearchCollectionView.reloadData()
                }
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                SearchCache.writeToPath()
                }
 
                override func setUI() {
                                super.setUI()
                                view.backgroundColor = UIColor(hexString: "f6f6f6")
                                tf_search.delegate = self
                                tf_search.returnKeyType = .search
 
                                let flowLayout = EqualCellSpaceFlowLayout(.left, 11.5)
                                cacheSearchCollectionView.delegate = self
                                cacheSearchCollectionView.dataSource = self
                                cacheSearchCollectionView.isScrollEnabled = false
                                cacheSearchCollectionView.collectionViewLayout = flowLayout
                                cacheSearchCollectionView.contentInset = UIEdgeInsets(top: 0, left: 20.5, bottom: 0, right: 20.5)
                                cacheSearchCollectionView.backgroundColor = .clear
                                cacheSearchCollectionView.register(UINib(nibName: "SearchHistoryCCell", bundle: nil), forCellWithReuseIdentifier: "_SearchHistoryCCell")
 
                                tableView.delegate = self
                                tableView.dataSource = self
                                tableView.separatorStyle = .none
                                tableView.backgroundColor = .clear
                                tableView.register(UINib(nibName: "SearchHotTCell", bundle: nil), forCellReuseIdentifier: "_SearchHotTCell")
 
                }
 
                override func setRx() {
                                cacheSearchCollectionView.rx.observe(CGSize.self, "contentSize").map { (size) -> CGFloat? in
                                                if let size = size{
                                                                return size.height
                                                }
                                                return nil
                                }.subscribe(onNext: { [unowned self](height) in
                                                if let height = height{
                                                                self.cacheSearchHei.constant = height
                                                }
                                }).disposed(by: disposeBag)
                }
 
                @objc func closeAction(_ btn:UIButton){
                                CommonAlertView.show(title: "提示", content: "是否删除?") {[unowned self] state in
                                                if state{
                                                                SearchCache.deleteAtIndex(btn.tag - 100)
                                                                self.cacheSearchCollectionView.reloadData()
                                                }
                                }
                }
 
                @IBAction func searchAction(_ sender: UIButton) {
                                searchDone()
                }
 
                private func searchDone(){
                                if tf_search.text!.isEmpty{
                                                alert(msg: "请输入冥想搜索内容");return
                                }
 
                                tf_search.resignFirstResponder()
                                SearchCache.writeToList(tf_search.text!)
                }
}
 
extension SearchVC:UITableViewDelegate & UITableViewDataSource{
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                                let cell = tableView.dequeueReusableCell(withIdentifier: "_SearchHotTCell", for: indexPath) as! SearchHotTCell
                                cell.selectionStyle = .none
                                cell.backgroundColor = .clear
                                return cell
                }
 
                func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                                return 10
                }
 
                func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                                return 41
                }
}
 
extension SearchVC:UICollectionViewDelegate & UICollectionViewDataSource{
 
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                                searchDone()
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return SearchCache.array.count
 
                }
 
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_SearchHistoryCCell", for: indexPath) as! SearchHistoryCCell
                                cell.label_content.text = (SearchCache.array[indexPath.row] as! String)
                                cell.btn_close.addTarget(self, action: #selector(closeAction(_:)), for: .touchUpInside)
                                cell.btn_close.tag = 100+indexPath.row
                                return cell
                }
}
 
extension SearchVC:UICollectionViewDelegateFlowLayout{
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
                                return 11.5
                }
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
                                return 11.5
                }
 
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                                let str = SearchCache.array[indexPath.row] as! String
                                let w = String.jq_getWidth(text: str, height: 11, font: 12)
                                return CGSize(width: w + 25, height: 28.1)
                }
}
 
extension SearchVC:UITextFieldDelegate{
                func textFieldShouldReturn(_ textField: UITextField) -> Bool {
                                searchDone()
                                return true
                }
}