Commit c25810c3 authored by Steven杜宇's avatar Steven杜宇

// 删除历史搜索记录

parent 9762ec05
......@@ -129,6 +129,17 @@ class YHSearchInfomationVC: YHBaseViewController {
searchText()
self.saveSearchHistory(text)
}
view.deleteBlock = {
[weak self] in
guard let self = self else { return }
self.view.endEditing(true)
let arr = self.getSearchHistoryList()
if arr.count <= 0 {
YHHUD.flash(message: "暂无搜索记录~")
return
}
self.showDeleteHistoryAlertView()
}
return view
}()
......@@ -179,6 +190,15 @@ class YHSearchInfomationVC: YHBaseViewController {
}
}
func showDeleteHistoryAlertView() {
YHCommonAlertView.show("", "确认删除全部历史搜索记录?", "取消", "确认") {
} callBack: {
self.deleteAllSearchHistory()
self.searchHistoryView.updateDataSource([])
}
}
func searchText() {
self.tableView.scrollToTop()
YHHUD.show(.progress(message: "搜索中..."))
......@@ -272,6 +292,11 @@ class YHSearchInfomationVC: YHBaseViewController {
return []
}
func deleteAllSearchHistory() {
UserDefaults.standard.set([], forKey: searchInfoHistoryKey)
UserDefaults.standard.synchronize()
}
func saveSearchHistory(_ text: String) {
if isEmptyString(text) { return }
......
......@@ -14,19 +14,27 @@ class YHSearchInfoHistoryView: UIView {
// var historyItems:[String] = ["几天2345", "阿", "萨法", "阿德"]
var selectBlock:((String)->())?
var deleteBlock:(()->())?
let interItemSpacing: CGFloat = 12.0 // 相邻单元格之间的水平间距
let gap: CGFloat = 12.0
let cellHeight: CGFloat = 30.0 // 单元格的固定高度
lazy var titleLabel: UILabel = {
var label = UILabel()
let label = UILabel()
label.font = .PFSC_R(ofSize: 14)
label.textColor = UIColor(hex: 0x94A3B8)
label.text = "历史搜索"
return label
}()
lazy var deleteAllBtn: UIButton = {
let btn = UIButton()
btn.setImage(UIImage(named: "home_history_delete"), for: .normal)
btn.addTarget(self, action: #selector(didDeleteAllBtnClicked), for: .touchUpInside)
btn.YH_clickEdgeInsets = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
return btn
}()
lazy var collectionView: UICollectionView = {
// 创建集合视图布局
let layout = YHSearchItemLayout()
......@@ -54,6 +62,7 @@ class YHSearchInfoHistoryView: UIView {
func setupUI() {
self.addSubview(titleLabel)
self.addSubview(deleteAllBtn)
self.addSubview(collectionView)
titleLabel.snp.makeConstraints { make in
......@@ -63,6 +72,12 @@ class YHSearchInfoHistoryView: UIView {
make.height.equalTo(20.0)
}
deleteAllBtn.snp.makeConstraints { make in
make.width.height.equalTo(16)
make.right.equalTo(-20)
make.centerY.equalTo(titleLabel)
}
collectionView.snp.makeConstraints { make in
make.top.equalTo(titleLabel.snp.bottom).offset(15.0)
make.left.right.equalTo(titleLabel)
......@@ -75,6 +90,11 @@ class YHSearchInfoHistoryView: UIView {
historyItems.append(contentsOf: arr)
self.collectionView.reloadData()
titleLabel.isHidden = arr.count <= 0
deleteAllBtn.isHidden = arr.count <= 0
}
@objc func didDeleteAllBtnClicked() {
deleteBlock?()
}
// 计算文字宽度大小
......
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "delete@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "delete@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment