// // SessionListCell.m // YUMI // // 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 "QEmotionHelper.h" #import "DJDKMIMOMColor.h" #import @interface SessionListCell() @property (nonatomic,strong) NetImageView *avatarImageView; @property (nonatomic,strong) UILabel *nameLabel; @property (nonatomic,strong) YYLabel*messageLabel; @property (nonatomic,strong) UILabel *timeLabel; @property (nonatomic,strong) NIMBadgeView *badgeView; @property (nonatomic,strong) UIView *divider; @property (nonatomic, strong) UIView *container; @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 { NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:recent.session.sessionId]; self.avatarImageView.image = nil; self.nameLabel.text = @""; if (user.userInfo == nil) { NSString * uid = recent.session.sessionId; if (uid > 0) { NSArray * uids = @[uid]; [[NIMSDK sharedSDK].userManager fetchUserInfos:uids completion:^(NSArray * _Nullable users, NSError * _Nullable error) { if (error == nil) { NIMUser * userInfo = users.firstObject; self.nameLabel.text = userInfo.userInfo.nickName; NSString *avatarUrl = userInfo.userInfo.avatarUrl; [self.avatarImageView loadImageWithUrl:avatarUrl completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) { self.avatarImageView.image = image; }]; [self.nameLabel sizeToFit]; } }]; } } else { NSString *avatarUrl = user.userInfo.avatarUrl; self.nameLabel.text = user.userInfo.nickName; [self.avatarImageView loadImageWithUrl:avatarUrl completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) { self.avatarImageView.image = image; }]; [self.nameLabel sizeToFit]; } NSString *messageTitle = [NIMMessageUtils messageContent:recent.lastMessage]; NSMutableAttributedString *messageAtt = [[QEmotionHelper sharedEmotionHelper]attributedStringByText:messageTitle font:self.messageLabel.font]; [messageAtt addAttributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x7b7b7d)} range:NSMakeRange(0, messageAtt.length)]; self.messageLabel.attributedText = messageAtt; 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; } NIMStickTopSessionInfo * topInfo = [self.stickTopMessages objectForKey:recent.session]; if (topInfo && topInfo.session.sessionId.integerValue == recent.session.sessionId.integerValue) { self.backgroundColor = [DJDKMIMOMColor colorWithHexString:@"#E5EFFC"]; } else { self.backgroundColor = [UIColor clearColor]; } } - (void)initView { UIView *bottomSpace = [[UIView alloc] init]; [self.contentView addSubview:bottomSpace]; [bottomSpace mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.bottom.mas_equalTo(self.contentView); make.height.mas_equalTo(12); }]; UIView *container = [[UIView alloc] init]; container.backgroundColor = [UIColor whiteColor]; container.layer.cornerRadius = 10; [self.contentView addSubview:container]; [container mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.contentView); make.bottom.mas_equalTo(bottomSpace.mas_top); make.leading.mas_equalTo(16); make.trailing.mas_equalTo(-16); }]; [container addSubview:self.avatarImageView]; [container addSubview:self.nameLabel]; [container addSubview:self.messageLabel]; [container addSubview:self.timeLabel]; [container addSubview:self.badgeView]; [container addSubview:self.divider]; _container = container; } - (void)initLayout { [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.container); make.leading.mas_equalTo(15); make.height.width.mas_equalTo(45); }]; [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).offset(-2); make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(15); }]; [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.mas_equalTo(self.nameLabel); make.top.mas_equalTo(self.avatarImageView.mas_centerY).offset(2); make.trailing.mas_equalTo(self.container).offset(-15); }]; [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.container).offset(21); make.trailing.mas_equalTo(self.container).offset(-15); }]; [self.divider mas_makeConstraints:^(MASConstraintMaker *make) { make.trailing.mas_equalTo (-15); make.leading.mas_equalTo(self.nameLabel); make.bottom.mas_equalTo(self.container); make.height.mas_equalTo(0.5f); }]; } - (void)layoutSubviews { [super layoutSubviews]; self.badgeView.nim_right = isMSRTL() ? 35 + self.badgeView.mj_w : self.nim_width - 35; self.badgeView.nim_top = 4; } - (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.layer.masksToBounds = YES; _avatarImageView.layer.cornerRadius = 45.f / 2; } return _avatarImageView; } - (UILabel *)nameLabel { if (!_nameLabel) { _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _nameLabel.font = kFontSemibold(14); _nameLabel.textColor = UIColorFromRGB(0x333333); } return _nameLabel; } - (YYLabel *)messageLabel { if (!_messageLabel) { _messageLabel = [[YYLabel alloc] initWithFrame:CGRectZero]; _messageLabel.font = kFontRegular(13); _messageLabel.textColor = UIColorFromRGB(0x7b7b7d); } return _messageLabel; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _timeLabel.backgroundColor = [UIColor clearColor]; _timeLabel.font = kFontRegular(12); _timeLabel.textColor = UIColorFromRGB(0x999999); [_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 = [DJDKMIMOMColor dividerColor]; _divider.hidden = YES; } return _divider; } @end