CNLiveVodIntroductionCell.swift 7.98 KB
//
//  CNLiveVodIntroductionCell.swift
//  metaCode
//
//  Created by zx on 2025/3/3.
//  简介,点赞等

import Foundation

// type 1详情按钮 2点赞按钮 3收藏按钮 4分享按钮
typealias CNLiveVodIntroductionCellUpdateBlock = (_ isExpandDetail:Bool,_ isFavorite:Bool,_ isCollection:Bool,_ type:String,_ model:CNLiveVodPlayBlockListListModel)->Void
class CNLiveVodIntroductionCell:UITableViewCell{
    
    var updateCellBlock:CNLiveVodIntroductionCellUpdateBlock?
    
    var _vodPlayBlockListModel : CNLiveVodPlayBlockListModel?
    var vodPlayBlockListModel : CNLiveVodPlayBlockListModel? {
        get{
            _vodPlayBlockListModel
        }
        set{
            _vodPlayBlockListModel = newValue!
            self.setupNewUI(model: vodPlayBlockListModel!)
        }
    }
    
    //MARK: - 懒加载
    lazy var titleLabel: UILabel = {
        let titleLbl = UILabel()
        titleLbl.font = BoldFont_15
        titleLbl.textColor = HexColor("#000102")
        titleLbl.text = "标题"
        return titleLbl
    }()
    //详情
    lazy var detailBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 40)
        expandBtn.setTitle("详情", for: .normal)
        expandBtn.setTitle("详情", for: .selected)
        expandBtn.setTitleColor(HexColor("#999999"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_expand"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_unexpand"), for: .selected)
        expandBtn.titleLabel?.font = Font_10
        expandBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .right)
        expandBtn.addTarget(self, action: #selector(detailBtnAction), for: .touchUpInside)
        return expandBtn
    }()


    //点赞
    lazy var likeBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: 0, y: 0, width: (KSreenWidth-40)/3.0, height: 40)
        expandBtn.setTitle("0", for: .normal)
        expandBtn.setTitle("0", for: .selected)
        expandBtn.setTitleColor(HexColor("#333333"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_like"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_liked"), for: .selected)
        expandBtn.titleLabel?.font = Font_10
        expandBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .left)
        expandBtn.addTarget(self, action: #selector(likeBtnAction), for: .touchUpInside)
        expandBtn.backgroundColor = HexColor("#F5F5F5")
        expandBtn.clipsToBounds = true
        expandBtn.layer.cornerRadius = 20
        return expandBtn
    }()
    //评论
    lazy var collectionBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: 0, y: 0, width: (KSreenWidth-40)/3.0, height: 40)
        expandBtn.setTitle("收藏", for: .normal)
        expandBtn.setTitle("已收藏", for: .selected)
        expandBtn.setTitleColor(HexColor("#333333"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_uncollection"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_collection"), for: .selected)
        expandBtn.titleLabel?.font = Font_10
        expandBtn.backgroundColor = HexColor("#F5F5F5")
        expandBtn.clipsToBounds = true
        expandBtn.layer.cornerRadius = 20
        expandBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .left)
        expandBtn.addTarget(self, action: #selector(collectionBtnAction), for: .touchUpInside)
        return expandBtn
    }()
    //分享
    lazy var shareBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .clear
        expandBtn.frame = CGRect(x: 0, y: 0, width: (KSreenWidth-40)/3.0, height: 40)
        expandBtn.setTitle("分享", for: .normal)
        expandBtn.setTitle("分享", for: .selected)
        expandBtn.setTitleColor(HexColor("#333333"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_share"), for: .normal)
        expandBtn.setImage(UIImage(named: "home_player_share"), for: .selected)
        expandBtn.titleLabel?.font = Font_10
        expandBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .left)
        expandBtn.backgroundColor = HexColor("#F5F5F5")
        expandBtn.clipsToBounds = true
        expandBtn.layer.cornerRadius = 20
        expandBtn.addTarget(self, action: #selector(shareBtnAction), for: .touchUpInside)
        return expandBtn
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
    private func setupUI() {
        contentView.backgroundColor = .white
        self.contentView.addSubview(self.titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(15)
            make.width.lessThanOrEqualTo(KSreenWidth-10-80)
        }
        
        self.contentView.addSubview(self.detailBtn)
        detailBtn.snp.makeConstraints { make in
            make.left.equalTo(self.titleLabel.snp.right).offset(10)
            make.centerY.equalTo(self.titleLabel.snp.centerY)
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        self.contentView.addSubview(self.likeBtn)
        likeBtn.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.top.equalTo(self.titleLabel.snp.bottom).offset(19)
            make.width.equalTo((KSreenWidth-40)/3.0)
            make.height.equalTo(40)
        }

        self.contentView.addSubview(self.collectionBtn)
        collectionBtn.snp.makeConstraints { make in
            make.left.equalTo(self.likeBtn.snp.right).offset(10)
            make.centerY.equalTo(self.likeBtn.snp.centerY)
            make.width.equalTo((KSreenWidth-40)/3.0)
            make.height.equalTo(40)
        }
        self.contentView.addSubview(self.shareBtn)
        shareBtn.snp.makeConstraints { make in
            make.left.equalTo(self.collectionBtn.snp.right).offset(10)
            make.centerY.equalTo(self.likeBtn.snp.centerY)
            make.width.equalTo((KSreenWidth-40)/3.0)
            make.height.equalTo(40)
        }

        self.contentView.clipsToBounds = true
    }
    
    func setupNewUI(model:CNLiveVodPlayBlockListModel){
        if model.list.count > 0{
            if let vodMdl = model.list.first{
                titleLabel.text = vodMdl.title
                //处理转换成万
                likeBtn.setTitle("\(vodMdl.likesNum)", for: .normal)
                likeBtn.setTitle("\(vodMdl.likesNum)", for: .selected)
                likeBtn.isSelected = vodMdl.liked

                collectionBtn.isSelected = vodMdl.collectioned
            }
        }
    }

    @objc func detailBtnAction(){
        self.updateCellBlock?(true, false, false, "1",self.vodPlayBlockListModel!.list.first!)
    }

    @objc func likeBtnAction(){
        likeBtn.isSelected = likeBtn.isSelected ? false : true
        let vodMdl = self.vodPlayBlockListModel!.list.first!
        let num = likeBtn.isSelected ? 1 : -1
        likeBtn.setTitle("\(vodMdl.likesNum+num)", for: .normal)
        likeBtn.setTitle("\(vodMdl.likesNum+num)", for: .selected)

        self.updateCellBlock?(false, likeBtn.isSelected, false, "2",self.vodPlayBlockListModel!.list.first!)
    }
    @objc func collectionBtnAction(){
        collectionBtn.isSelected = collectionBtn.isSelected ? false : true
        self.updateCellBlock?(false, false, true, "3",self.vodPlayBlockListModel!.list.first!)
    }
    @objc func shareBtnAction(){
        self.updateCellBlock?(false, false, false, "4",self.vodPlayBlockListModel!.list.first!)
    }

    
}