CNLivePlayletHomePageHeaderCell.swift 14.5 KB
//
//  CNLivePlayletHomePageHeaderCell.swift
//  metaCode
//
//  Created by zx on 2024/1/4.
//  短剧主页-头视图Cell

import Foundation
import QMUIKit
import SwiftUI
import SnapKit
import Kingfisher

//cell高度变化
typealias homePageHeaderCellChangedBlockActionBlock = (_ cell: CNLivePlayletHomePageHeaderCell ,_ jianjieHeight:CGFloat) -> Void
class CNLivePlayletHomePageHeaderCell:UITableViewCell{
    var homePageHeaderCellChangedBlock:homePageHeaderCellChangedBlockActionBlock?
    
    var _albumListModel : CNLiveAlbumListModel?
    var albumListModel : CNLiveAlbumListModel? {
        get{
            _albumListModel
        }
        set{
            _albumListModel = newValue!
            
            self.setupNewUI(model: _albumListModel!)
        }
    }
    
    //MARK: - 懒加载
    lazy var iconImgView :UIImageView = {
        let iconImgView = UIImageView(frame: CGRect(x: 20, y: 20, width: 112, height: 147))
        iconImgView.layer.cornerRadius = 5
        iconImgView.clipsToBounds = true
        return iconImgView
    }()
    lazy var payIconImgView :UIImageView = {
        let payIconImgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 14))
        payIconImgView.image = UIImage(named: "homePage_payment")
        return payIconImgView
    }()
    
    ///标题
    lazy var titleLabl: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.iconImgView.right+15, self.iconImgView.top, KSreenWidth-(self.iconImgView.right+15)-10, 21))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = BoldFont_15
        titleLabel.text = "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"
        let height = titleLabel.qmui_lineHeight
        titleLabel.height = height
        return titleLabel
    }()
    ///集数
    lazy var jishuLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.titleLabl.left, self.titleLabl.bottom+20, KSreenWidth-(self.iconImgView.right+15)-10, 21))
        titleLabel.textColor = HexColor("#999999")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_12
        titleLabel.text = "45集"
        let height = titleLabel.qmui_lineHeight
        titleLabel.height = height
        return titleLabel
    }()
    ///简介
    lazy var jianjieLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.iconImgView.left, self.iconImgView.bottom+10, KSreenWidth-2*self.iconImgView.left, 21))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_12
        titleLabel.backgroundColor = UIColor(red:0.98, green:0.98, blue:0.98, alpha:1.00)
        titleLabel.text = "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"
        titleLabel.numberOfLines = 0
        var lines = titleLabel.getRealLabelTextLines()
        let height = titleLabel.qmui_lineHeight
        if lines > 2 {
            lines = 2
        }
        titleLabel.height = height*CGFloat(lines)
        return titleLabel
    }()
    //展开,收起
    lazy var expandBtn: UIButton = {
        let expandBtn = UIButton.init(type: .custom)
        expandBtn.backgroundColor = .white
        
        expandBtn.frame = CGRect(x: self.jianjieLabel.right-60, y: self.jianjieLabel.bottom, width: 60, height: 30)
        expandBtn.setTitle("展开", for: .normal)
        expandBtn.setTitle("收起", for: .selected)

        expandBtn.setTitleColor(HexColor("#666666"), for: .normal)
        expandBtn.setImage(UIImage(named: "homepage_unfold"), for: .normal)
        expandBtn.setImage(UIImage(named: "homepage_fold"), for: .selected)
        expandBtn.titleLabel?.font = Font_12
        expandBtn.setupButtonImageAndTitlePossitionWith(padding: 5, style: .right)
        expandBtn.addTarget(self, action: #selector(expandBtnAction), for: .touchUpInside)
        return expandBtn
    }()
    //追剧|已追剧
    lazy var followBtn: UIButton = {
        let  followBtn = UIButton(type: .custom)
        followBtn.setTitle("追剧", for: .normal)
        followBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
        followBtn.titleLabel?.font = Font_15
        followBtn.layer.cornerRadius = 20
        followBtn.clipsToBounds = true
        followBtn.addTarget(self, action: #selector(followBtnAction), for: .touchUpInside)
        
        let gradient = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.colors = [HexColor("#FFD20B")!.cgColor, HexColor("#FFA81A")!.cgColor]
        gradient.locations = [0, 1]
        gradient.startPoint = CGPoint(x: 0.96, y: 0.42)
        gradient.endPoint = CGPoint(x: 0.42, y: 0.42)
        gradient.name = "followBtnSubLayer"
        followBtn.layer.insertSublayer(gradient, at: 0)
//        #FDF4E4
        return followBtn
    }()
    //付费解锁全集|已购买
    lazy var paymentBtn: UIButton = {
        let  paymentBtn = UIButton(type: .custom)
        paymentBtn.setTitle("付费解锁全集", for: .normal)
        paymentBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
        paymentBtn.setTitle("已购买", for: .selected)
        paymentBtn.setTitleColor(HexColor("#619CFF"), for: .selected)
        paymentBtn.titleLabel?.font = Font_15
        paymentBtn.layer.cornerRadius = 20
        paymentBtn.clipsToBounds = true
        paymentBtn.addTarget(self, action: #selector(paymentBtnAction), for: .touchUpInside)
        
        let gradient = CAGradientLayer()
        gradient.frame = self.bounds
        gradient.colors = [HexColor("#67C5FF")!.cgColor, HexColor("#609AFF")!.cgColor]
        gradient.locations = [0, 1]
        gradient.startPoint = CGPoint(x: 0, y: 0.5)
        gradient.endPoint = CGPoint(x: 0.5, y: 0.5)
        paymentBtn.layer.insertSublayer(gradient, at: 0)
        
//        #ECF3FF
        return paymentBtn
    }()
    ///全部剧集
    lazy var allJuJiLabl: UILabel = {
        let allJuJiLabl = UILabel.init(frame: CGRectMake(self.iconImgView.right+15, self.followBtn.bottom+20, 100, 16))
        allJuJiLabl.textColor = HexColor("#333333")
        allJuJiLabl.textAlignment = .left
        allJuJiLabl.font = BoldFont_16
        allJuJiLabl.text = "全部剧集"
        return allJuJiLabl
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.setUI()
    }
    
    func setUI(){
        self.contentView.addSubview(self.iconImgView)
        self.iconImgView.addSubview(self.payIconImgView)
        self.contentView.addSubview(self.titleLabl)
        self.contentView.addSubview(self.jishuLabel)
        self.contentView.addSubview(self.jianjieLabel)
        self.contentView.addSubview(self.expandBtn)
        self.contentView.addSubview(self.followBtn)
        self.contentView.addSubview(self.paymentBtn)
        self.contentView.addSubview(self.allJuJiLabl)
        
        let lines = jianjieLabel.getRealLabelTextLines()
        if lines <= 2{
            self.expandBtn.isHidden = true
        }else{
            self.expandBtn.isHidden = false
        }
        
        self.followBtn.snp.makeConstraints { make in
            make.top.equalTo(self.jianjieLabel.snp.bottom).offset(25)
            make.left.equalTo(self.iconImgView.snp.left)
            make.width.equalTo(140)
            make.height.equalTo(40)
        }
        self.paymentBtn.snp.makeConstraints { make in
            make.top.equalTo(self.followBtn)
            make.left.equalTo(self.followBtn.snp.right).offset(15)
            make.right.equalTo(self.contentView.snp.right).offset(-15)
            make.height.equalTo(40)
        }
        self.allJuJiLabl.snp.makeConstraints { make in
            make.top.equalTo(self.followBtn.snp.bottom).offset(20)
            make.left.equalTo(self.iconImgView.snp.left)
            make.width.equalTo(100)
            make.height.equalTo(16)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupNewUI(model:CNLiveAlbumListModel){
        
        self.titleLabl.text = model.albumMsgModel?.albumName ?? "专辑名称"
        self.jishuLabel.text = String(format: "共%@集",model.albumMsgModel?.episodeNow ?? "1")
        self.iconImgView.kf.setImage(with: URL(string: model.albumMsgModel?.albumImg ?? ""))
        if model.product?.albumProductType == "0"{
            //免费
            self.payIconImgView.isHidden = true
        }else if model.product?.albumProductType == "1"{
            //积分
            self.payIconImgView.isHidden = false
        }else if model.product?.albumProductType == "2"{
            //付费
            self.payIconImgView.isHidden = false
        }else{
            self.payIconImgView.isHidden = false
        }
        self.jianjieLabel.text = (model.albumMsgModel?.desc != "") ? model.albumMsgModel?.desc : "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈"//"短剧简介"
        var lines = jianjieLabel.getRealLabelTextLines()
        let height = jianjieLabel.qmui_lineHeight
        if lines <= 2{
            self.expandBtn.isHidden = true
        }else{
            self.expandBtn.isHidden = false
        }
        
        if self.expandBtn.isSelected{
            //展开状态
            self.jianjieLabel.height = height*CGFloat(lines)

        }else{
            //收起状态
            self.jianjieLabel.height = 2*height
        }
        if model.albumMsgModel!.collection{
            //已追剧
            self.followBtn.setTitleColor(HexColor("#F5A623"), for: .normal)
            self.followBtn.setTitle("已追剧", for: .normal)
            self.followBtn.isUserInteractionEnabled = false
            let follLayer =  self.followBtn.layer.qmui_layer(withName: "followBtnSubLayer")
            follLayer?.removeFromSuperlayer()
            self.followBtn.backgroundColor = HexColor("#FDF4E4")
        }else{
            //未追剧
            self.followBtn.setTitle("追剧", for: .normal)
            self.followBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
            self.followBtn.isUserInteractionEnabled = true
            
            let follLayer =  self.followBtn.layer.qmui_layer(withName: "followBtnSubLayer")
            if (follLayer != nil){
                //已添加layer
            }else{
                //未添加layer
                let gradient = CAGradientLayer()
                gradient.frame = self.bounds
                gradient.colors = [HexColor("#FFD20B")!.cgColor, HexColor("#FFA81A")!.cgColor]
                gradient.locations = [0, 1]
                gradient.startPoint = CGPoint(x: 0.96, y: 0.42)
                gradient.endPoint = CGPoint(x: 0.42, y: 0.42)
                gradient.name = "followBtnSubLayer"
                followBtn.layer.insertSublayer(gradient, at: 0)
                self.followBtn.backgroundColor = .clear
            }
            
        }
    }
    
    // MARK: - Action
    @objc func expandBtnAction(button:UIButton){
        button.isSelected = !button.isSelected
        let lines = jianjieLabel.getRealLabelTextLines()
        let height = jianjieLabel.qmui_lineHeight

        if button.isSelected{
            //文本收起状态,点击展开
            self.jianjieLabel.height = CGFloat(lines)*height
        }else{
            //文本展开状态,点击收起
            self.jianjieLabel.height = 2*height
        }
        self.homePageHeaderCellChangedBlock!(self, self.jianjieLabel.height)
        self.expandBtn.top = self.jianjieLabel.bottom

    }
    @objc func followBtnAction(button:UIButton){

        //网络请求追剧
        CNLiveHomePageViewModel.postAlbumCollect(aid: self.albumListModel?.albumMsgModel?.albumId ?? "", type: 1) { result, status in
            if status == .success{
                self.followBtn.setTitleColor(HexColor("#F5A623"), for: .normal)
                self.followBtn.setTitle("已追剧", for: .normal)
                self.followBtn.isUserInteractionEnabled = false
                let follLayer =  self.followBtn.layer.qmui_layer(withName: "followBtnSubLayer")
                follLayer?.removeFromSuperlayer()
                self.followBtn.backgroundColor = HexColor("#FDF4E4")
            }else{
                
            }
        }
    }
    @objc func paymentBtnAction(button:UIButton){
        button.isSelected = !button.isSelected

    }
    
}