CNLivePlayletHomePageHeaderCell.swift 17.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
//
//  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
//刷新短剧首页
typealias homePageNeedReloadBlockActionBlock = () -> Void


class CNLivePlayletHomePageHeaderCell:UITableViewCell{
    var homePageHeaderCellChangedBlock:homePageHeaderCellChangedBlockActionBlock?
    var homePageNeedReloadActionBlock:homePageNeedReloadBlockActionBlock?

    
    let followBtnSubLayer = "followBtnSubLayer"
    let payBtnSubLayer = "payBtnSubLayer"
    
    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: 15, y: 20, width: 112, height: 147))
        iconImgView.layer.cornerRadius = 5
        iconImgView.contentMode = .scaleAspectFill
        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 = "1集"
        let height = titleLabel.qmui_lineHeight
        titleLabel.height = height
        return titleLabel
    }()
    ///简介背景
    lazy var jianjieBgView :UIView = {
        let jianjieBgView = UIView(frame: CGRect(x: self.iconImgView.left, y: self.iconImgView.bottom+10, width: KSreenWidth-2*self.iconImgView.left, height: 21))
        jianjieBgView.backgroundColor = UIColor(red:0.98, green:0.98, blue:0.98, alpha:1.00)
        return jianjieBgView
    }()
    ///简介
    lazy var jianjieLabel: UILabel = {
        let titleLabel = UILabel.init(frame: CGRectMake(self.iconImgView.left+10, self.iconImgView.bottom+20, KSreenWidth-2*self.iconImgView.left-13, 21))
        titleLabel.textColor = HexColor("#333333")
        titleLabel.textAlignment = .left
        titleLabel.font = Font_12
        titleLabel.backgroundColor = .clear// 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 = .clear
        
        expandBtn.frame = CGRect(x: self.jianjieBgView.right-60, y: self.jianjieBgView.bottom-10, 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)
        return followBtn
    }()
    //付费解锁全集|已购买
    lazy var paymentBtn: UIButton = {
        let  paymentBtn = UIButton(type: .custom)
        paymentBtn.setTitle("付费解锁全集", for: .normal)
        paymentBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
        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)
        gradient.name = payBtnSubLayer
        paymentBtn.layer.insertSublayer(gradient, at: 0)
        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.jianjieBgView)
        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.jianjieBgView.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?.check == true{
            //付过费 或 免费
            self.paymentBtn.isUserInteractionEnabled = false
            self.paymentChangeStatus(isPayed: true)
            self.payIconImgView.isHidden = true
            //显示已购买 或者 不显示
            if model.albumMsgModel?.product?.albumProductType == "0"{
                //免费
                self.payIconImgView.isHidden = true
            }else if model.product?.albumProductType == "1"{
                //积分
                self.payIconImgView.isHidden = false
                self.payIconImgView.image = UIImage(named: "homePage_payed")
            }else if model.product?.albumProductType == "2"{
                //付费
                self.payIconImgView.isHidden = false
                self.payIconImgView.image = UIImage(named: "homePage_payed")
            }else{
                self.payIconImgView.isHidden = false
                self.payIconImgView.image = UIImage(named: "homePage_payed")
            }
        }else{
            //需要付费
            self.paymentBtn.isUserInteractionEnabled = true
            self.paymentChangeStatus(isPayed: false)
            if model.product?.albumProductType == "0" || model.product?.albumProductType == "4"{
                //免费
                self.payIconImgView.isHidden = true
            }else if model.product?.albumProductType == "1"{
                //积分
                self.payIconImgView.isHidden = false
                self.payIconImgView.image = UIImage(named: "homePage_payment")
            }else if model.product?.albumProductType == "2"{
                //付费
                self.payIconImgView.isHidden = false
                self.payIconImgView.image = UIImage(named: "homePage_payment")
            }else{
                self.payIconImgView.isHidden = false
                self.payIconImgView.image = UIImage(named: "homePage_payment")
            }
        }
                
        self.jianjieLabel.text = (model.albumMsgModel?.desc != "") ? model.albumMsgModel?.desc : "短剧简介"
        let 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{
            //收起状态
            if lines <= 2{
                //小于2行,按实际行数
                self.jianjieLabel.height = CGFloat(lines)*height
            }else{
                //大于2行, 显示2行
                self.jianjieLabel.height = 2*height
            }
        }
        self.jianjieBgView.height = self.jianjieLabel.height+20
        self.expandBtn.top = self.jianjieBgView.bottom-10
        if model.albumMsgModel!.collection{
            self.followChangeStatus(isFollowed: true)
        }else{
            self.followChangeStatus(isFollowed: false)
        }
    }
    
    // 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.jianjieBgView.height = self.jianjieLabel.height+20
        self.homePageHeaderCellChangedBlock!(self, self.jianjieLabel.height)
        
        self.expandBtn.top = self.jianjieBgView.bottom-10

    }
    @objc func followBtnAction(button:UIButton){
        button.isSelected = !button.isSelected
        if button.isSelected{
            //已追剧->取消追剧
            if self.albumListModel?.albumMsgModel?.product?.check == true{
                //已购买,不能取消追剧
            }else{
                self.postAlbumCollect(type: 2)
            }
        }else{
            //未追剧->追剧
            self.postAlbumCollect(type: 1)
        }
    }
    @objc func paymentBtnAction(button:UIButton){
        button.isSelected = !button.isSelected
        if self.albumListModel?.product?.check == false{
            //未购买->购买
            CNLiveIntegralAlertTool.shared.showIntegralAlert(title: "\(self.albumListModel?.albumMsgModel?.albumName ?? "")\(self.albumListModel?.albumMsgModel?.episodeNow ?? "1")集", integralCount: self.albumListModel?.albumMsgModel?.product?.point ?? "0", productId: self.albumListModel?.product?.productId ?? "")
            CNLiveIntegralAlertTool.shared.integralStatusChangedBlock = { integralPayStatus in
                if integralPayStatus == .paySuccess{
                    self.homePageNeedReloadActionBlock!()
                    DispatchQueue.main.asyncAfter(deadline: .now()+1.5) { [self] in
                        self.followChangeStatus(isFollowed: true)
                    }
                }
            }
        }
    }
    func paymentChangeStatus(isPayed:Bool){
        if isPayed{
            //已购买
            self.paymentBtn.setTitle("已购买", for: .normal)
            self.paymentBtn.setTitleColor(HexColor("#619CFF"), for: .normal)
            
            let payLayer = self.subLayer(name: payBtnSubLayer, viewLayer: self.paymentBtn.layer)

            payLayer?.removeFromSuperlayer()
            self.paymentBtn.backgroundColor = HexColor("#ECF3FF")
        }else{
            //未购买
            self.paymentBtn.setTitle("付费解锁全集", for: .normal)
            self.paymentBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
            
            let payLayer = self.subLayer(name: payBtnSubLayer, viewLayer: self.paymentBtn.layer)

            if (payLayer != nil){
                //已添加layer
            }else{
                //未添加layer
                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)
                gradient.name = followBtnSubLayer
                self.paymentBtn.layer.insertSublayer(gradient, at: 0)
                self.paymentBtn.backgroundColor = .clear
            }
        }
        
    }
    func followChangeStatus(isFollowed:Bool){
        if isFollowed{
            //已追剧
            self.followBtn.setTitleColor(HexColor("#F5A623"), for: .normal)
            self.followBtn.setTitle("已追剧", for: .normal)
            let follLayer = self.subLayer(name: followBtnSubLayer, viewLayer: self.followBtn.layer)
            follLayer?.removeFromSuperlayer()
            self.followBtn.backgroundColor = HexColor("#FDF4E4")
        }else{
            //未追剧
            self.followBtn.setTitle("追剧", for: .normal)
            self.followBtn.setTitleColor(HexColor("#FFFFFF"), for: .normal)
            let follLayer = self.subLayer(name: followBtnSubLayer, viewLayer: self.followBtn.layer)
            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
            }

        }
    }
    
    func subLayer(name:String,viewLayer:CALayer) -> CALayer?{
        if let subLayer = viewLayer.sublayers?.first(where: { $0.name == name }) {
            // 这里可以对获取到的子图层进行操作或者访问其属性等
            return subLayer
        } else {
//            print("未找到指定名称的图层")
            return nil
        }
    }

    // MARK: - 网络请求 追剧1/取消追剧2
    func postAlbumCollect(type:NSInteger){
        CNLiveHomePageViewModel.postAlbumCollect(aid: self.albumListModel?.albumMsgModel?.albumId ?? "", type: type) { result, status in
            if status == .success{
                if type == 1{
                    self.followChangeStatus(isFollowed: true)
                }else if type == 0{
                    self.followChangeStatus(isFollowed: false)
                }else{
                    self.followChangeStatus(isFollowed: false)
                }
            }else{
                showMessage(message: "网络请求失败")
            }
        }
    }
    
}