CNWitnessSearchVideoCell.m 7.73 KB
//
//  CNWitnessSearchVideoCell.m
//  CNLiveVideoClassifyKit
//
//  Created by CNLive on 2022/6/17.
//

#import "CNWitnessSearchVideoCell.h"
#import "CNWitnessSearchModel.h"
#import "CNWitnessSearchPresent.h"

@interface CNWitnessSearchVideoCell ()
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UIImageView *videoImage;
@property (nonatomic, strong)UIImageView *playImage;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIImageView *logoImage;
@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)QMUIButton *likeBtn;
@end

@implementation CNWitnessSearchVideoCell
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.bgView];
        [self.bgView addSubview:self.videoImage];
        [self.videoImage addSubview:self.playImage];
        [self.bgView addSubview:self.titleLabel];
        [self.bgView addSubview:self.logoImage];
        [self.bgView addSubview:self.nameLabel];
        [self.bgView addSubview:self.likeBtn];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.equalTo(self.contentView);
        }];
        [self.videoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(242);
        }];
        [self.playImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(9);
            make.right.equalTo(self.contentView).offset(-9);
            make.width.height.mas_equalTo(21);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(10);
            make.right.equalTo(self.contentView).offset(-10);
            make.top.equalTo(self.videoImage.mas_bottom).offset(9);
        }];
        [self.logoImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-10);
            make.left.equalTo(self.contentView).offset(10);
            make.width.height.mas_equalTo(21);
        }];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.logoImage);
            make.left.equalTo(self.logoImage.mas_right).offset(5);
            make.right.equalTo(self.likeBtn.mas_left).offset(-5);
        }];
        [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-10);
            make.centerY.equalTo(self.logoImage);
        }];
    }
    return self;
}
#pragma mark - Action
- (void)likeBtnClick:(QMUIButton *)sender{
    if (![CNUserShareManager isLogin]) {
        #pragma mark -- TODO grp
     [CNLiveClassifyJumpBridge  showLoginViewWithString:@""];
        return;
    }
    [QMUITips showLoadingInView:self.contentView];
    weakself
    [CNWitnessSearchPresent requestWitnessLikeMomentWithPid:self.model.pid ContentSid:self.model.icon.id support:sender.selected?@"0":@"1" contentTag:self.model.tagName responseBlock:^(BOOL succeed) {
        
        [QMUITips hideAllTips];
        if (succeed) {
            if (sender.selected) {
                sender.selected = NO;
                weakSelf.model.tool.likesNum --;
            }else{
                sender.selected = YES;
                weakSelf.model.tool.likesNum ++;
            }
            weakSelf.model.tool.liked = sender.selected;
            [weakSelf.likeBtn setTitle:[NSString stringWithFormat:@"%ld",weakSelf.model.tool.likesNum] forState:UIControlStateNormal];
            [weakSelf.likeBtn setTitle:[NSString stringWithFormat:@"%ld",weakSelf.model.tool.likesNum] forState:UIControlStateSelected];
            [weakSelf notificationH5Update];
        }
    }];
}
- (void)notificationH5Update{
    NSDictionary *dic = @{
                          @"contentId":self.model.contentId?self.model.contentId:@"",
                          @"selected":self.model.tool.liked?@"true":@"false",
                          @"model":self.model.model?self.model.model:@""
                          };
    [CNLiveClassifyJumpBridge requestWittnessLikeNotification:dic];
}
#pragma mark - Setting
- (void)setModel:(CNWitnessSearchDetailModel *)model{
    _model = model;
    if (model.isHorizontalScreen == 1) {
        [self.videoImage mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(121);
        }];
    }else{
        [self.videoImage mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(242);
        }];
    }
    [self.videoImage sd_setImageWithURL:[NSURL URLWithString:model.poster] placeholderImage:[UIImage loadBundleImage:@"moren"]];
    self.titleLabel.text = model.title;
    [self.logoImage sd_setImageWithURL:[NSURL URLWithString:model.icon.face] placeholderImage:[UIImage loadBundleImage:@"default _icon_bg"]];
    self.nameLabel.text = model.icon.name;
    [self.likeBtn setTitle:[NSString stringWithFormat:@"%ld",model.tool.likesNum] forState:UIControlStateNormal];
    [self.likeBtn setTitle:[NSString stringWithFormat:@"%ld",model.tool.likesNum] forState:UIControlStateSelected];
    self.likeBtn.selected = _model.tool.liked;
    [self.likeBtn sizeToFit];
}
#pragma mark - Getting
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = UIColorWhite;
        _bgView.layer.cornerRadius = 8;
        _bgView.clipsToBounds = YES;
    }
    return _bgView;
}
- (UIImageView *)videoImage{
    if (!_videoImage) {
        _videoImage = [[UIImageView alloc] init];
        _videoImage.layer.cornerRadius = 8;
        _videoImage.clipsToBounds = YES;
        _videoImage.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _videoImage;
}
- (UIImageView *)playImage {
    if (!_playImage) {
        _playImage = [[UIImageView alloc] init];
        [_playImage setImage:[UIImage loadBundleImage:@"witness_search_play"]];
    }
    return _playImage;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = UIColorHex(#333333);
        _titleLabel.font = UIFontMake(15);
        _titleLabel.numberOfLines = 2;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}
- (UIImageView *)logoImage
{
    if (!_logoImage) {
        _logoImage = [[UIImageView alloc] init];
        _logoImage.contentMode = UIViewContentModeScaleAspectFill;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 20, 20) cornerRadius:10];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = path.CGPath;
        _logoImage.layer.mask = maskLayer;
    }
    return _logoImage;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = UIColorHex(#666666);
        _nameLabel.font = UIFontMake(11);
        _nameLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _nameLabel;
}
- (QMUIButton *)likeBtn
{
    if (!_likeBtn) {
        _likeBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [_likeBtn setImage:[UIImage loadBundleImage:@"witness_search_like"] forState:UIControlStateSelected];
        [_likeBtn setImage:[UIImage loadBundleImage:@"witness_search_unLike"] forState:UIControlStateNormal];
        _likeBtn.titleLabel.font = UIFontMake(13);
        [_likeBtn setTitleColor:UIColorHex(#666666) forState:UIControlStateNormal];
        [_likeBtn setTitleColor:UIColorHex(#666666) forState:UIControlStateSelected];
        _likeBtn.spacingBetweenImageAndTitle = 5;
        [_likeBtn addTarget:self action:@selector(likeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeBtn;
}
@end