Files
peko-ios/YuMi/Modules/YMMessage/View/SessionList/SessionListCell.m
2024-05-23 18:16:04 +08:00

212 lines
7.1 KiB
Objective-C

//
// 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 <Masonry/Masonry.h>
@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;
@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<NIMUser *> * _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];
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 {
[self.contentView addSubview:self.avatarImageView];
[self.contentView addSubview:self.nameLabel];
[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.centerY.mas_equalTo(self.contentView);
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.timeLabel.mas_leading).offset(-15);
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self).offset(-15);
make.trailing.mas_equalTo(self).offset(-15);
}];
[self.divider mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo (-15);
make.leading.mas_equalTo(self.nameLabel);
make.bottom.mas_equalTo(self);
make.height.mas_equalTo(0.5f);
}];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.badgeView.nim_right = isMSRTL() ? 15 + self.badgeView.mj_w : self.nim_width - 15;
self.badgeView.nim_top = 20;
}
- (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.backgroundColor = [UIColor clearColor];
_nameLabel.font = [UIFont systemFontOfSize:15.f weight:UIFontWeightMedium];
_nameLabel.textColor = DJDKMIMOMColor.mainTextColor;
}
return _nameLabel;
}
- (YYLabel *)messageLabel {
if (!_messageLabel) {
_messageLabel = [[YYLabel alloc] initWithFrame:CGRectZero];
_messageLabel.backgroundColor = [UIColor clearColor];
_messageLabel.font = [UIFont systemFontOfSize:13.f];
_messageLabel.textColor = DJDKMIMOMColor.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 = DJDKMIMOMColor.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 = [DJDKMIMOMColor dividerColor];
_divider.hidden = YES;
}
return _divider;
}
@end