//
|
// 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
|
}
|
}
|