CNDownSlideSingleCourseCell.swift 16.1 KB
//
//  CNDownSlideSingleCourseCell.swift
//  metaCode
//
//  Created by zx on 2023/10/16.
//  单课程大图专辑 | 全部课程列表 | 我的课程列表


import Foundation
import UIKit

enum GlobalConstants {
    static let leftMargin: CGFloat = 20
    
    static let toDayCardRowH: CGFloat = 440
    static let toDayCardCornerRadius: CGFloat = 12.0
    static let todayCardSize: CGSize = CGSize(width: KSreenWidth - 2 * 15, height: DownSlideSingleCourseImgH)
    static let cardDetailTopImageH: CGFloat = 500
    
    static let iconBorderColor: CGColor = UIColor(red: 239/255.0, green: 240/255.0, blue: 241/255.0, alpha: 1).cgColor
    static let iconBorderWidth: CGFloat = 0.8
    static let iconCornerRadius: CGFloat = 17.5
    
    static let textBlueColor = UIColor(red: 0/255.0, green: 122/255.0, blue: 255/255.0, alpha: 1.0)   //007AFF
    static let speratorLineColor = UIColor(red: 224/255.0, green: 224/255.0, blue: 224/255.0, alpha: 1)

}

///全部课程 标题高度
let DownSlideAllCourseTitleH: CGFloat = 55
//全部课程 每row_topY间距
let DownSlideAllCourseRowTopYGap: CGFloat = 10
//全部课程 每row_topY间距
let DownSlideAllCourseRowBottomYGap: CGFloat = 20
//全部课程 每row_img宽高
let DownSlideAllCourseRowImageWH: CGFloat = 45

///全部课程 每row高度
let DownSlideAllCourseRowH: CGFloat = DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowBottomYGap + DownSlideAllCourseRowImageWH
///全部课程 宽度
let DownSlideAllCourseW: CGFloat = KSreenWidth - DownSlideRecommendLeftGap*2

///单节课程 大图高度
let DownSlideSingleCourseImgH: CGFloat = 345
///单节课程 更多课程高度
let DownSlideSingleCourseMoreH: CGFloat = 60
///单节课程 宽度
let DownSlideSingleCourseW: CGFloat = KSreenWidth - DownSlideRecommendLeftGap*2

class CNDownSlideSingleCourseCell: UITableViewCell {
    
    let bgBackView = UIView()
    let bgImageView = UIImageView()

    //slideModel
    var _slideModel : CNLiveDownSlideModel?
    var slideModel : CNLiveDownSlideModel? {
        get{
            _slideModel
        }
        set{
            _slideModel = newValue!
            self.setupNewUI(model: _slideModel!)
        }
    }
    
    //myCurriculum
    var _myCurriculum : CNLiveAlbumBaseModel?
    var myCurriculum : CNLiveAlbumBaseModel? {
        get{
            _myCurriculum
        }
        set{
            _myCurriculum = newValue!
            self.setupMyCurriculumUI(model: _myCurriculum!)
        }
    }
    
    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 = HexColor(kCNDownSlideBackgroundColorHex)
    }
    
    func setupNewUI(model:CNLiveDownSlideModel){
        contentView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        bgBackView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        bgImageView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        
        if model.type == SlideModelTypeAllCourse12 {
            //全部课程列表
            contentView.addSubview(bgBackView)

            bgBackView.frame = CGRect(x: DownSlideRecommendLeftGap, y: 0,width:DownSlideSingleCourseW , height:CGFloat(model.cellH)-DownSlideRecommendLeftGap)
            // 设置阴影效果
            bgBackView.layer.masksToBounds = false
            //定义view的阴影颜色
            bgBackView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
            //定义view的阴影透明度,注意:如果view没有设置背景色阴影也是不会显示的
            bgBackView.layer.shadowOpacity = 1
            //定义view的阴影宽度,模糊计算的半径
            bgBackView.layer.shadowRadius = 10.0
            //阴影偏移量
            bgBackView.layer.shadowOffset = CGSize(width: 0, height: 0)
            bgBackView.layer.cornerRadius = 12
            bgBackView.backgroundColor = .white
            
            var titleTop:CGFloat = DownSlideRecommendLeftGap
            if model.blockSubTitle.count ?? 0 > 0{
                let subtitleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: DownSlideRecommendLeftGap, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 17))
                subtitleLbl.text = model.blockSubTitle
                subtitleLbl.font = Font_12
                subtitleLbl.textColor = HexColor("#999999")
                bgBackView.addSubview(subtitleLbl)
                titleTop = DownSlideRecommendLeftGap+17+1
            }
            let titleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: titleTop, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
            titleLbl.text = model.blockName ?? "全部课程"
            titleLbl.font = BoldFont_21
            titleLbl.textColor = HexColor("#333333")
            bgBackView.addSubview(titleLbl)
            
            //总块数
            let contentsNum = model.contents.count
            //model行数
            let row = model.rowNum
            //总显示块数
            var totalNum = (contentsNum >= row) ? row : contentsNum
            if row == 0{//row为0 , contents全部显示
                totalNum = contentsNum
            }
            for i in 0..<totalNum{//i是第几行
                let contentMdl = model.contents[i]
                
                //小icon
                let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, titleLbl.bottom+DownSlideRecommendLeftGap+CGFloat(i)*(DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowImageWH+DownSlideAllCourseRowBottomYGap), CGFloat(DownSlideAllCourseRowImageWH), CGFloat(DownSlideAllCourseRowImageWH)))
                imageView.kf.setImage(with: URL(string:contentMdl.poster ?? ""))
                imageView.layer.cornerRadius = 12
                imageView.clipsToBounds = true
                imageView.contentMode = .scaleAspectFill
                bgBackView.addSubview(imageView)
                
                let titleLbl = UILabel(frame: CGRect(x: imageView.right+DownSlideRecommendLeftGap, y: imageView.top, width: DownSlideSingleCourseW-90-imageView.right, height: 21))
                titleLbl.text = contentMdl.title ?? ""
                titleLbl.font = Font_15
                titleLbl.textColor = HexColor("#333333")
                bgBackView.addSubview(titleLbl)
                
                let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y:titleLbl.bottom+3 , width: titleLbl.width, height: 17))
                subTitleLbl.text =  contentMdl.subTitle ?? ""
                subTitleLbl.font = Font_12
                subTitleLbl.textColor = HexColor("#999999")
                bgBackView.addSubview(subTitleLbl)
                
                let enterBtn = UIButton(type: .custom)
                enterBtn.frame = CGRect(x: bgBackView.right-25-65, y: 0, width: 65, height: 30)
                enterBtn.centerY = imageView.centerY
                enterBtn.setBackgroundImage(UIImage(named: "homepage_down_enter"), for: .normal)
                enterBtn.setTitle("进入", for: .normal)
                enterBtn.titleLabel?.font = BoldFont_13
                enterBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
                enterBtn.addTarget(self, action: #selector(enterBtnAction(_:)), for: .touchUpInside)
                enterBtn.tag = i
                bgBackView.addSubview(enterBtn)
            }
        }else if model.type == SlideModelTypeOneCourse75 {
            //单图课程
            contentView.addSubview(bgBackView)
            bgBackView.addSubview(bgImageView)
            
            bgBackView.frame = CGRect(x: DownSlideRecommendLeftGap, y: 0,width:DownSlideSingleCourseW , height:DownSlideSingleCourseMoreH+DownSlideSingleCourseImgH)
            
            // 设置阴影效果
            bgBackView.layer.masksToBounds = false
            //定义view的阴影颜色
            bgBackView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
            //定义view的阴影透明度,注意:如果view没有设置背景色阴影也是不会显示的
            bgBackView.layer.shadowOpacity = 1
            //定义view的阴影宽度,模糊计算的半径
            bgBackView.layer.shadowRadius = 10.0
            //阴影偏移量
            bgBackView.layer.shadowOffset = CGSize(width: 0, height: 0)
            bgBackView.layer.cornerRadius = 12
            bgBackView.backgroundColor = .white
            
            bgImageView.frame = CGRect(x: 0, y: 0,width:DownSlideSingleCourseW , height:DownSlideSingleCourseImgH)
            bgImageView.contentMode = .scaleAspectFill
            bgImageView.layer.cornerRadius = GlobalConstants.toDayCardCornerRadius
            bgImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
            bgImageView.layer.masksToBounds = true
            if model.background.count > 0{
                bgImageView.kf.setImage(with: URL(string: model.background))
            }else{
                bgImageView.kf.setImage(with: URL(string: model.contents[0].poster ?? ""))
            }
                        
            let titleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: DownSlideRecommendLeftGap, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
            titleLbl.text = model.contents[0].title ?? ""
            titleLbl.font = BoldFont_21
            titleLbl.textColor = HexColor("#FFFFFF")
//            bgImageView.addSubview(titleLbl)
            
            let subTitleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y:titleLbl.bottom , width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
            subTitleLbl.text = model.contents[0].subTitle ?? ""
            subTitleLbl.font = Font_12
            subTitleLbl.textColor = HexColor("#FFFFFF")
//            bgImageView.addSubview(subTitleLbl)
            
            //单节底部视图
            let singleCourseBottomView = UIView(frame: CGRect(x: 0, y: DownSlideSingleCourseImgH, width: DownSlideSingleCourseW, height: DownSlideSingleCourseMoreH))
            singleCourseBottomView.layer.cornerRadius = 12
            singleCourseBottomView.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
            singleCourseBottomView.backgroundColor = .white
            singleCourseBottomView.isUserInteractionEnabled = true
            bgBackView.addSubview(singleCourseBottomView)
            
            let moreLabel = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: 15, width: DownSlideSingleCourseW - 50 - DownSlideRecommendLeftGap   , height: 30))
            moreLabel.text = model.blockName ?? "更多"
            moreLabel.font = BoldFont_15_Fit
            moreLabel.textColor = HexColor("#333333")
            singleCourseBottomView.addSubview(moreLabel)
            
            let moreImgView = UIImageView(image: UIImage(named: "homepage_down_more"))
            moreImgView.left = moreLabel.right
            moreImgView.centerY = moreLabel.centerY
            singleCourseBottomView.addSubview(moreImgView)
        }
    }
    
    func setupMyCurriculumUI(model:CNLiveAlbumBaseModel){
        contentView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        bgBackView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        bgImageView.subviews.forEach { view in
            view.removeFromSuperview()
        }
        //全部课程列表
        contentView.addSubview(bgBackView)

        let contentsNum = model.list.count
        //总显示块数/循环数
        var totalNum = (contentsNum >= 4) ? 4 : contentsNum
        var cellH = DownSlideAllCourseTitleH + DownSlideAllCourseRowH * CGFloat(totalNum)+DownSlideRecommendLeftGap
        
        bgBackView.frame = CGRect(x: DownSlideRecommendLeftGap, y: 0,width:DownSlideSingleCourseW , height:cellH-DownSlideRecommendLeftGap)
        // 设置阴影效果
        bgBackView.layer.masksToBounds = false
        //定义view的阴影颜色
        bgBackView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
        //定义view的阴影透明度,注意:如果view没有设置背景色阴影也是不会显示的
        bgBackView.layer.shadowOpacity = 1
        //定义view的阴影宽度,模糊计算的半径
        bgBackView.layer.shadowRadius = 10.0
        //阴影偏移量
        bgBackView.layer.shadowOffset = CGSize(width: 0, height: 0)
        bgBackView.layer.cornerRadius = 12
        bgBackView.backgroundColor = .white
        
        var titleTop:CGFloat = DownSlideRecommendLeftGap
        let titleLbl = UILabel(frame: CGRect(x: DownSlideRecommendLeftGap, y: titleTop, width: DownSlideSingleCourseW-DownSlideRecommendLeftGap*2, height: 30))
        titleLbl.text = "我的课程"
        titleLbl.font = BoldFont_21
        titleLbl.textColor = HexColor("#333333")
        bgBackView.addSubview(titleLbl)
        
        for i in 0..<totalNum{//i是第几行
            let contentMdl = model.list[i]
            
            //小icon
            let imageView = UIImageView(frame: CGRectMake(DownSlideRecommendLeftGap, titleLbl.bottom+DownSlideRecommendLeftGap+CGFloat(i)*(DownSlideAllCourseRowTopYGap+DownSlideAllCourseRowImageWH+DownSlideAllCourseRowBottomYGap), CGFloat(DownSlideAllCourseRowImageWH), CGFloat(DownSlideAllCourseRowImageWH)))
            imageView.kf.setImage(with: URL(string:contentMdl.poster ))
            imageView.layer.cornerRadius = 12
            imageView.clipsToBounds = true
            imageView.contentMode = .scaleAspectFill
            bgBackView.addSubview(imageView)
            
            let titleLbl = UILabel(frame: CGRect(x: imageView.right+DownSlideRecommendLeftGap, y: imageView.top, width: DownSlideSingleCourseW-90-imageView.right, height: 21))
            titleLbl.text = contentMdl.title 
            titleLbl.font = Font_15
            titleLbl.textColor = HexColor("#333333")
            bgBackView.addSubview(titleLbl)
            
            let subTitleLbl = UILabel(frame: CGRect(x: titleLbl.left, y:titleLbl.bottom+3 , width: titleLbl.width, height: 17))
            subTitleLbl.text =  contentMdl.subTitle 
            subTitleLbl.font = Font_12
            subTitleLbl.textColor = HexColor("#999999")
            bgBackView.addSubview(subTitleLbl)
            
            let enterBtn = UIButton(type: .custom)
            enterBtn.frame = CGRect(x: bgBackView.right-25-65, y: 0, width: 65, height: 30)
            enterBtn.centerY = imageView.centerY
            enterBtn.setBackgroundImage(UIImage(named: "homepage_down_enter"), for: .normal)
            enterBtn.setTitle("进入", for: .normal)
            enterBtn.titleLabel?.font = BoldFont_13
            enterBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
            enterBtn.addTarget(self, action: #selector(enterBtnAction(_:)), for: .touchUpInside)
            enterBtn.tag = i
            bgBackView.addSubview(enterBtn)
        }
    }
    //MARK: - 事件
    @objc func enterBtnAction(_ sender: UIButton){
        let tag = sender.tag
        if self.myCurriculum?.list.count ?? 0 > 0{
            let albumMdl = self.myCurriculum?.list[tag]
            CNLiveDownSlideTool.pushWebVCWithContentsMdl(model: albumMdl?.model ?? "", contentId: albumMdl?.contentId ?? "", pid: albumMdl?.pid ?? "", shareUrl: albumMdl?.shareUrl ?? "", thirdUrl: albumMdl?.thirdUrl ?? "", title: albumMdl?.title ?? "", s_to: albumMdl?.s_to ?? "",trailerId: albumMdl?.trailerId ?? "", controller: nil)
        }else if self.slideModel?.contents.count ?? 0 > 0{
            let contentMdl = self.slideModel?.contents[tag]
            CNLiveDownSlideTool.pushWebVCWithContentsMdl(model: contentMdl?.model ?? "", contentId: contentMdl?.contentId ?? "", pid: contentMdl?.pid ?? "", shareUrl: contentMdl?.shareUrl ?? "", thirdUrl: contentMdl?.thirdUrl ?? "", title: contentMdl?.title ?? "", s_to: contentMdl?.s_to ?? "",trailerId: contentMdl?.trailerId ?? "", controller: nil)
        }
    }
}