IVTMeSupportCommonCell.m 6.1 KB
//
//  IVTMeSupportCommonCell.m
//  IVTMeLive
//
//  Created by XRG on 16/5/5.
//  Copyright © 2016年 Joky. All rights reserved.
//

#import "IVTMeSupportCommonCell.h"
#import "NameSexLevelView.h"

@interface IVTMeSupportCommonCell()
{
    UIImageView *_headIcon;
    NameSexLevelView *nameSexLevelView;
    UILabel *_mark;
    UIButton *_follow;
    
    BOOL isFollowed;
}

@end
@implementation IVTMeSupportCommonCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        __unsafe_unretained typeof (self) wself = self;

        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _headIcon = [[UIImageView alloc] init];
        _headIcon.image = [UIImage imageNamed:@"占位头像"];
        [self.contentView addSubview:_headIcon];
        _headIcon.clipsToBounds = YES;
        _headIcon.layer.cornerRadius = 40/2;
        [_headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(wself.contentView.mas_leading).with.offset(12);
            make.centerY.equalTo(wself.contentView);
            make.size.mas_equalTo(CGSizeMake(39.5, 39.5));
            
        }];
        
        nameSexLevelView = [[NameSexLevelView alloc]init];
        nameSexLevelView.userNickNameColor = UIColorFromRGBAlphaHex(0x343434, 1.0);
        [self.contentView addSubview:nameSexLevelView];
        [nameSexLevelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(wself.contentView.mas_top).offset(10);
            make.left.equalTo(_headIcon.mas_right).offset(10);
            make.width.mas_equalTo(50);
            make.height.mas_equalTo(18);
        }];
        
        _mark = [[UILabel alloc]init];
        [self.contentView addSubview:_mark];
        _mark.backgroundColor = [UIColor clearColor];
        _mark.textColor = [UIColor  YXColorWithHexCode:@"#8d8d8d"];
        _mark.font = [UIFont YXFontOfSize:11];
        _mark.textAlignment = NSTextAlignmentLeft;
        [_mark mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_headIcon.mas_right).with.offset(10);
            make.right.equalTo(wself.contentView.mas_right).with.offset(-50);
            make.top.equalTo(nameSexLevelView.mas_bottom).offset(5);
            make.height.mas_equalTo(15);
        }];
        
        _follow =[UIButton buttonWithType:UIButtonTypeCustom];
        [_follow addTarget:self action:@selector(followClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_follow];
        [_follow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(27.5, 27.5));
            make.top.equalTo(wself.contentView.mas_top).with.offset(16);
            make.trailing.equalTo(wself.contentView.mas_trailing).with.offset(-17);
        }];
        
        UIImageView *lineImageView = [[UIImageView alloc]init];
        lineImageView.backgroundColor = UIColorFromRGBAlphaHex(0xdcdcdc, 1.0);
        [self.contentView addSubview:lineImageView];
        [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(wself.contentView.mas_left).offset(28);
            make.bottom.equalTo(wself.contentView.mas_bottom);
            make.width.mas_equalTo(kScreenWidth-28);
            make.height.mas_equalTo(0.4);
        }];
    }
    return self;
}

- (void)setEntity:(NSDictionary *)entity{
    _entity = entity;
    
    if (![[_entity valueForKey:@"avatar"] isEqualToString:@"http://me.demo.bjivt.com/api/avatar.png"]){
        [_headIcon sd_setImageWithURL:[NSURL  URLWithString:[_entity valueForKey:@"avatar"]]];
        
    }else{
        _headIcon.image = [UIImage imageNamed:@"占位头像"];
    }
    
    nameSexLevelView.userNickName = _entity[@"name"];
    nameSexLevelView.userSex = _entity[@"sex"];
    nameSexLevelView.userLevel = _entity[@"level"];
    
    _mark.text = [NSString stringWithFormat:@"贡献%@蜜糖",_entity[@"price"]];
    
    if ([_entity[@"id"] integerValue] == [[IVTAccountTool getUserId] integerValue]) {
        _follow.hidden = YES;
    }
    else{
        _follow.hidden = NO;
        
        isFollowed = [IVTAccountTool isFollowedToUserId:[self.entity valueForKey:@"id"]];
        if (isFollowed) {
            [_follow setBackgroundImage:[UIImage imageNamed:@"已添加关注图标"] forState:UIControlStateNormal];
        }
        else{
            [_follow setBackgroundImage:[UIImage imageNamed:@"添加关注图标"]  forState:UIControlStateNormal];
        }
    }
}

-(void)followClick:(id)sender{
    if (isFollowed == NO) {
        [[IVTNetworkAPIClient sharedClient] doFollowUser:self.entity[@"id"] success:^(NSDictionary *successData) {
            isFollowed = YES;
            //添加到已关注列表
            [IVTAccountTool insertToMyFollowListArrayWithUserDict:self.entity];
            [_follow setBackgroundImage:[UIImage imageNamed:@"已添加关注图标"] forState:UIControlStateNormal];
            [SVProgressHUD setMinimumDismissTimeInterval:2];
            [SVProgressHUD showSuccessWithStatus:@"关注成功"];
        } error:^(NSDictionary *successData) {
            
        } failure:^(NSError *failureError) {
            
        }];
    }
    else{
        [[IVTNetworkAPIClient sharedClient] doCancelFollowUser:self.entity[@"id"] success:^(NSDictionary *successData) {
            isFollowed = NO;
            //取消关注,从列表删除
            [IVTAccountTool deleteMyFollowToUserId:self.entity[@"id"]];
            [_follow setBackgroundImage:[UIImage imageNamed:@"添加关注图标"]  forState:UIControlStateNormal];
            [SVProgressHUD setMinimumDismissTimeInterval:2];
            [SVProgressHUD showSuccessWithStatus:@"取消关注"];
        } error:^(NSDictionary *successData) {
            
        } failure:^(NSError *failureError) {
            
        }];
    }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end