Files
yinmeng-ios/xplan-ios/Main/Message/View/SessionList/SessionListCell.m
2023-08-31 17:50:20 +08:00

212 lines
6.9 KiB
Objective-C

//
// SessionListCell.m
// xplan-ios
//
// Created by zu on 2021/11/26.
//
#import "SessionListCell.h"
#import "NIMBadgeView.h"
#import "NetImageView.h"
#import "NIMMessageUtils.h"
#import "NIMTimeUtils.h"
#import "UIView+NIM.h"
#import "ClientConfig.h"
#import "ThemeColor.h"
#import <Masonry/Masonry.h>
@interface SessionListCell()
@property (nonatomic,strong) NetImageView *avatarImageView;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UIImageView *sexImageView;
@property (nonatomic,strong) UILabel *messageLabel;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) NIMBadgeView *badgeView;
@property (nonatomic,strong) UIView *divider;
@end
@implementation SessionListCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = UIColor.clearColor;
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self initView];
[self initLayout];
}
return self;
}
- (void)renderWithSession:(NIMRecentSession *)recent {
_session = recent;
NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:recent.session.sessionId];
NSString *avatarUrl = user.userInfo.avatarUrl;
self.avatarImageView.imageUrl = avatarUrl;
self.nameLabel.text = user.userInfo.nickName;
[self.nameLabel sizeToFit];
self.messageLabel.text = [NIMMessageUtils messageContent:recent.lastMessage];
[self.messageLabel sizeToFit];
if (recent.lastMessage) {
self.timeLabel.text = [NIMTimeUtils showTime:recent.lastMessage.timestamp showDetail:NO];
} else {
NSTimeInterval timeSecond = recent.updateTime / 1000.0;
self.timeLabel.text = [NIMTimeUtils showTime:timeSecond showDetail:NO];
}
[self.timeLabel sizeToFit];
if (recent.unreadCount) {
self.badgeView.hidden = NO;
self.badgeView.badgeValue = @(recent.unreadCount).stringValue;
} else {
self.badgeView.hidden = YES;
}
//过滤官方账号
if ([[ClientConfig shareConfig].configInfo.officialMsgUids containsObject:user.userId] || [[ClientConfig shareConfig].configInfo.officialAccountUids containsObject:user.userId]) {
self.sexImageView.image = nil;
}else{
if (user.userInfo.gender == NIMUserGenderMale) {
self.sexImageView.image = [UIImage imageNamed:@"common_male"];
} else if (user.userInfo.gender == NIMUserGenderFemale) {
self.sexImageView.image = [UIImage imageNamed:@"common_female"];
}else{
self.sexImageView.image = nil;
}
}
}
- (void)initView {
[self.contentView addSubview:self.avatarImageView];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.sexImageView];
[self.contentView addSubview:self.messageLabel];
[self.contentView addSubview:self.timeLabel];
[self.contentView addSubview:self.badgeView];
[self.contentView addSubview:self.divider];
}
- (void)initLayout {
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(20);
make.left.mas_equalTo(15);
make.height.width.mas_equalTo(54);
}];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).offset(-2);
make.left.mas_equalTo(self.avatarImageView.mas_right).offset(15);
}];
[self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.nameLabel.mas_right).offset(4);
make.centerY.mas_equalTo(self.nameLabel);
make.size.mas_equalTo(CGSizeMake(14, 14));
}];
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.nameLabel);
make.top.mas_equalTo(self.avatarImageView.mas_centerY).offset(2);
make.right.mas_equalTo(self.timeLabel.mas_left).offset(-15);
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self).offset(-15);
make.right.mas_equalTo(self).offset(-15);
}];
[self.divider mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.offset(-15);
make.left.mas_equalTo(self.nameLabel);
make.bottom.mas_equalTo(self);
make.height.mas_equalTo(0.5f);
}];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.badgeView.nim_right = self.nim_width - 15;
self.badgeView.nim_top = 26;
}
- (NetImageView *)avatarImageView {
if (!_avatarImageView) {
NetImageConfig *config = [[NetImageConfig alloc]init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
config.radius = CGFLOAT_MAX;
config.imageType = ImageTypeUserIcon;
_avatarImageView = [[NetImageView alloc] initWithConfig:config];
_avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
_avatarImageView.layer.masksToBounds = YES;
_avatarImageView.layer.cornerRadius = 54.f / 2;
}
return _avatarImageView;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.font = [UIFont systemFontOfSize:15.f weight:UIFontWeightMedium];
_nameLabel.textColor = ThemeColor.mainTextColor;
}
return _nameLabel;
}
- (UILabel *)messageLabel {
if (!_messageLabel) {
_messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_messageLabel.backgroundColor = [UIColor clearColor];
_messageLabel.font = [UIFont systemFontOfSize:13.f];
_messageLabel.textColor = ThemeColor.secondTextColor;
}
return _messageLabel;
}
- (UILabel *)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.backgroundColor = [UIColor clearColor];
_timeLabel.font = [UIFont systemFontOfSize:12.f weight:UIFontWeightLight];
_timeLabel.textColor = ThemeColor.textThirdColor;
[_timeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_timeLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return _timeLabel;
}
- (NIMBadgeView *)badgeView {
if (!_badgeView) {
_badgeView = [NIMBadgeView viewWithBadgeTip:@""];
}
return _badgeView;
}
- (UIView *)divider {
if (!_divider) {
_divider = [[UIView alloc]init];
_divider.backgroundColor = [ThemeColor dividerColor];
_divider.hidden = YES;
}
return _divider;
}
- (UIImageView *)sexImageView {
if (!_sexImageView) {
_sexImageView = [[UIImageView alloc] init];
_sexImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _sexImageView;
}
@end