ComDynamicCell.m 9.9 KB
//
//  ComDynamicCell.m
//  DreamSleep
//
//  Created by peter on 2022/9/27.
//

#import "ComDynamicCell.h"
#import "GKPhotoBrowser.h"
#import "LikeButton.h"

@interface ComDynamicCell ()
@property (nonatomic, assign) DynModelType modelType;
@property (nonatomic, assign) CGFloat margin;
@property (nonatomic, strong) UIImageView *userIcon;
@property (nonatomic, strong) UILabel *userNameLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) UIButton *informBtn;
@property (nonatomic, strong) UIView *imgContainerView;
@property (nonatomic, strong) UILabel *contentLab;
@property (nonatomic, strong) LikeButton *likeBtn;
@property (nonatomic, strong) UIButton *remarkCountBtn;
@property (nonatomic, strong) NSMutableArray *photos;
@end

@implementation ComDynamicCell

#pragma mark - init
- (instancetype)initWithCellType:(DynModelType)type style:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.modelType = type;
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.photos = [NSMutableArray array];
        
        [self.contentView addSubview:self.userIcon];
        [self.contentView addSubview:self.userNameLab];
        [self.contentView addSubview:self.timeLab];
        [self.contentView addSubview:self.informBtn];
        [self.contentView addSubview:self.imgContainerView];
        [self.contentView addSubview:self.contentLab];
        [self.contentView addSubview:self.likeBtn];
        [self.contentView addSubview:self.remarkCountBtn];
        
        self.margin = 15;
        if (type == DynModelTypeCom) {
            [self cornerRadius:24.0];
            self.dk_backgroundColorPicker = DKColorPickerWithColors(DSWhite, CornerViewDarkColor, DSWhite);
        } else {
            self.margin = 30;
            [self hideNeedlessView];
            self.backgroundColor = DSClearColor;
        }
        
        [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(self.margin);
            make.top.equalTo(self.contentView).offset(15);
            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.informBtn.mas_left).offset(-14);
        }];
        [self.timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.userNameLab);
            make.top.equalTo(self.contentView).offset(38);
        }];
        [self.informBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(15);
            make.right.equalTo(self.contentView).offset(-15);
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        [self.remarkCountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-15);
            make.bottom.equalTo(self.contentView).offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.remarkCountBtn.mas_left);
            make.size.bottom.equalTo(self.remarkCountBtn);
        }];
    }
    return self;
}

#pragma mark - public
- (void)fireLikeAnimate {
    [self.likeBtn executeAnimation];
}

- (void)updateLikeUI {
    [self.likeBtn updateLikeBtnState:self.model.isLike likeCount:self.model.likeCount];
}

- (void)hideNeedlessView {
    self.informBtn.hidden = YES;
    self.likeBtn.hidden = YES;
    self.remarkCountBtn.hidden = YES;
}

#pragma mark - setter
- (void)setModel:(ComDynModel *)model {
    _model = model;
    
    [self.userIcon yy_setImageWithURL:[NSURL URLWithString:model.userIcon] placeholder:[UIImage defaultPlaceholderWithSize:CGSizeMake(40, 40)]];
    self.userNameLab.text = model.userName;
    self.timeLab.text = model.time;
    self.contentLab.text = model.content;
    [self.likeBtn updateLikeBtnState:model.isLike likeCount:model.likeCount];
    [self.remarkCountBtn setTitle:[NSString stringWithFormat:@"%d", model.remarkCount] forState:UIControlStateNormal];
        
    [self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userIcon.mas_bottom).offset(12);
        make.left.equalTo(self.contentView).offset(self.margin);
        make.right.equalTo(self.contentView).offset(-self.margin);
        make.height.equalTo(@([model contentHeight:self.modelType]));
    }];
    
    // 先移除
    [self.imgContainerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self.photos removeAllObjects];
    // 再添加
    CGFloat imgContainerViewH = [model imgHeight];
    [self.imgContainerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLab.mas_bottom).offset(12);
        make.left.equalTo(self.contentView).offset(self.margin);
        make.right.equalTo(self.contentView).offset(-self.margin);
        make.height.equalTo(@(imgContainerViewH));
    }];
    if (imgContainerViewH > 0) {
        NSArray *imgUrlArr = [model getImgUrlsArr:model.imgUrls];
        CGFloat itemCount = imgUrlArr.count;
        CGFloat itemSpace = itemCount > 1 ? (kScreenWidth - 60 - imgContainerViewH * itemCount) / (itemCount - 1) : 0;
        for (int index = 0; index < itemCount; index++) {
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(index*(imgContainerViewH + itemSpace), 0, imgContainerViewH, imgContainerViewH)];
            [imgView cornerRadius:12];
            imgView.tag = index;
            imgView.userInteractionEnabled = YES;
            [imgView yy_setImageWithURL:[NSURL URLWithString:imgUrlArr[index]] placeholder:[UIImage defaultPlaceholderWithSize:imgView.size]];
            imgView.dk_alphaPicker = DKAlphaPickerWithAlphas(1.f, 0.5f, 0.1f);
            [self.imgContainerView addSubview:imgView];
            
            UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lookImg:)];
            [imgView addGestureRecognizer:tapGR];
            
            GKPhoto *photo = [GKPhoto new];
            photo.url = [NSURL URLWithString:imgUrlArr[index]];
            photo.sourceImageView = imgView;
            [self.photos addObject:photo];
        }
    }
}

#pragma mark - private
- (void)lookImg:(UITapGestureRecognizer *)tapGR {
    UIImageView *showIV = (UIImageView *)tapGR.view;
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:[_photos copy] currentIndex:showIV.tag];
    browser.showStyle = GKPhotoBrowserShowStyleZoom;
    browser.loadStyle = GKPhotoBrowserLoadStyleDeterminate;
    [browser showFromVC:self.ds_viewController];
}

#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;
}

- (UIButton *)informBtn {
    if (!_informBtn) {
        _informBtn = [UIButton btnWithTitle:@"•••" font:BoldFont(15)];
        _informBtn.tag = 1;
        [_informBtn dk_setTitleColorPicker:DKColorPickerWithColors(MainTextColor, ColorFromHex(0xE8E9E9), DSWhite) forState:UIControlStateNormal];
        [_informBtn addTarget:self action:@selector(informOrlikeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _informBtn;
}

- (UIView *)imgContainerView {
    if (!_imgContainerView) {
        _imgContainerView = [UIView new];
    }
    return _imgContainerView;
}

- (UILabel *)contentLab {
    if (!_contentLab) {
        _contentLab = [UILabel labWithFont:SysFont(14)];
        _contentLab.dk_textColorPicker = DKColorPickerWithColors(MainTextColor, ColorFromHexA(0xFFFFFF, .5), DSWhite);
        _contentLab.numberOfLines = 0;
    }
    return _contentLab;
}

- (void)informOrlikeOrRemarkAction:(UIButton *)sender {
    if (sender.tag == 1) {
        if (self.tapInformBlock) { self.tapInformBlock(); }
    } else if (sender.tag == 2) {
        if (self.tapLikeBlock) { self.tapLikeBlock(); }
    } else {
        if (self.tapRemarkBlock) { self.tapRemarkBlock(); }
    }
}

- (LikeButton *)likeBtn {
    if (!_likeBtn) {
        _likeBtn = [[LikeButton alloc] initWithNormalImg:nil nightNormalImg:nil selectedImg:nil nightSelectedImg:nil];
        _likeBtn.tag = 2;
        [_likeBtn addTarget:self action:@selector(informOrlikeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeBtn;
}

- (UIButton *)remarkCountBtn {
    if (!_remarkCountBtn) {
        _remarkCountBtn = [UIButton btnWithTitle:@"0" font:SysFont(12)];
        _remarkCountBtn.tag = 3;
        [_remarkCountBtn dk_setTitleColorPicker:DKColorPickerWithColors(SmallTextColor, ColorFromHexA(0xFFFFFF, .3), DSWhite) forState:UIControlStateNormal];
        [_remarkCountBtn dk_setImage:DKImagePickerWithNames(@"ic_shequ_pinlun", @"dk_ic_shequ_pinlun", @"ic_shequ_pinlun") forState:UIControlStateNormal];
        _remarkCountBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 2, 0, 0);
        [_remarkCountBtn addTarget:self action:@selector(informOrlikeOrRemarkAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _remarkCountBtn;
}

@end