DynamicCommentCell.m 6.8 KB
//
//  DynamicCommentCell.m
//  DreamSleep
//
//  Created by peter on 2022/10/8.
//

#import "DynamicCommentCell.h"

@interface DynamicCommentCell ()
@property (nonatomic, strong) UIImageView *userIcon;
@property (nonatomic, strong) UILabel *userNameLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UILabel *commentContentLab;
@property (nonatomic, strong) UIView *replyView;
@property (nonatomic, strong) UILabel *replyUserLab;
@property (nonatomic, strong) UILabel *replyContentLab;
@property (nonatomic, strong) UIButton *totalReplyBtn;
@end

@implementation DynamicCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, DarkColor, DSWhite);
        
        [self.contentView addSubview:self.userIcon];
        [self.contentView addSubview:self.userNameLab];
        [self.contentView addSubview:self.timeLab];
        [self.contentView addSubview:self.commentContentLab];
        [self.contentView addSubview:self.replyView];
        
        [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(30);
            make.top.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
        [self.userNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.userIcon.mas_right).offset(14);
            make.top.equalTo(self.userIcon);
            make.right.equalTo(self.contentView).offset(-30);
        }];
        [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.userNameLab);
            make.bottom.equalTo(self.userIcon);
        }];
    }
    return self;
}

- (void)setComReplyModel:(ComReplyModel *)comReplyModel {
    _comReplyModel = comReplyModel;
    
    [self.userIcon yy_setImageWithURL:[NSURL URLWithString:comReplyModel.commentModel.user_profile] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(40, 40)]];
    self.userNameLab.text = comReplyModel.commentModel.nick_name;
    self.timeLab.text = comReplyModel.commentModel.publish_time;
    self.commentContentLab.text = comReplyModel.commentModel.content;
    
    self.replyUserLab.text = [comReplyModel replyNickName];
    self.replyContentLab.attributedText = [comReplyModel replyContentAttriStr];
    [self.totalReplyBtn setTitle:[NSString stringWithFormat:@"共%d条回复>", comReplyModel.commentModel.total_replys] forState:UIControlStateNormal];
    self.totalReplyBtn.hidden = comReplyModel.commentModel.total_replys > 1 ? NO : YES;
    
    [self.commentContentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userIcon.mas_bottom).offset(8);
        make.left.equalTo(self.contentView).offset([comReplyModel commentContentLeftMargin]);
        make.right.equalTo(self.contentView).offset(-[comReplyModel commentContentRightMargin]);
        make.height.equalTo(@([comReplyModel commentContentHeight]));
    }];
    [self.replyView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.commentContentLab.mas_bottom).offset(12);
        make.left.right.equalTo(self.commentContentLab);
        make.height.equalTo(@([comReplyModel replyViewHeight]));
    }];
    [self.replyUserLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.replyView).offset(8);
        make.left.equalTo(self.replyView).offset(8);
        make.width.equalTo(@([comReplyModel replyNickNameW]));
    }];
    [self.replyContentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.replyUserLab);
        make.left.equalTo(self.replyUserLab.mas_right);
        make.size.mas_equalTo([comReplyModel replyContentSize]);
    }];
    [self.totalReplyBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.replyUserLab);
        make.bottom.equalTo(self.replyView).offset(-8);
    }];
}

#pragma mark - private
- (void)lookAllRelpyList {
    if (self.lookAllRelpyListBlock) {
        self.lookAllRelpyListBlock();
    }
}

#pragma mark - lazy
- (UIImageView *)userIcon {
    if (!_userIcon) {
        _userIcon = [UIImageView new];
        [_userIcon cornerRadius:20];
        _userIcon.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
    }
    return _userIcon;
}

- (UILabel *)userNameLab {
    if (!_userNameLab) {
        _userNameLab = [UILabel labWithFont:BoldFont(15)];
        _userNameLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, DkTitleColor, DSWhite);
    }
    return _userNameLab;
}

- (UILabel *)timeLab {
    if (!_timeLab) {
        _timeLab = [UILabel labWithFont:SysFont(12)];
        _timeLab.dk_textColorPicker = DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite);
    }
    return _timeLab;
}

- (UILabel *)commentContentLab {
    if (!_commentContentLab) {
        _commentContentLab = [UILabel labWithFont:[ComReplyModel commentContentFont]];
        _commentContentLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
        _commentContentLab.numberOfLines = 0;
        _commentContentLab.userInteractionEnabled = YES;
    }
    return _commentContentLab;
}

- (UIView *)replyView {
    if (!_replyView) {
        _replyView = [UIView new];
        [_replyView cornerRadius:12];
        _replyView.dk_backgroundColorPicker = DKColorPickerWithColors(BGColor, CornerViewDarkColor, BGColor);
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookAllRelpyList)];
        [_replyView addGestureRecognizer:tapGR];
        
        UILabel *replyUserLab = [UILabel labWithFont:[ComReplyModel replyNickNameFont]];
        replyUserLab.dk_textColorPicker = DKColorPickerWithColors(SubTitleColor, DkTitleColor, DSWhite);
        [_replyView addSubview:replyUserLab];
        self.replyUserLab = replyUserLab;
        
        UILabel *replyContentLab = [UILabel labWithFont:[ComReplyModel replyContentFont]];
        replyContentLab.numberOfLines = 0;
        replyContentLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
        [_replyView addSubview:replyContentLab];
        self.replyContentLab = replyContentLab;
        
        UIButton *totalReplyBtn = [UIButton btnWithTitle:@"" font:SysFont(15)];
        [totalReplyBtn setTitleColor:BrandColor forState:UIControlStateNormal];
        totalReplyBtn.userInteractionEnabled = NO;
        [_replyView addSubview:totalReplyBtn];
        self.totalReplyBtn = totalReplyBtn;
    }
    return _replyView;
}

@end