Files
peko-ios/YuMi/Modules/YMMessage/View/SessionList/SessionListCell.m

242 lines
8.4 KiB
Mathematica
Raw Normal View History

2023-07-14 18:50:55 +08:00
//
// 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"
2023-09-26 17:57:39 +08:00
#import "QEmotionHelper.h"
2023-07-14 18:50:55 +08:00
#import "DJDKMIMOMColor.h"
2023-10-28 04:46:10 +08:00
2023-07-14 18:50:55 +08:00
#import <Masonry/Masonry.h>
@interface SessionListCell()
@property (nonatomic,strong) NetImageView *avatarImageView;
@property (nonatomic,strong) UILabel *nameLabel;
2023-09-26 17:57:39 +08:00
@property (nonatomic,strong) YYLabel*messageLabel;
2023-07-14 18:50:55 +08:00
@property (nonatomic,strong) UILabel *timeLabel;
2025-01-07 20:07:54 +08:00
@property(nonatomic, strong) UILabel *badgeView;
2023-07-14 18:50:55 +08:00
@property (nonatomic,strong) UIView *divider;
@property (nonatomic, strong) UIView *container;
2023-07-14 18:50:55 +08:00
@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];
2024-05-23 18:08:07 +08:00
self.avatarImageView.image = nil;
self.nameLabel.text = @"";
2024-12-20 19:05:43 +08:00
if (user.userInfo == nil){//} || [user.userInfo.nickName.lowercaseString isEqualToString:@"Platform New User".lowercaseString]) {
2023-07-14 18:50:55 +08:00
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;
2024-05-23 11:25:18 +08:00
[self.avatarImageView loadImageWithUrl:avatarUrl completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) {
self.avatarImageView.image = image;
}];
2023-07-14 18:50:55 +08:00
[self.nameLabel sizeToFit];
}
}];
}
} else {
NSString *avatarUrl = user.userInfo.avatarUrl;
2024-12-20 19:05:43 +08:00
self.nameLabel.text = user.userInfo.nickName;
2024-05-23 11:25:18 +08:00
[self.avatarImageView loadImageWithUrl:avatarUrl completion:^(UIImage * _Nullable image, NSURL * _Nonnull url) {
self.avatarImageView.image = image;
}];
2023-07-14 18:50:55 +08:00
[self.nameLabel sizeToFit];
}
2023-09-26 17:57:39 +08:00
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)];
2023-09-26 17:57:39 +08:00
self.messageLabel.attributedText = messageAtt;
2023-07-14 18:50:55 +08:00
if (recent.lastMessage) {
2024-12-20 19:05:43 +08:00
self.timeLabel.text = [NIMTimeUtils formattedTimeFromInterval:recent.lastMessage.timestamp];
2023-07-14 18:50:55 +08:00
} else {
NSTimeInterval timeSecond = recent.updateTime / 1000.0;
2024-12-20 19:05:43 +08:00
self.timeLabel.text = [NIMTimeUtils formattedTimeFromInterval:timeSecond];
2023-07-14 18:50:55 +08:00
}
[self.timeLabel sizeToFit];
if (recent.unreadCount) {
self.badgeView.hidden = NO;
2025-01-07 20:07:54 +08:00
if (recent.unreadCount > 100) {
self.badgeView.text = @" 99+ ";
} else {
self.badgeView.text = [NSString stringWithFormat:@" %@ ", @(recent.unreadCount)];
}
2023-07-14 18:50:55 +08:00
} else {
self.badgeView.hidden = YES;
}
2025-01-07 20:07:54 +08:00
2023-07-14 18:50:55 +08:00
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;
2023-07-14 18:50:55 +08:00
}
- (void)initLayout {
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.container);
2024-04-11 17:05:27 +08:00
make.leading.mas_equalTo(15);
2025-01-07 20:07:54 +08:00
make.height.width.mas_equalTo(47);
2023-07-14 18:50:55 +08:00
}];
[self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).offset(-2);
2024-04-11 17:05:27 +08:00
make.leading.mas_equalTo(self.avatarImageView.mas_trailing).offset(15);
2023-07-14 18:50:55 +08:00
}];
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
2024-04-11 17:05:27 +08:00
make.leading.mas_equalTo(self.nameLabel);
2023-07-14 18:50:55 +08:00
make.top.mas_equalTo(self.avatarImageView.mas_centerY).offset(2);
2025-01-07 20:07:54 +08:00
make.trailing.mas_equalTo(self.badgeView.mas_leading).offset(-15);
2023-07-14 18:50:55 +08:00
}];
[self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.container).offset(21);
make.trailing.mas_equalTo(self.container).offset(-15);
2023-07-14 18:50:55 +08:00
}];
[self.divider mas_makeConstraints:^(MASConstraintMaker *make) {
2024-04-11 17:05:27 +08:00
make.trailing.mas_equalTo (-15);
make.leading.mas_equalTo(self.nameLabel);
make.bottom.mas_equalTo(self.container);
2023-07-14 18:50:55 +08:00
make.height.mas_equalTo(0.5f);
}];
2025-01-07 20:07:54 +08:00
[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);
}];
2023-07-14 18:50:55 +08:00
}
- (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;
2025-01-07 20:07:54 +08:00
_avatarImageView.layer.cornerRadius = 47.f / 2;
2023-07-14 18:50:55 +08:00
}
return _avatarImageView;
}
- (UILabel *)nameLabel {
if (!_nameLabel) {
_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_nameLabel.font = kFontSemibold(14);
_nameLabel.textColor = UIColorFromRGB(0x333333);
2023-07-14 18:50:55 +08:00
}
return _nameLabel;
}
2023-09-26 17:57:39 +08:00
- (YYLabel *)messageLabel {
2023-07-14 18:50:55 +08:00
if (!_messageLabel) {
2023-09-26 17:57:39 +08:00
_messageLabel = [[YYLabel alloc] initWithFrame:CGRectZero];
_messageLabel.font = kFontRegular(13);
_messageLabel.textColor = UIColorFromRGB(0x7b7b7d);
2023-07-14 18:50:55 +08:00
}
return _messageLabel;
}
- (UILabel *)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.backgroundColor = [UIColor clearColor];
_timeLabel.font = kFontRegular(12);
_timeLabel.textColor = UIColorFromRGB(0x999999);
2023-07-14 18:50:55 +08:00
[_timeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[_timeLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}
return _timeLabel;
}
2025-01-07 20:07:54 +08:00
- (UILabel *)badgeView {
2023-07-14 18:50:55 +08:00
if (!_badgeView) {
2025-01-07 20:07:54 +08:00
_badgeView = [UILabel labelInitWithText:@"" font:kFontMedium(10) textColor:[UIColor whiteColor]];
_badgeView.backgroundColor = UIColorFromRGB(0xFF4E5C) ;
_badgeView.textAlignment = NSTextAlignmentCenter;
[_badgeView setCornerRadius:8];
2023-07-14 18:50:55 +08:00
}
return _badgeView;
}
- (UIView *)divider {
if (!_divider) {
_divider = [[UIView alloc]init];
_divider.backgroundColor = [DJDKMIMOMColor dividerColor];
_divider.hidden = YES;
}
return _divider;
}
@end