Files
yinmeng-ios/xplan-ios/Main/Mine/View/Cell/Friend/XPMineFansTableViewCell.m
2022-03-10 18:55:18 +08:00

280 lines
9.6 KiB
Objective-C

//
// XPMineFansTableViewCell.m
// xplan-ios
//
// Created by 冯硕 on 2021/12/21.
//
#import "XPMineFansTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
#import <NIMSDK/NIMSDK.h>
///Tool
#import "NetImageView.h"
#import "ThemeColor.h"
#import "UIImage+Utils.h"
#import "UIColor+Extension.h"
///Model
#import "FansInfoModel.h"
@interface XPMineFansTableViewCell ()
///头像
@property (nonatomic,strong) NetImageView *avatarImageView;
///容器
@property (nonatomic,strong) UIStackView *stackView;
///昵称
@property (nonatomic,strong) UILabel *nickLabel;
///性别
@property (nonatomic,strong) UIImageView *sexImageView;
///等级
@property (nonatomic,strong) NetImageView *experImageView;
///魅力等级
@property (nonatomic,strong) NetImageView *charmImageView;
///贵族
@property (nonatomic,strong) NetImageView *nobleImageView;
///签名
@property (nonatomic,strong) UILabel *signLabel;
///关注
@property (nonatomic,strong) UIButton *attentionButton;
///分割线
@property (nonatomic,strong) UIView * lineView;
@end
@implementation XPMineFansTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [ThemeColor appCellBackgroundColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.avatarImageView];
[self.contentView addSubview:self.stackView];
[self.contentView addSubview:self.signLabel];
[self.contentView addSubview:self.attentionButton];
[self.contentView addSubview:self.lineView];
[self.stackView addArrangedSubview:self.nobleImageView];
[self.stackView addArrangedSubview:self.nickLabel];
[self.stackView addArrangedSubview:self.sexImageView];
[self.stackView addArrangedSubview:self.experImageView];
[self.stackView addArrangedSubview:self.charmImageView];
}
- (void)initSubViewConstraints {
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(55, 55));
make.centerY.mas_equalTo(self.contentView);
make.left.mas_equalTo(self.contentView).offset(15);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.avatarImageView.mas_right).offset(10);
make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).offset(-2);
}];
[self.signLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.stackView);
make.top.mas_equalTo(self.avatarImageView.mas_centerY).offset(2);
}];
[self.attentionButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(65);
make.height.mas_equalTo(30);
make.centerY.mas_equalTo(self.contentView);
make.right.mas_equalTo(self.contentView).offset(-15);
}];
[self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.stackView);
make.bottom.mas_equalTo(self.contentView);
make.height.mas_equalTo(1);
make.right.mas_equalTo(self.contentView).offset(-15);
}];
}
#pragma mark - Event Response
- (void)attentionButtonAction:(UIButton *)sender {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMineFansTableViewCell:didClickAttention:)]) {
[self.delegate xPMineFansTableViewCell:self didClickAttention:self.fansInfo.uid];
}
}
#pragma mark - Getters And Setters
- (void)setFansInfo:(FansInfoModel *)fansInfo {
_fansInfo = fansInfo;
if (_fansInfo) {
self.avatarImageView.imageUrl = _fansInfo.avatar;
self.experImageView.imageUrl = _fansInfo.experUrl;
self.charmImageView.imageUrl = _fansInfo.charmUrl;
self.nobleImageView.imageUrl = _fansInfo.userVipInfoVO.vipIcon;
self.nickLabel.text = _fansInfo.nick;
NSString *sexStr;
if (_fansInfo.gender == GenderType_Male) {
sexStr = @"common_male";
} else {
sexStr = @"common_female";
}
self.sexImageView.image = [UIImage imageNamed:sexStr];
self.signLabel.text = _fansInfo.userDesc && _fansInfo.userDesc.length > 0? _fansInfo.userDesc : @"这个人很懒还没有签名";
BOOL isMyFriend = [[NIMSDK sharedSDK].userManager isMyFriend:_fansInfo.uid];
self.attentionButton.enabled = !isMyFriend;
UIImage* image = self.experImageView.image;
if (image) {
CGFloat scale = image.size.width / image.size.height;
[self.experImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20* scale, 20));
}];
} else {
NSURL *imgUrl = [NSURL URLWithString:_fansInfo.experUrl];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
CGFloat scale = myImage.size.width / myImage.size.height;
[self.experImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20* scale, 20));
}];
}
UIImage* charmImage = self.charmImageView.image;
if (charmImage) {
CGFloat scale = charmImage.size.width / charmImage.size.height;
[self.charmImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20* scale, 20));
}];
} else {
NSURL *imgUrl = [NSURL URLWithString:_fansInfo.charmUrl];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
CGFloat scale = myImage.size.width / myImage.size.height;
[self.charmImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20* scale, 20));
}];
}
UIImage *nobleImage = self.nobleImageView.image;
if (nobleImage) {
CGFloat scale = nobleImage.size.width / nobleImage.size.height;
[self.nobleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(20* scale, 20));
}];
} else {
[self.nobleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(0, 20));
}];
}
if (fansInfo.userVipInfoVO && fansInfo.userVipInfoVO.friendNickColour) {
self.nickLabel.textColor = [UIColor colorWithHexString:fansInfo.userVipInfoVO.friendNickColour];
} else {
self.nickLabel.textColor = [ThemeColor mainTextColor];
}
}
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.imageType = ImageTypeUserIcon;
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.layer.cornerRadius = 55/2;
}
return _avatarImageView;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 2;
}
return _stackView;
}
- (UILabel *)nickLabel {
if (!_nickLabel) {
_nickLabel = [[UILabel alloc] init];
_nickLabel.font = [UIFont systemFontOfSize:15];
_nickLabel.textColor = [ThemeColor mainTextColor];
}
return _nickLabel;
}
- (UIImageView *)sexImageView {
if (!_sexImageView) {
_sexImageView = [[UIImageView alloc] init];
_sexImageView.userInteractionEnabled = YES;
}
return _sexImageView;
}
- (NetImageView *)experImageView {
if (!_experImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_experImageView = [[NetImageView alloc] initWithConfig:config];
}
return _experImageView;
}
- (NetImageView *)charmImageView {
if (!_charmImageView) {
NetImageConfig * config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
_charmImageView = [[NetImageView alloc] initWithConfig:config];
}
return _charmImageView;
}
- (NetImageView *)nobleImageView {
if (!_nobleImageView) {
_nobleImageView = [[NetImageView alloc] init];
}
return _nobleImageView;
}
- (UILabel *)signLabel{
if (!_signLabel) {
_signLabel = [[UILabel alloc] init];
_signLabel.textColor = [ThemeColor secondTextColor];
_signLabel.font = [UIFont systemFontOfSize:12];
}
return _signLabel;
}
- (UIButton *)attentionButton {
if (!_attentionButton) {
_attentionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_attentionButton setTitle:@"+关注" forState:UIControlStateNormal];
[_attentionButton setTitleColor:[ThemeColor confirmButtonTextColor] forState:UIControlStateNormal];
_attentionButton.titleLabel.font = [UIFont systemFontOfSize:12];
[_attentionButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
[_attentionButton setTitle:@"互相关注" forState:UIControlStateDisabled];
[_attentionButton setTitleColor:[ThemeColor disableButtonTextColor] forState:UIControlStateNormal];
[_attentionButton setBackgroundImage:[UIImage imageWithColor:[ThemeColor disableButtonColor]] forState:UIControlStateDisabled];
_attentionButton.layer.masksToBounds = YES;
_attentionButton.layer.cornerRadius = 15;
[_attentionButton addTarget:self action:@selector(attentionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _attentionButton;
}
- (UIView *)lineView {
if (!_lineView) {
_lineView = [[UIView alloc] init];
_lineView.backgroundColor = [ThemeColor dividerColor];
}
return _lineView;
}
@end