CNLiveShortVideoCommentListView.swift 21.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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
//
//  CNLiveShortVideoCommentListView.swift
//  metaCode
//
//  Created by open on 2024/1/9.
//

import Foundation
import EmptyDataSet_Swift
import MJRefresh
import CNLiveCustomUI
import Kingfisher
import YYText
import APButton
typealias CloseCommentListResultBlock = (_ commentModel: Any) ->Void
class CNLiveShortVideoCommentListView: 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.text = "0条评论"
        label.numberOfLines = 1
        label.lineBreakMode = .byTruncatingTail
        return label
    }()
    
    lazy var sendButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = HexColor("#f8f8f8")
        button.setTitle("我来说一下...", for: .normal)
        button.layer.cornerRadius = 20
        button.layer.masksToBounds = true
        button.titleLabel?.font = medium_adapt_font(pointSize: 15)
        button.setTitleColor(UIColor.lightGray, for: .normal)
        button.addTarget(self, action: #selector(sendCommentAction), for: .touchUpInside)
        return button
    }()
    
    lazy var emjButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "short_video_commet_emoji"), for: .normal)
        button.backgroundColor = .clear
        button.layer.cornerRadius = 20
        button.layer.masksToBounds = true
        button.addTarget(self, action: #selector(sendCommentAction), for: .touchUpInside)
        return button
    }()
    
    
    lazy var lineLabel: UILabel = {
        let lineLabel = UILabel.init()
        lineLabel.backgroundColor = HexColor("#686868")
        return lineLabel
    }()
    
    let kShortVideoCommentListTableViewCellIdentifier = "kShortVideoCommentListTableViewCellIdentifier"
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        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(CNLiveShortVideoCommentListTableViewCell.self, forCellReuseIdentifier: kShortVideoCommentListTableViewCellIdentifier)
        return tableView
    }()
    
    lazy var dataArray: NSMutableArray = {
        let array = NSMutableArray()
        return array
    }()
    
    var videoId: String!
    var contentSid: String!
    var pageNum: NSInteger! = 0
    var footerRefreshView: CNLiveFooterRefresh!
    var resultBlock: CloseCommentListResultBlock!
    
    func requestDataSource() {
        CNLiveShortVideoViewModel.requestCommentList(pid: self.videoId, page: self.pageNum) {[weak self] result, respStatue in
            guard let self = self else { return }
            if respStatue == .success {
                if self.pageNum == 0 {
                    self.dataArray.removeAllObjects()
                }
                let listArray = (result as! ShortVideoHomeCommentModel).comments
                self.dataArray.addObjects(from: listArray)
                self.titleLabel.text = "\((result as! ShortVideoHomeCommentModel).total_hd ?? 0)条评论"
                if self.tableView.mj_footer != nil {
                    self.tableView.mj_footer?.endRefreshing()
                }
                self.tableView.mj_header?.endRefreshing()
                self.tableView.reloadData()
                let hasNextPage = (result as! ShortVideoHomeCommentModel).hasNext!
                if !hasNextPage {
                    self.tableView.mj_footer?.endRefreshingWithNoMoreData()
                    self.tableView.reloadData()
                } else {
                    if self.tableView.mj_footer == nil  {
                        self.tableView.mj_footer = footerRefreshView
                    }
                }
            } else {
                if self.tableView.mj_footer != nil {
                    self.tableView.mj_footer?.endRefreshing()
                }
                self.tableView.mj_header?.endRefreshing()
                if self.dataArray.count == 0 {
                    self.tableView.reloadEmptyDataSet()
                }
            }
        }
    }
    
    func addRefreshView() {
        self.tableView.mj_header = CNLiveHeaderRefresh(refreshingBlock: { [weak self] in
            guard let self = self else { return }
            self.pageNum = 0
            self.requestDataSource()
        })
        footerRefreshView = CNLiveFooterRefresh(refreshingBlock: { [weak self] in
            guard let self = self else { return }
            self.pageNum += 1
            self.requestDataSource()
        })
    }

    init(frame: CGRect, pid: String, toSid: String) {
        super.init(frame: frame)

        videoId = pid
        contentSid = toSid
        addRefreshView()
        requestDataSource()
        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(emjButton)
        emjButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-(10+get_system_tabbar_ui_offsetHeight()))
            make.size.equalTo(40)
        }
        contentView.addSubview(sendButton)
        sendButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalTo(emjButton.snp.left).offset(-10)
            make.bottom.equalToSuperview().offset(-(10+get_system_tabbar_ui_offsetHeight()))
            make.height.equalTo(40)
        }
        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.left.right.equalToSuperview()
            make.top.equalTo(lineLabel.snp.bottom).offset(10)
            make.bottom.equalTo(sendButton.snp.top).offset(-5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func closeMethod() {
        removeFromSuperview()
    }
    
    @objc func sendCommentAction(button: UIButton) {

        CNLiveContentToolTextView.showCommentViewForCustom(withCommType: .comment, fromNick: "", firstRespFace: button == self.emjButton, successBlcok: {[weak self] commentString, toolbarView in
            guard let self = self else { return }
            print("评论内容\(commentString ?? "")")
            CNLiveShortVideoViewModel.requestCommentWithSend(videoId: self.videoId, comment: commentString ?? "", isAuthor: UserInfoManager.shared.userInfo.uid == self.contentSid) { result, respStatue in
                if respStatue == .success {
                    showMessage(message: result as! String)
                    self.tableView.mj_header?.beginRefreshing()
                }
            }
        }, dissBlock: { toolbarView in
            
        })
    }
    
    static public func showVideoCommentListView(pid: String,contentSid: String,result: @escaping CloseCommentListResultBlock) {
        
        let commentView = CNLiveShortVideoCommentListView.init(frame: CGRect.zero, pid: pid, toSid: contentSid)
        commentView.resultBlock = result
        
        let delegate = UIApplication.shared.delegate as! AppDelegate
        delegate.window?.addSubview(commentView)
        commentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        delegate.window?.layoutIfNeeded()
        commentView.alpha = 0
        UIView.animate(withDuration: 0.1) {
            commentView.alpha = 1
        } completion: { _ in}
    }
}

extension CNLiveShortVideoCommentListView {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kShortVideoCommentListTableViewCellIdentifier) as? CNLiveShortVideoCommentListTableViewCell else {
            return CNLiveShortVideoCommentListTableViewCell()
        }
        cell.setupWithDataSource(commentData: (dataArray[indexPath.row] as! ShortVideoHomeCommentListModel).comment, replyData: (dataArray[indexPath.row] as! ShortVideoHomeCommentListModel).replyData, tableView_h: (dataArray[indexPath.row] as! ShortVideoHomeCommentListModel).tableView_h)
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model: ShortVideoHomeCommentListModel = dataArray[indexPath.row] as! ShortVideoHomeCommentListModel
        return model.cell_h
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model: ShortVideoHomeCommentFListModel = ((dataArray[indexPath.row] as! ShortVideoHomeCommentListModel).comment)
        self.resultBlock(model)
    }
    func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return UIImage(named: "short_video_default")
    }
}

class CNLiveShortVideoCommentListTableViewCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    
    lazy var iconView: UIImageView = {
        let imageView = UIImageView.init()
        imageView.image = UIImage(named: "short_video_default")
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = 20
        imageView.layer.masksToBounds = true
        imageView.clipsToBounds = 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 commentLabel: YYLabel = {
        let commentLabel = YYLabel()
        commentLabel.textColor = .white
        commentLabel.displaysAsynchronously = true
        commentLabel.clearContentsBeforeAsynchronouslyDisplay = false
        commentLabel.fadeOnAsynchronouslyDisplay = false
        commentLabel.font = medium_adapt_font(pointSize: 16)
        commentLabel.numberOfLines = 0
        commentLabel.textAlignment = .left
        commentLabel.textVerticalAlignment = .center
        commentLabel.textContainerInset = UIEdgeInsets.zero
        commentLabel.isUserInteractionEnabled = true
        return commentLabel
    }()
    
    lazy var timeLabel: UILabel = {
        let timeLabel = UILabel.init()
        timeLabel.textColor = .white
        timeLabel.font = light_adapt_font(pointSize: 13)
        return timeLabel
    }()
    
    lazy var replyButton: UIButton = {
        let replyButton = APButton()
        replyButton.setTitleColor(.white, for: .normal)
        replyButton.activityIndicator.color = .white
        replyButton.backgroundColor = .clear
        replyButton.setTitle("回复", for: .normal)
        replyButton.titleLabel?.font = medium_adapt_font(pointSize: 13)
        replyButton.addTarget(self, action: #selector(replyMethod), for: .touchUpInside)
        return replyButton
    }()
    
    lazy var likeButton: QMUIButton = {
        let button = QMUIButton()
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 2.5
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitle("0", for: .normal)
        button.imageView?.contentMode = .scaleAspectFit
        button.setImage(UIImage(named: "short_video_zan_nomal"), for: .normal)
        button.setImage(UIImage(named: "short_video_zan_select"), for: .selected)
        button.titleLabel?.font = regular_adapt_font(pointSize: 14)
        button.addTarget(self, action: #selector(likeActionMethod), for: .touchUpInside)
        return button
    }()
    
    let kShortVideoReplyListTableViewCellIdentifier = "kShortVideoReplyListTableViewCellIdentifier"
    lazy var tableView : UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.backgroundColor = HexColor("#010101")
        tableView.tableFooterView = UIView.init()
        tableView.separatorStyle = .none
        tableView.scrollsToTop = false
        tableView.register(CNLiveShortVideoCommentListTableViewCell.self, forCellReuseIdentifier: kShortVideoReplyListTableViewCellIdentifier)
        return tableView
    }()
    
    var commentModel: ShortVideoHomeCommentFListModel!
    var replyModel: ShortVideoHomeCommentRListModel!
    var replyItemModel: ShortVideoHomeCommentRListItemModel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        selectionStyle = .none
        let isReply = reuseIdentifier == kShortVideoReplyListTableViewCellIdentifier
        
        contentView.addSubview(iconView)
        iconView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalToSuperview().offset(isReply ? 0 : 10)
            make.size.equalTo(40)
        }
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconView.snp.right).offset(10)
            make.top.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(20)
        }
        contentView.addSubview(commentLabel)
        commentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(20)
        }
        contentView.addSubview(likeButton)
        likeButton.snp.makeConstraints { make in
            if isReply {
                make.right.equalToSuperview()
            } else {
                make.right.equalToSuperview().offset(-10)
            }
            make.height.equalTo(20)
            make.width.equalTo(30)
            make.top.equalTo(commentLabel.snp.bottom).offset(10)
        }
        contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.top.equalTo(commentLabel.snp.bottom).offset(10)
            make.left.equalTo(titleLabel.snp.left)
            make.height.equalTo(20)
        }
        contentView.addSubview(replyButton)
        replyButton.snp.makeConstraints { make in
            make.left.equalTo(timeLabel.snp.right).offset(10)
            make.height.equalTo(20)
            make.width.equalTo(40)
            make.top.equalTo(commentLabel.snp.bottom).offset(10)
        }
        contentView.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(60)
            make.right.equalToSuperview()
            make.top.equalTo(timeLabel.snp.bottom).offset(10)
            make.height.equalTo(0.01)
        }
    }
    
    func setupWithDataSource(commentData: ShortVideoHomeCommentFListModel, replyData: ShortVideoHomeCommentRListModel, tableView_h: CGFloat) {
        replyModel = replyData
        commentModel = commentData
        iconView.kf.indicatorType = .activity
        iconView.kf.setImage(with: URL(string: commentData.icon!), placeholder: UIImage(named: "short_video_default"))
        titleLabel.text = commentData.nick
        likeButton.isSelected = commentData.isPraised!
        likeButton.setTitle(commentData.praise, for: .normal)
        timeLabel.text = commentData.created_at
        DispatchQueue.main.async {[weak self] in
            guard let self = self else { return }
            self.commentLabel.textLayout = commentData.textLayout
            self.commentLabel.snp.remakeConstraints { make in
                make.top.equalTo(self.titleLabel.snp.bottom).offset(10)
                make.left.equalTo(self.iconView.snp.right).offset(10)
                make.right.equalToSuperview().offset(-10)
                make.height.equalTo(commentData.textLayout.textBoundingRect.size.height)
            }
        }
        if replyData.replys.count > 0 {
            tableView.reloadData()
            tableView.snp.remakeConstraints { make in
                make.left.equalToSuperview().offset(60)
                make.right.equalToSuperview()
                make.top.equalTo(timeLabel.snp.bottom).offset(10)
                make.height.equalTo(tableView_h)
            }
        }
    }
    
    func setupWithReplyModel(  replyModel: ShortVideoHomeCommentRListItemModel) {
        iconView.kf.indicatorType = .activity
        iconView.kf.setImage(with: URL(string: replyModel.fromFace!), placeholder: UIImage(named: "short_video_default"))
        titleLabel.text = replyModel.fromNick
        likeButton.isSelected = replyModel.isPraised!
        likeButton.setTitle("\(replyModel.praiseCount ?? 0)", for: .normal)
        timeLabel.text = replyModel.created_at
        DispatchQueue.main.async {[weak self] in
            guard let self = self else { return }
            self.commentLabel.textLayout = replyModel.textLayout
            self.commentLabel.snp.remakeConstraints { make in
                make.top.equalTo(self.titleLabel.snp.bottom).offset(10)
                make.left.equalTo(self.iconView.snp.right).offset(10)
                make.right.equalToSuperview().offset(-10)
                make.height.equalTo(replyModel.textLayout.textBoundingRect.size.height)
            }
        }
    }
    
    @objc func likeActionMethod() {
        
    }
    
    @objc func replyMethod() {
        CNLiveContentToolTextView.showCommentViewForCustom(withCommType: .reply, fromNick: commentModel == nil ? replyItemModel.fromNick : commentModel.nick, firstRespFace: false, successBlcok: {[weak self] commentString, toolbarView in
            guard let self = self else { return }
            CNLiveShortVideoViewModel.requestCommentWithReply(commentId: self.commentModel.commentId!, comment: commentString ?? "", toSid: commentModel.uid!, fromSid: UserInfoManager.shared.userInfo.uid, level: "1") { result, respStatue in
                if respStatue == .success {
                    showMessage(message: result as! String)
                    var replyItemModel = ShortVideoHomeCommentRListItemModel()
                    
                }
            }
        }, dissBlock: { toolbarView in
            
        })
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension CNLiveShortVideoCommentListTableViewCell {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return replyModel == nil ? 0 : replyModel.replys.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: kShortVideoReplyListTableViewCellIdentifier) as? CNLiveShortVideoCommentListTableViewCell else {
            return CNLiveShortVideoCommentListTableViewCell()
        }
        cell.setupWithReplyModel(replyModel: replyModel.replys[indexPath.row] )
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model: ShortVideoHomeCommentRListItemModel = replyModel.replys[indexPath.row]
        return model.cellH
    }
}