CNLiveShortVideoCommentListView.swift
36.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//
// 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
import QMUIKit
typealias CloseCommentListResultBlock = (_ commentModel: Any) ->Void
typealias ReloadCommentListResultBlock = (_ commentModel: Any) ->Void
typealias ChangeCommentListResultBlock = (_ commentModel: Any) ->Void
typealias DeleteCommentListResultBlock = (_ commentModel: Any) ->Void
typealias ReloadItemCommentListResultBlock = (_ commentModel: Any) ->Void
typealias ReloadRowCommentListResultBlock = (_ viewCell: CNLiveShortVideoCommentListTableViewCell, _ cell_h: CGFloat) ->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("#FEFFFF")
return view
}()
lazy var closeButton: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "short_video_comment_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 = HexColor("#333333")
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("#F0F0F0")
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("#E6E6E6")
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("#FEFFFF")
tableView.tableFooterView = UIView.init()
tableView.separatorStyle = .none
tableView.scrollsToTop = false
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
tableView.contentInsetAdjustmentBehavior = .never;
tableView.register(CNLiveShortVideoCommentListTableViewCell.self, forCellReuseIdentifier: kShortVideoCommentListTableViewCellIdentifier)
return tableView
}()
lazy var dataArray: NSMutableArray = {
let array = NSMutableArray()
return array
}()
var isReset: Bool! = false
var total_hd: Int! = 0
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.total_hd = (result as! ShortVideoHomeCommentModel).total_hd ?? 0
self.titleLabel.text = "\(self.total_hd ?? 0)条评论"
self.resultBlock(self.total_hd ?? 0)
if self.tableView.mj_footer != nil {
self.tableView.mj_footer?.endRefreshing()
}
self.tableView.mj_header?.endRefreshing()
if self.tableView.mj_footer == nil {
self.tableView.mj_footer = footerRefreshView
}
let hasNextPage = (result as! ShortVideoHomeCommentModel).hasNext!
if !hasNextPage {
self.tableView.mj_footer?.endRefreshingWithNoMoreData()
}
self.tableView.beginUpdates()
self.tableView.reloadSections(IndexSet(integer: 0), with: .fade)
self.tableView.endUpdates()
} 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() {
let headerRefreshView: CNLiveHeaderRefresh = CNLiveHeaderRefresh(refreshingBlock: { [weak self] in
guard let self = self else { return }
self.isReset = true
self.pageNum = 0
self.requestDataSource()
})
headerRefreshView.loadingView?.color = .black
self.tableView.mj_header = headerRefreshView
footerRefreshView = CNLiveFooterRefresh(refreshingBlock: { [weak self] in
guard let self = self else { return }
self.pageNum += 1
self.requestDataSource()
})
footerRefreshView.stateLabel!.textColor = .black
footerRefreshView.loadingView?.color = .black
}
init(frame: CGRect, pid: String, toSid: String) {
super.init(frame: frame)
videoId = pid
contentSid = toSid
// 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)
make.bottom.equalTo(sendButton.snp.top).offset(-5)
}
addRefreshView()
self.tableView.mj_header?.beginRefreshing()
}
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 }
CNLiveShortVideoViewModel.requestCommentWithSend(videoId: self.videoId, comment: commentString ?? "", isAuthor: UserInfoManager.shared.userInfo.uid == self.contentSid) {[weak self] result, respStatue in
guard let self = self else { return }
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
currentViewController()?.view.addSubview(commentView)
commentView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
currentViewController()?.view.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, tableView_top: (dataArray[indexPath.row] as! ShortVideoHomeCommentListModel).tableView_top, indexPath: indexPath, isReset: isReset)
cell.reloadRowBlock = {[weak self] viewCell, cell_h in
guard let self = self else { return }
let commentModel_temp = self.dataArray[viewCell.indexPath_current.row] as! ShortVideoHomeCommentListModel
commentModel_temp.cell_h += cell_h
commentModel_temp.cell_h -= 30
commentModel_temp.tableView_h += cell_h
commentModel_temp.tableView_top = 0.0
commentModel_temp.replyData.replys = viewCell.dataSource as! [ShortVideoHomeCommentRListItemModel]
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [viewCell.indexPath_current], with: .automatic)
self.tableView.endUpdates()
}
cell.reloadBlcok = {[weak self] commentModel in
guard let self = self else { return }
showMessage(message: "")
self.tableView.mj_header?.beginRefreshing()
}
cell.changeBlock = {[weak self] commentModel in
guard let self = self else { return }
self.titleLabel.text = "\(self.total_hd - 1)条评论"
}
cell.deleteBlock = {[weak self] commentModel in
guard let self = self else { return }
showMessage(message: "")
self.tableView.mj_header?.beginRefreshing()
// let tempArray = NSMutableArray(array: self.dataArray)
// let tempModel = commentModel as! ShortVideoHomeCommentFListModel
// tempArray.forEach { itemModel in
// let model: ShortVideoHomeCommentListModel = itemModel as! ShortVideoHomeCommentListModel
// if tempModel.commentId == model.comment.commentId {
// let index = self.dataArray.index(of: itemModel)
// let indexPath_current: IndexPath = IndexPath(row: index, section: 0)
// self.dataArray.remove(itemModel)
// self.tableView.beginUpdates()
// self.tableView.deleteRows(at: [indexPath_current], with: .automatic)
// self.tableView.endUpdates()
// self.titleLabel.text = "\(self.total_hd - (model.replyData.total_hd! + 1))条评论"
// self.tableView.reloadData()
// return
// }
// }
}
cell.videoId = videoId
cell.contentSid = contentSid
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let model: ShortVideoHomeCommentListModel = dataArray[indexPath.row] as! ShortVideoHomeCommentListModel
return model.cell_h
}
func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
return UIImage(named: "short_video_comment_empty")
}
func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
let attributes = NSMutableAttributedString(string: "暂无评论~")
attributes.addAttribute(NSAttributedString.Key.foregroundColor, value: HexColor("#666666")!, range: NSRange(location: 0, length: 5))
attributes.addAttribute(NSAttributedString.Key.font, value: medium_adapt_font(pointSize: 16), range: NSRange(location: 0, length: 5))
return attributes
}
}
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 owerView: UIImageView = {
let imageView = UIImageView.init()
imageView.image = UIImage(named: "short_video_comment_ower")
imageView.contentMode = .scaleAspectFill
imageView.isHidden = true
return imageView
}()
lazy var titleLabel: YYLabel = {
let titleLabel = YYLabel()
titleLabel.textColor = HexColor("#AFAFAF")
titleLabel.lineBreakMode = .byTruncatingTail
titleLabel.numberOfLines = 2
titleLabel.font = medium_adapt_font(pointSize: 12)
titleLabel.displaysAsynchronously = false
titleLabel.clearContentsBeforeAsynchronouslyDisplay = true
titleLabel.fadeOnAsynchronouslyDisplay = false
titleLabel.fadeOnHighlight = false
titleLabel.textAlignment = .left
titleLabel.textVerticalAlignment = .center
titleLabel.textContainerInset = UIEdgeInsets.zero
titleLabel.isUserInteractionEnabled = true
return titleLabel
}()
lazy var commentLabel: YYLabel = {
let commentLabel = YYLabel()
commentLabel.textColor = HexColor("#333333")
commentLabel.displaysAsynchronously = false
commentLabel.clearContentsBeforeAsynchronouslyDisplay = false
commentLabel.fadeOnAsynchronouslyDisplay = false
commentLabel.fadeOnHighlight = false
commentLabel.font = medium_adapt_font(pointSize: 16)
commentLabel.numberOfLines = 0
commentLabel.textAlignment = .left
commentLabel.textVerticalAlignment = .center
commentLabel.textContainerInset = UIEdgeInsets.zero
commentLabel.isUserInteractionEnabled = true
let longPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressGestureRecognizerAction))
longPressGestureRecognizer.minimumPressDuration = 0.5
commentLabel.addGestureRecognizer(longPressGestureRecognizer)
return commentLabel
}()
lazy var timeLabel: UILabel = {
let timeLabel = UILabel.init()
timeLabel.textColor = HexColor("#969696")
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.setTitleColor(HexColor("#969696"), for: .normal)
replyButton.titleLabel?.font = medium_adapt_font(pointSize: 13)
replyButton.addTarget(self, action: #selector(replyMethod), for: .touchUpInside)
return replyButton
}()
lazy var replyUnfoldButton: UIButton = {
let button = QMUIButton()
button.imagePosition = .right
button.spacingBetweenImageAndTitle = 3
button.backgroundColor = .clear
button.isHidden = true
button.setTitle(" ── 展开回复", for: .normal)
button.setTitleColor(HexColor("#969696"), for: .normal)
button.titleLabel?.font = regular_adapt_font(pointSize: 13)
button.imageView?.contentMode = .scaleAspectFit
button.setImage(UIImage(named: "short_video_reply_open"), for: .normal)
button.setImage(UIImage(named: "short_video_reply_close"), for: .selected)
button.addTarget(self, action: #selector(replyUnfoldMethod), for: .touchUpInside)
return button
}()
lazy var likeButton: QMUIButton = {
let button = QMUIButton()
button.imagePosition = .left
button.spacingBetweenImageAndTitle = 2
button.setTitleColor(HexColor("#333333"), for: .normal)
button.setTitle("0", for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.setImage(UIImage(named: "short_comment_zan_nomal"), for: .normal)
button.setImage(UIImage(named: "short_comment_zan_select"), for: .selected)
button.titleLabel?.font = semibold_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 = .white
tableView.tableFooterView = UIView.init()
tableView.separatorStyle = .none
tableView.scrollsToTop = false
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(CNLiveShortVideoCommentListTableViewCell.self, forCellReuseIdentifier: kShortVideoReplyListTableViewCellIdentifier)
return tableView
}()
lazy var dataSource: NSMutableArray = {
let array = NSMutableArray()
return array
}()
var videoId: String!
var contentSid: String!
var tableView_height: CGFloat! = 0.0
var indexPath_current: IndexPath!
var changeBlock: ChangeCommentListResultBlock!
var deleteBlock: DeleteCommentListResultBlock!
var reloadBlcok: ReloadCommentListResultBlock!
var deleteItemBlock: DeleteCommentListResultBlock!
var commentModel: ShortVideoHomeCommentFListModel!
var reloadRowBlock: ReloadRowCommentListResultBlock!
var reloadItemBlock: ReloadItemCommentListResultBlock!
var replyItemModel: ShortVideoHomeCommentRListItemModel!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = HexColor("#FEFFFF")
backgroundColor = HexColor("#FEFFFF")
selectionStyle = .none
contentView.addSubview(iconView)
iconView.snp.makeConstraints { make in
make.top.equalToSuperview().offset(10)
make.left.equalToSuperview().offset(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(1)
}
contentView.addSubview(owerView)
owerView.snp.makeConstraints { make in
make.centerY.equalTo(titleLabel)
make.left.equalTo(titleLabel.snp.right).offset(10)
}
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(1)
}
contentView.addSubview(likeButton)
likeButton.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-10)
make.height.equalTo(20)
make.width.equalTo(50)
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(replyUnfoldButton)
replyUnfoldButton.snp.makeConstraints { make in
make.top.equalTo(timeLabel.snp.bottom)
make.left.equalTo(titleLabel.snp.left)
make.height.equalTo(30)
}
contentView.addSubview(tableView)
}
func setupWithDataSource(commentData: ShortVideoHomeCommentFListModel, replyData: ShortVideoHomeCommentRListModel, tableView_h: CGFloat, tableView_top: CGFloat, indexPath: IndexPath, isReset: Bool) {
dataSource.removeAllObjects()
dataSource.addObjects(from: replyData.replys)
tableView_height = tableView_h
indexPath_current = indexPath
commentModel = commentData
iconView.kf.indicatorType = .activity
iconView.kf.setImage(with: URL(string: commentData.facepath!), placeholder: UIImage(named: "short_video_default"))
likeButton.isSelected = commentData.isPraised!
likeButton.setTitle("\(commentData.praiseCount ?? 0)", for: .normal)
timeLabel.text = commentData.created_at
owerView.isHidden = !(contentSid == commentData.uid)
if tableView_top > 0.0 {
replyUnfoldButton.isHidden = replyData.replyCount! <= 2
replyUnfoldButton.setTitle(" ── 展开\(replyData.replyCount! - 2)条回复", for: .normal)
} else {
replyUnfoldButton.isHidden = true
}
if ((contentSid == UserInfoManager.shared.userInfo.uid) || (commentData.uid == UserInfoManager.shared.userInfo.uid)) {
replyButton.setTitle("删除", for: .normal)
} else {
replyButton.setTitle("回复", for: .normal)
}
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)
}
self.titleLabel.textLayout = commentData.nickLayout
self.titleLabel.snp.remakeConstraints { make in
make.left.equalTo(self.iconView.snp.right).offset(10)
make.top.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(commentData.nickLayout.textBoundingRect.size.height)
}
self.tableView.snp.removeConstraints()
self.tableView.removeFromSuperview()
if replyData.replyCount ?? 0 > 0 {
self.contentView.addSubview(self.tableView)
self.tableView.snp.makeConstraints { make in
make.left.equalToSuperview().offset(60)
make.right.equalToSuperview().offset(-10)
make.top.equalTo(self.timeLabel.snp.bottom).offset(tableView_top)
make.height.equalTo(self.tableView_height)
}
self.tableView.reloadData()
}
}
}
func setupWithReplyModel(commentData: ShortVideoHomeCommentFListModel,replyModel: ShortVideoHomeCommentRListItemModel) {
replyItemModel = replyModel
commentModel = commentData
iconView.kf.indicatorType = .activity
iconView.kf.setImage(with: URL(string: replyModel.fromFace!), placeholder: UIImage(named: "short_video_default"))
titleLabel.textLayout = replyModel.nickLayout
likeButton.isSelected = replyModel.isPraised!
likeButton.setTitle("\(replyModel.praiseCount ?? 0)", for: .normal)
timeLabel.text = replyModel.created_at
if ((contentSid == UserInfoManager.shared.userInfo.uid) || (replyItemModel.fromSid == UserInfoManager.shared.userInfo.uid)) {
replyButton.setTitle("删除", for: .normal)
} else {
replyButton.setTitle("回复", for: .normal)
}
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)
}
self.titleLabel.textLayout = replyModel.nickLayout
self.titleLabel.snp.remakeConstraints { make in
make.left.equalTo(self.iconView.snp.right).offset(10)
make.top.equalToSuperview().offset(10)
make.right.equalToSuperview().offset(-10)
make.height.equalTo(replyModel.nickLayout.textBoundingRect.size.height)
}
}
}
@objc func replyMethod(button: UIButton) {
if button.titleLabel?.text == "回复" {
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: self.commentModel.uid!, fromSid: UserInfoManager.shared.userInfo.uid, level: "1") {[weak self] result, respStatue in
guard let self = self else { return }
if respStatue == .success {
showMessage(message: result as! String)
if self.replyItemModel != nil {
self.reloadItemBlock(self.commentModel == nil ? replyItemModel! : commentModel!)
} else {
self.reloadBlcok(self.commentModel == nil ? replyItemModel! : commentModel!)
}
}
}
}, dissBlock: { toolbarView in})
} else {
AlertTool.systemAlert(title: "确认删除该条\(commentModel != nil ? "评论" : "回复")吗?", message: "", cancelTitle: "取消", confirmTitle: "确定") {[weak self] index in
guard let self = self else { return }
if index == 1 {
if replyItemModel != nil {
showLoading(message: "")
CNLiveShortVideoViewModel.requestCommentReplyDelete(replyId: replyItemModel.replyId!, commentId: replyItemModel.commentId!) {[weak self] result, respStatue in
guard let self = self else { return }
if respStatue == .success {
self.deleteItemBlock(self.replyItemModel!)
}
}
} else {
showLoading(message: "")
CNLiveShortVideoViewModel.requestCommentDelete(videoId: commentModel.topicId!, commentId: commentModel.commentId!) {[weak self] result, respStatue in
guard let self = self else { return }
if respStatue == .success {
self.deleteBlock(self.commentModel!)
}
}
}
}
}
}
}
@objc func longPressGestureRecognizerAction(gestureRecognizer: UIGestureRecognizer) {
if gestureRecognizer.state == .began {
if commentModel.uid == UserInfoManager.shared.userInfo.uid || contentSid == UserInfoManager.shared.userInfo.uid {
AlertTool.systemAlert(title: "确认删除该条评论吗?", message: "", cancelTitle: "取消", confirmTitle: "确定") {[weak self] index in
guard let self = self else { return }
if index == 1 {
showLoading(message: "")
CNLiveShortVideoViewModel.requestCommentDelete(videoId: self.videoId, commentId: self.commentModel.commentId!) {result, respStatue in
if respStatue == .success {
self.deleteBlock(self.commentModel!)
}
}
}
}
}
}
}
@objc func likeActionMethod(button: UIButton) {
let contentId: String = commentModel == nil ? replyItemModel.replyId! : commentModel.commentId!
let support: Bool = commentModel == nil ? replyItemModel.isPraised! : commentModel.isPraised!
let isComment: Bool = commentModel != nil
CNLiveShortVideoViewModel.requestCommentWithLike(contentId: contentId, support: support, isComment: isComment) {[weak self] result, respStatue in
guard let self = self else { return }
if respStatue == .success {
showMessage(message: result as! String)
if isComment {
commentModel.isPraised = !commentModel.isPraised!
if commentModel.isPraised! {
commentModel.praiseCount = commentModel.praiseCount! + 1
} else {
commentModel.praiseCount = commentModel.praiseCount! - 1
}
button.setTitle("\(commentModel.praiseCount ?? 0)", for: .normal)
button.isSelected = commentModel.isPraised!
} else {
replyItemModel.isPraised = !replyItemModel.isPraised!
if replyItemModel.isPraised! {
replyItemModel.praiseCount = replyItemModel.praiseCount! + 1
} else {
replyItemModel.praiseCount = replyItemModel.praiseCount! - 1
}
button.setTitle("\(replyItemModel.praiseCount ?? 0)", for: .normal)
button.isSelected = replyItemModel.isPraised!
}
}
}
}
@objc func replyUnfoldMethod(button: UIButton) {
showLoading(message: "")
print("这里点击的时候走了多少次")
let replyModel: ShortVideoHomeCommentRListItemModel = self.dataSource.lastObject as! ShortVideoHomeCommentRListItemModel
CNLiveShortVideoViewModel.requestcommentWithReplyList(commentId: commentModel.commentId!, seqId: replyModel.seqId!) {[weak self] result, respStatue in
guard let self = self else { return }
if respStatue == .success {
let tempArray = result as! [ShortVideoHomeCommentRListItemModel]
if tempArray.count == 0 {
return
}
let dataArray_temp = NSMutableArray(array: self.dataSource)
self.dataSource.removeAllObjects()
var cell_h: CGFloat = 0.0
tempArray.forEach { item in
dataArray_temp.add(item)
cell_h += item.cellH
}
self.dataSource.addObjects(from: dataArray_temp as! [ShortVideoHomeCommentRListItemModel])
self.reloadRowBlock(self, cell_h)
}
}
}
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 dataSource.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: kShortVideoReplyListTableViewCellIdentifier) as? CNLiveShortVideoCommentListTableViewCell else {
return CNLiveShortVideoCommentListTableViewCell()
}
cell.setupWithReplyModel(commentData: commentModel,replyModel: dataSource[indexPath.row] as! ShortVideoHomeCommentRListItemModel)
cell.reloadItemBlock = {[weak self] commentModel in
guard let self = self else { return }
self.reloadBlcok(commentModel)
}
cell.deleteItemBlock = {[weak self] commentModel in
guard let self = self else { return }
self.deleteBlock(commentModel)
// let tempArray = NSMutableArray(array: replyModel.replys)
// let tempModel = commentModel as! ShortVideoHomeCommentRListItemModel
// tempArray.forEach { itemModel in
// let model: ShortVideoHomeCommentRListItemModel = itemModel as! ShortVideoHomeCommentRListItemModel
// if tempModel.replyId == model.replyId {
// let index = tempArray.index(of: model)
// let indexPath_current: IndexPath = IndexPath(row: index, section: 0)
// tempArray.remove(itemModel)
// self.replyModel.replys = tempArray as! [ShortVideoHomeCommentRListItemModel]
// self.tableView.beginUpdates()
// self.tableView.deleteRows(at: [indexPath_current], with: .automatic)
// self.tableView.endUpdates()
// self.changeBlock(itemModel)
// return
// }
// }
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let model: ShortVideoHomeCommentRListItemModel = dataSource[indexPath.row] as! ShortVideoHomeCommentRListItemModel
return model.cellH
}
}