CNLiveHomeSearchHistoryCell.swift 5.33 KB
//
//  CNLiveHomeSearchHistoryCell.swift
//  metaCode
//
//  Created by zx on 2025/3/8.
//

import Foundation
import QMUIKit

typealias CNLiveHomeSearchHistoryClickBlock = (_ selectText:String)->Void;
typealias CNLiveHomeSearchHistoryHeightUpdateBlock = (_ height:CGFloat)->Void;


class CNLiveHomeSearchHistoryCell: UITableViewCell {

    var searchHistoryClickBlock:CNLiveHomeSearchHistoryClickBlock?
    var newHeight:CNLiveHomeSearchHistoryHeightUpdateBlock?
    //_historyModel
    var _historyModel : CNLiveHomeSearchHistoryModel?
    var historyModel : CNLiveHomeSearchHistoryModel? {
        get{
            _historyModel
        }
        set{
            _historyModel = newValue!
            for obj in self.historyItemView.subviews{
                obj.removeFromSuperview()
            }

            self.setupNewUI(model: _historyModel!)
        }
    }
    
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = BoldFont_16
        titleLbl.textColor = UIColor(red:0.62, green:0.62, blue:0.62, alpha:1.00)
        titleLbl.text = "搜索历史"
        return titleLbl
    }()
    lazy var historyItemView : CNLiveSearchHistoryFloatLayoutView = {
        let historyItemView = CNLiveSearchHistoryFloatLayoutView()
        historyItemView.padding = UIEdgeInsets(top: 0, left: 0, bottom: 9, right: 0)
        historyItemView.itemMargins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 10)
        historyItemView.minimumItemSize = CGSize(width: 43, height: 29)
        historyItemView.zxMaxShowLinesCount = 10
        return historyItemView
    }()

    lazy var noHisLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = Font_14
        titleLbl.textColor = UIColor(red:0.62, green:0.62, blue:0.62, alpha:1.00)
        titleLbl.text = "暂无搜索历史"
        return titleLbl
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        contentView.backgroundColor = .white
        contentView.clipsToBounds = true
        
        self.contentView.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(20)
        }
        
        self.contentView.addSubview(self.historyItemView)
        let contentWidth = KSreenWidth-30
        let floatLayoutViewSize = self.historyItemView.sizeThatFits(CGSize(width: contentWidth, height: CGFLOAT_MAX))
        self.historyItemView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.width.equalTo(KSreenWidth-30)//make.right.equalToSuperview().offset(-15)
            make.top.equalTo(self.titleLabel.snp.bottom)//.offset(20)
            make.height.equalTo(floatLayoutViewSize.height)
        }
        
        self.contentView.addSubview(self.noHisLabel)
        self.noHisLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(20)
        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if self.historyModel?.history.count ?? 0 > 0{
            self.newHeight?(self.historyItemView.bottom)
        }else{
            self.newHeight?(176)
        }
        print(self.historyItemView)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupNewUI(model:CNLiveHomeSearchHistoryModel){
        //倒序
        let reversedArray = Array(model.history.reversed())
        if reversedArray.count > 0{
            var hisArr = [String]()
            if reversedArray.count > 10{
                hisArr = Array(reversedArray.prefix(10))
            }else{
                hisArr = reversedArray
            }
            for hisStr in hisArr {
                let closeBtn = QMUIGhostButton(type: .custom)
                closeBtn.ghostColor = HexColor("#999999")
                closeBtn.qmui_borderColor = .clear
                closeBtn.layer.borderWidth = 0
                closeBtn.layer.borderColor = UIColor.clear.cgColor
                closeBtn.setTitle(hisStr, for: .normal)
                closeBtn.backgroundColor = HexColor("#FAFAFA")
                closeBtn.titleLabel?.font = Font_14
                closeBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 10, bottom: 4, right: 10)
                closeBtn.addTarget(self, action: #selector(hisBtnAction), for: .touchUpInside)
                self.historyItemView.addSubview(closeBtn)
            }
            let contentWidth = KSreenWidth-30
            let floatLayoutViewSize = self.historyItemView.sizeThatFits(CGSize(width: contentWidth, height: CGFLOAT_MAX))
            self.historyItemView.snp.remakeConstraints { make in
                make.left.equalTo(15)
                make.width.equalTo(KSreenWidth-30)
                make.top.equalTo(self.titleLabel.snp.bottom).offset(20)
                make.height.equalTo(floatLayoutViewSize.height)
            }
            self.noHisLabel.isHidden = true
        }else{
            //没有历史
            self.noHisLabel.isHidden = false
        }
    }
    
    @objc func hisBtnAction(button:QMUIGhostButton){
        //点击历史按钮
        if let text = button.titleLabel?.text{
            self.searchHistoryClickBlock?(text)
        }
    }
}