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

import Foundation
import MJRefresh
import EmptyDataSet_Swift

typealias CloseVideoListResultBlock = (_ listModel: Any) ->Void
typealias ReloadVideoListResultBlock = (_ page: NSInteger) ->Void
class CNLiveShortVideoContentListView: UIView, UITableViewDelegate, UITableViewDataSource, EmptyDataSetSource, EmptyDataSetDelegate {
    
    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.emptyDataSetDelegate = self
        tableView.emptyDataSetSource = 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 aid: String! = ""
    var pageNum: NSInteger! = 1
    var footerRefreshView: CNLiveFooterRefresh!
    var resultBlock: CloseVideoListResultBlock!
    var reloadBlock: ReloadVideoListResultBlock!
    
    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)
        }
        
        addRefreshView()
    }
    
    func addRefreshView() {
        footerRefreshView = CNLiveFooterRefresh(refreshingBlock: { [weak self] in
            guard let self = self else { return }
            self.pageNum += 1
            self.requestData()
        })
        footerRefreshView.stateLabel!.textColor = .white
        footerRefreshView.loadingView?.color = .white
        self.tableView.mj_footer = footerRefreshView
    }
    

    
    func requestData() {
        self.reloadBlock(self.pageNum)
//        CNLiveShortVideoViewModel.requstWithShortVideo(aid: self.aid, page: self.pageNum) { [weak self] result, respStatue in
//            guard let self = self else { return }
//            if respStatue == .success {
//                let listArray = (result as! [Any]).first as! [CNLiveAlbumListModel]
//                self.dataSource.addObjects(from: listArray)
//                let urlArray = (result as! [Any]).last as! [String]
//                if listArray.count > 0 {
//                    self.reloadBlock(NSMutableArray(array: listArray), NSMutableArray(array: urlArray), self.pageNum)
//                }
//                if self.tableView.mj_footer != nil {
//                    self.tableView.mj_footer?.endRefreshing()
//                }
//                self.tableView.reloadData()
//                let hasNextPage = (result as! [Any])[1] as! Bool
//                if listArray.count == 0 {
//                    if !hasNextPage {
//                        self.tableView.mj_footer?.endRefreshingWithNoMoreData()
//                        self.tableView.reloadData()
//                    }
//                }
//            } else {
//                if self.tableView.mj_footer != nil {
//                    self.tableView.mj_footer?.endRefreshing()
//                }
//                self.tableView.contentOffset = CGPoint.zero
//                self.tableView.refreshControl?.endRefreshing()
//                if self.dataSource.count == 0 {
//                    self.tableView.reloadEmptyDataSet()
//                }
//            }
//        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("弹出界面销毁")
    }
    
    @objc func closeMethod() {
        removeFromSuperview()
    }
    
    static public func showVideoContentListView(aid: String, pageNum: NSInteger,title: String,dataArray: NSMutableArray,episodeNow: String ,result: @escaping CloseVideoListResultBlock, reload: @escaping ReloadVideoListResultBlock) -> CNLiveShortVideoContentListView {
        
        let pickerView = CNLiveShortVideoContentListView.init(frame: CGRect.zero)
        pickerView.aid = aid
        pickerView.pageNum = pageNum
        pickerView.resultBlock = result
        pickerView.reloadBlock = reload
        pickerView.setupWithDataSource(title: title,dataArray: dataArray,episodeNow: episodeNow)
        
        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}
        return pickerView
    }
    
    func setupWithDataSource(title: String,dataArray: NSMutableArray,episodeNow: String) {
        dataSource = dataArray
        if Int(episodeNow) == dataArray.count {
            self.tableView.mj_footer?.endRefreshingWithNoMoreData()
        } else {
            self.pageNum = dataArray.count / 10
        }
        titleLabel.text = "\(title)·\(episodeNow)集"
        self.tableView.reloadData()
    }
    
    public func setupWithArray(dataArray: NSMutableArray) {
        dataSource = dataArray
        self.tableView.reloadData()
    }
    
}

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()
    }
    func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return UIImage(named: "short_video_default")
    }
}

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 = .scaleAspectFit
        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.height.equalTo(15)
        }
        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" {
                if listModel.product?.productType == "4" {
                    //这里表示可以进行试听
                    tipView.isHidden = true
                } else {
                    tipView.isHidden = false
                    tipView.image = UIImage(named: "short_video_free")
                }
            }
        } else {
            if listModel.product?.productType == "4" {
                //这里表示可以进行试听
                tipView.isHidden = true
            } else {
                tipView.isHidden = false
                tipView.image = UIImage(named: "short_video_pay")
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}