Files
yinmeng-ios/xplan-ios/Main/Monents/View/Cell/XPMonentsCommentTableViewCell.m
2022-11-11 17:46:37 +08:00

330 lines
12 KiB
Objective-C

//
// XPMonentsCommentTableViewCell.m
// xplan-ios
//
// Created by 冯硕 on 2022/6/22.
//
#import "XPMonentsCommentTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
#import <YYText/YYText.h>
///Tool
#import "NetImageView.h"
#import "ThemeColor.h"
#import "XPMonentsLayoutConfig.h"
#import "NSString+Utils.h"
#import "QEmotionHelper.h"
#import "AccountInfoStorage.h"
#import "NSArray+Safe.h"
///Model
#import "MonentsCommentModel.h"
///View
#import "XPMonentsReplyTableViewCell.h"
#import "XPMonentsReplyMoreTableViewCell.h"
@interface XPMonentsCommentTableViewCell ()<UITableViewDelegate, UITableViewDataSource, XPMonentsReplyMoreTableViewCellDelegate, XPMonentsReplyTableViewCellDelegate>
///容器
@property (nonatomic,strong) UIStackView *stackView;
///评论者
@property (nonatomic,strong) UIView * commentUserView;
///评论者的头像
@property (nonatomic,strong) NetImageView *commentAvatarView;
///评论者的昵称
@property (nonatomic,strong) UILabel *commentNickLabel;
///楼主
@property (nonatomic,strong) UIImageView *ownerImageView;
///评论的内容
@property (nonatomic,strong) YYLabel *commentLabel;
///时间
@property (nonatomic,strong) UILabel *commentDateLabel;
///列表
@property (nonatomic,strong) UITableView *tableView;
///容器
@property (nonatomic,strong) UIView * lineContainerView;
///分割线
@property (nonatomic,strong) UIView * lineView;
///数据源
@property (nonatomic,strong) NSArray *datasource;
@end
@implementation XPMonentsCommentTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [ThemeColor appCellBackgroundColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.stackView];
[self.stackView addArrangedSubview:self.commentUserView];
[self.stackView addArrangedSubview:self.tableView];
[self.stackView addArrangedSubview:self.lineContainerView];
[self.lineContainerView addSubview:self.lineView];
[self.commentUserView addSubview:self.commentAvatarView];
[self.commentUserView addSubview:self.commentNickLabel];
[self.commentUserView addSubview:self.ownerImageView];
[self.commentUserView addSubview:self.commentLabel];
[self.commentUserView addSubview:self.commentDateLabel];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.left.mas_equalTo(self.contentView).inset(15);
make.top.bottom.mas_equalTo(self.contentView);
}];
[self.lineContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(1);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.commentNickLabel);
make.top.bottom.right.mas_equalTo(self.lineContainerView);
}];
[self.commentAvatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(KMONENTS_COMMENT_AVATAR_WIDTH, KMONENTS_COMMENT_AVATAR_WIDTH));
make.left.mas_equalTo(self.commentUserView);
make.top.mas_equalTo(self.commentUserView).offset(10);
}];
[self.commentNickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.commentAvatarView.mas_right).offset(10);
make.top.mas_equalTo(self.commentUserView).offset(10);
make.height.mas_equalTo(15);
}];
[self.ownerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(32, 15));
make.centerY.mas_equalTo(self.commentNickLabel);
make.left.mas_equalTo(self.commentNickLabel.mas_right).offset(5);
}];
[self.commentDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.commentUserView);
make.centerY.mas_equalTo(self.commentNickLabel);
}];
[self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.commentNickLabel);
make.top.mas_equalTo(self.commentNickLabel.mas_bottom).offset(10);
make.right.mas_equalTo(self.commentUserView).offset(-kMONENTS_COMMENT_RIGHT_PADDING);
}];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return self.datasource.count;
}
return self.commentInfo.replyInfo.leftCount > 0 ? 1 : 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
MonentsReplyModel * replayinfo = [self.datasource safeObjectAtIndex1:indexPath.row];
return replayinfo.replyRowHeight;
}
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
XPMonentsReplyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMonentsReplyTableViewCell class])];
cell.replyInfo = [self.datasource safeObjectAtIndex1:indexPath.row];
cell.delegate = self;
return cell;
}
XPMonentsReplyMoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPMonentsReplyMoreTableViewCell class])];
cell.leftCount = self.commentInfo.replyInfo.leftCount;
cell.delegate = self;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0 && self.datasource.count > 0) {
MonentsReplyModel * replyInfo = [self.datasource safeObjectAtIndex1:indexPath.row];
if (replyInfo.uid.integerValue != [AccountInfoStorage instance].getUid.integerValue && self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsCommentTableViewCell:didClickCommon:)]) {
[self.delegate xPMonentsCommentTableViewCell:self didClickCommon:replyInfo];
}
}
}
#pragma mark - XPMonentsReplyTableViewCellDelegate
- (void)xPMonentsReplyTableViewCell:(XPMonentsReplyTableViewCell *)view didClickAvatar:(MonentsReplyModel *)replyInfo {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsCommentTableViewCell:didClickAvatar:)]) {
[self.delegate xPMonentsCommentTableViewCell:self didClickAvatar:replyInfo.uid];
}
}
#pragma mark - XPMonentsReplyMoreTableViewCellDelegate
- (void)xPMonentsReplyMoreTableViewCellDidClickMoreReply:(XPMonentsReplyMoreTableViewCell *)view {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsCommentTableViewCell:didClickMoreReply:)]) {
[self.delegate xPMonentsCommentTableViewCell:self didClickMoreReply:self.commentInfo];
}
}
#pragma mark - Event Response
- (void)tapAvatarRecognizer {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMonentsCommentTableViewCell:didClickAvatar:)]) {
[self.delegate xPMonentsCommentTableViewCell:self didClickAvatar:self.commentInfo.uid];
}
}
#pragma mark - Getters And Setters
- (void)setCommentInfo:(MonentsCommentModel *)commentInfo {
_commentInfo = commentInfo;
if (_commentInfo) {
self.ownerImageView.hidden = !_commentInfo.landLordFlag;
self.commentAvatarView.imageUrl = _commentInfo.avatar;
NSString * nick = _commentInfo.nick;
if (nick.length > 8) {
nick = [nick substringToIndex:8];
}
QEmotionHelper *faceManager = [QEmotionHelper sharedEmotionHelper];
///内容
NSMutableAttributedString * attribute = [faceManager attributedStringByText:_commentInfo.content font:[UIFont systemFontOfSize:15]];
self.commentLabel.attributedText = attribute;
self.commentNickLabel.text = nick;
self.commentDateLabel.text = [NSString stringWithTimeStamp:_commentInfo.publishTime];
self.tableView.hidden = _commentInfo.replyInfo.replyList.count <=0;
self.datasource = _commentInfo.replyInfo.replyList;
[self.tableView reloadData];
if (_commentInfo.replyInfo.replyList.count > 0) {
NSString * content = _commentInfo.content.length > 0 ? _commentInfo.content : @"";
CGFloat commentHeight = [content boundingRectWithSize:CGSizeMake(KMONENTS_COMMENT_MAX_WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height + 5;
commentHeight = MAX(commentHeight, 15);
[self.commentUserView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(commentHeight + 10 + 15 + 10);
}];
}
}
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.spacing = 10;
}
return _stackView;
}
- (UIView *)commentUserView {
if (!_commentUserView) {
_commentUserView = [[UIView alloc] init];
_commentUserView.backgroundColor = [UIColor clearColor];
}
return _commentUserView;
}
- (NetImageView *)commentAvatarView {
if (!_commentAvatarView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeUserIcon;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_commentAvatarView = [[NetImageView alloc] initWithConfig:config];
_commentAvatarView.layer.masksToBounds = YES;
_commentAvatarView.layer.cornerRadius = 45/2.0;
_commentAvatarView.userInteractionEnabled = YES;
UITapGestureRecognizer * tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAvatarRecognizer)];
[_commentAvatarView addGestureRecognizer:tap];
}
return _commentAvatarView;
}
- (UILabel *)commentNickLabel {
if (!_commentNickLabel) {
_commentNickLabel = [[UILabel alloc] init];
_commentNickLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
_commentNickLabel.textColor = [ThemeColor mainTextColor];
[_commentNickLabel sizeToFit];
}
return _commentNickLabel;
}
- (YYLabel *)commentLabel {
if (!_commentLabel) {
_commentLabel = [[YYLabel alloc] init];
_commentLabel.font = [UIFont systemFontOfSize:15];
_commentLabel.numberOfLines = 0;
_commentLabel.textColor = [ThemeColor secondTextColor];
_commentLabel.preferredMaxLayoutWidth = KMONENTS_COMMENT_MAX_WIDTH;
}
return _commentLabel;
}
- (UILabel *)commentDateLabel {
if (!_commentDateLabel) {
_commentDateLabel = [[UILabel alloc] init];
_commentDateLabel.font = [UIFont systemFontOfSize:12];
_commentDateLabel.textColor = [ThemeColor textThirdColor];
}
return _commentDateLabel;
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
_tableView.scrollEnabled = NO;
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[_tableView registerClass:[XPMonentsReplyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMonentsReplyTableViewCell class])];
[_tableView registerClass:[XPMonentsReplyMoreTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPMonentsReplyMoreTableViewCell class])];
}
return _tableView;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [ThemeColor dividerColor];
}
return _lineView;
}
- (UIView *)lineContainerView {
if (!_lineContainerView) {
_lineContainerView = [[UIView alloc] init];
_lineContainerView.backgroundColor = [UIColor clearColor];
}
return _lineContainerView;
}
- (UIImageView *)ownerImageView {
if (!_ownerImageView) {
_ownerImageView = [[UIImageView alloc] init];
_ownerImageView.userInteractionEnabled = YES;
_ownerImageView.image = [UIImage imageNamed:@"monents_common_landLordFlag"];
_ownerImageView.hidden = YES;
}
return _ownerImageView;
}
@end