CNLiveNotSeeCell.m 4.96 KB
//
//  CNLiveNotSeeCell.m
//  CNLiveSettingModule
//
//  Created by CNLive-zxw on 2019/4/22.
//  Copyright © 2019 cnlive. All rights reserved.
//

#import "CNLiveNotSeeCell.h"
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import <Masonry/Masonry.h>
#import "CNLiveCategory.h"

#import "CNLiveNotSeeModel.h"

@interface CNLiveNotSeeCell()
@property (nonatomic, strong) UIButton *delButton;
@property (nonatomic, strong) UIImageView *avatarImage;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation CNLiveNotSeeCell
static const NSInteger margin = 15;

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configSubviews];
        [self xw_layoutSubviews];

    }
    return self;
}

#pragma mark - Data
- (void)setModel:(CNLiveNotSeeModel *)model {
    _model = model;
    if(model.type == CNLiveNotSeeModelTypeAdd){
        _avatarImage.layer.masksToBounds = NO;
        _avatarImage.layer.cornerRadius = 0;
        UIImage *xw_image = [self getImageWithImageName:@"privacy_plus" bundleName:@"CNLiveSettingModule" targetClass:[self class]];
        [_avatarImage sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:xw_image];
        _nameLabel.text = @"";

    }else if (model.type == CNLiveNotSeeModelTypeMinus){
        _avatarImage.layer.masksToBounds = NO;
        _avatarImage.layer.cornerRadius = 0;
        UIImage *xw_image = [self getImageWithImageName:@"privacy_less" bundleName:@"CNLiveSettingModule" targetClass:[self class]];
        [_avatarImage sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:xw_image];
        _nameLabel.text = @"";

    }else{
        _avatarImage.layer.masksToBounds = YES;
        _avatarImage.layer.cornerRadius = 5;
        [_avatarImage sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@""]];
        _nameLabel.text = model.nickName;

    }
    if(model.type == CNLiveNotSeeModelTypeDefault && [model.isEdit isEqualToString:@"1"]){
        [_delButton setHidden:NO];
        
    }else{
        [_delButton setHidden:YES];

    }
}

- (void)configSubviews {
    [self.contentView addSubview:self.avatarImage];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.delButton];
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 1;
    [self.contentView addGestureRecognizer:longPress];
}

#pragma mark - Layout
- (void)xw_layoutSubviews {
    [self.avatarImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(margin);
        make.centerX.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(45, 45));
    }];
    [self.delButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(3);;
        make.right.mas_equalTo(self.avatarImage.mas_left).offset(12.5);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView).offset(-margin);
        make.left.and.right.mas_lessThanOrEqualTo(self.contentView);
    }];
    
}
#pragma mark - Getter
- (UIButton *)delButton{
    if (!_delButton) {
        _delButton = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *image = [self getImageWithImageName:@"privacy_edit_minus" bundleName:@"CNLiveSettingModule" targetClass:[self class]];
        [_delButton setBackgroundImage:image forState:UIControlStateNormal];
        [_delButton addTarget:self action:@selector(delButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_delButton setHidden:YES];
    }
    return _delButton;
}

- (UIImageView *)avatarImage{
    if (!_avatarImage) {
        _avatarImage = [[UIImageView alloc]init];
        _avatarImage.userInteractionEnabled = YES;
    }
    return _avatarImage;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        [_nameLabel setFont:UIFontMake(14.0)];
        [_nameLabel setTextAlignment:NSTextAlignmentCenter];
        _nameLabel.textColor = [UIColor blackColor];
    }
    return _nameLabel;
}

#pragma mark - Action Methods
- (void)delButtonDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickedDel:)]) {
        [self.delegate clickedDel:self];
    }
}

// 长按手势事件
- (void)longPress:(UILongPressGestureRecognizer *)sender{
    if(_model.type == CNLiveNotSeeModelTypeAdd||_model.type == CNLiveNotSeeModelTypeMinus){
        return;
    }
    if (sender.state == UIGestureRecognizerStateBegan) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(longPressCell:)]) {
            [self.delegate longPressCell:self];
        }
        
    }
}

@end