Files
peko-ios/YuMi/Modules/YMMessage/View/SessionList/SessionListCell.m
2025-01-07 20:07:54 +08:00

242 lines
8.4 KiB
Objective-C

//
// SessionListCell.m
// YUMI
//
// Created by zu on 2021/11/26.
//
#import "SessionListCell.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) UILabel *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){//} || [user.userInfo.nickName.lowercaseString isEqualToString:@"Platform New User".lowercaseString]) {
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];
[messageAtt addAttributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x7b7b7d)} range:NSMakeRange(0, messageAtt.length)];
self.messageLabel.attributedText = messageAtt;
if (recent.lastMessage) {
self.timeLabel.text = [NIMTimeUtils formattedTimeFromInterval:recent.lastMessage.timestamp];
} else {
NSTimeInterval timeSecond = recent.updateTime / 1000.0;
self.timeLabel.text = [NIMTimeUtils formattedTimeFromInterval:timeSecond];
}
[self.timeLabel sizeToFit];
if (recent.unreadCount) {
self.badgeView.hidden = NO;
if (recent.unreadCount > 100) {
self.badgeView.text = @" 99+ ";
} else {
self.badgeView.text = [NSString stringWithFormat:@" %@ ", @(recent.unreadCount)];
}
} 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(47);
}];
[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.badgeView.mas_leading).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);
}];
[self.badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.messageLabel);
make.trailing.mas_equalTo(self.timeLabel);
make.height.mas_equalTo(16);
make.width.mas_lessThanOrEqualTo(30);
}];
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (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 = 47.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;
}
- (UILabel *)badgeView {
if (!_badgeView) {
_badgeView = [UILabel labelInitWithText:@"" font:kFontMedium(10) textColor:[UIColor whiteColor]];
_badgeView.backgroundColor = UIColorFromRGB(0xFF4E5C) ;
_badgeView.textAlignment = NSTextAlignmentCenter;
[_badgeView setCornerRadius:8];
}
return _badgeView;
}
- (UIView *)divider {
if (!_divider) {
_divider = [[UIView alloc]init];
_divider.backgroundColor = [DJDKMIMOMColor dividerColor];
_divider.hidden = YES;
}
return _divider;
}
@end