CNLiveShortVideoContentListView.swift 9.47 KB
//
//  CNLiveShortVideoContentListView.swift
//  metaCode
//
//  Created by open on 2024/1/9.
//

import Foundation

typealias CloseVideoListResultBlock = (_ listModel: Any) ->Void
class CNLiveShortVideoContentListView: UIView, UITableViewDelegate, UITableViewDataSource {
    
    lazy var grayMaskView: UIView = {
       let grayMaskView = UIView()
       grayMaskView.backgroundColor = UIColor.black.withAlphaComponent(0.2)
       return grayMaskView
    }()
    
    lazy var contentView: UIView = {
        let view = UIView.init()
        view.backgroundColor = HexColor("#010101")
        return view
    }()
    
    lazy var closeButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "short_video_close"), for: .normal)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(closeMethod), for: .touchUpInside)
        return button
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = semibold_adapt_font(pointSize: 19)
        label.textAlignment = .left
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    lazy var lineLabel: UILabel = {
        let lineLabel = UILabel.init()
        lineLabel.backgroundColor = HexColor("#686868")
        return lineLabel
    }()
    
    let kVideoContentListViewCellIdentifier = "kVideoContentListViewCellIdentifier"
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = 90
        tableView.estimatedRowHeight = 90
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.backgroundColor = HexColor("#010101")
        tableView.tableFooterView = UIView.init()
        tableView.separatorStyle = .none
        tableView.scrollsToTop = false
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: get_system_tabbar_ui_offsetHeight(), right: 0)
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(CNLiveShortVideoContentListTableViewCell.self, forCellReuseIdentifier: kVideoContentListViewCellIdentifier)
        return tableView
    }()
    
    lazy var dataSource: NSMutableArray = {
        let array = NSMutableArray()
        return array
    }()
    
    var resultBlock: CloseVideoListResultBlock!
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(grayMaskView)
        grayMaskView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        grayMaskView.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.top.equalTo(250)
            make.right.left.bottom.equalToSuperview()
        }
        contentView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(15)
            make.size.equalTo(30)
        }
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview().offset(20)
            make.right.equalTo(closeButton.snp.left).offset(-10)
        }
        contentView.addSubview(lineLabel)
        lineLabel.snp.makeConstraints { make in
            make.top.equalTo(closeButton.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(1)
        }
        contentView.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.top.equalTo(lineLabel.snp.bottom).offset(10)
        }
    }
    
    func setupWithDataSource(title: String,dataArray: NSMutableArray) {
        dataSource = dataArray
        titleLabel.text = title
        self.tableView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("弹出界面销毁")
    }
    
    @objc func closeMethod() {
        removeFromSuperview()
    }
    
    static public func showVideoContentListView(title: String,dataArray: NSMutableArray,result: @escaping CloseVideoListResultBlock) {
        
        let pickerView = CNLiveShortVideoContentListView.init(frame: CGRect.zero)
        pickerView.resultBlock = result
        pickerView.setupWithDataSource(title: title,dataArray: dataArray)
        
        let delegate = UIApplication.shared.delegate as! AppDelegate
        delegate.window?.addSubview(pickerView)
        pickerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        delegate.window?.layoutIfNeeded()
        pickerView.alpha = 0
        UIView.animate(withDuration: 0.1) {
            pickerView.alpha = 1
        } completion: { _ in}
    }
    
}

extension CNLiveShortVideoContentListView {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kVideoContentListViewCellIdentifier) as? CNLiveShortVideoContentListTableViewCell else {
            return CNLiveShortVideoContentListTableViewCell()
        }
        cell.setupWithDataModel(listModel: dataSource[indexPath.row] as! CNLiveAlbumListModel)
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.resultBlock(dataSource[indexPath.row] as! CNLiveAlbumListModel)
        closeMethod()
    }
}

class CNLiveShortVideoContentListTableViewCell: UITableViewCell {
    
    lazy var iconView: UIImageView = {
        let imageView = UIImageView.init()
        imageView.image = UIImage(named: "short_video_default")
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        return imageView
    }()
    
    lazy var tipView: UIImageView = {
        let imageView = UIImageView.init()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = true
        imageView.isHidden = true
        return imageView
    }()
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.textColor = .white
        titleLabel.lineBreakMode = .byTruncatingTail
        titleLabel.numberOfLines = 2
        titleLabel.font = medium_adapt_font(pointSize: 12)
        return titleLabel
    }()
    
    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel.init()
        timeLabel.textColor = .white
        timeLabel.font = light_adapt_font(pointSize: 13)
        return timeLabel
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        selectionStyle = .none
        

        contentView.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(20)
            make.width.equalTo(50)
            make.height.equalTo(70)
        }
        contentView.addSubview(tipView)
        tipView.snp.makeConstraints { make in
            make.left.top.equalToSuperview().offset(20)
            make.width.equalTo(50)
            make.height.equalTo(10)
        }
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconView.snp.right).offset(10)
            make.top.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
        }
        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(iconView.snp.right).offset(10)
            make.bottom.equalTo(iconView.snp.bottom)
            make.right.equalToSuperview().offset(-20)
        }
    }
    
    func setupWithDataModel(listModel: CNLiveAlbumListModel) {
        iconView.kf.indicatorType = .activity
        iconView.kf.setImage(with: URL(string: listModel.poster), placeholder: UIImage(named: "short_video_default"))
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 6
        titleLabel.attributedText = NSAttributedString(string: listModel.title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, ])
        timeLabel.text = listModel.duration
        
        if listModel.product?.check == true {
            if listModel.product?.albumProductType != "0" {
                tipView.isHidden = false
                tipView.image = UIImage(named: "short_video_free")
            }
        } else {
            if listModel.product?.albumProductType != "0" {
                tipView.isHidden = false
                tipView.image = UIImage(named: "short_video_pay")
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}