Files
yinmeng-ios/xplan-ios/Main/Message/View/Session/MessageCell.m

319 lines
12 KiB
Mathematica
Raw Normal View History

2021-11-29 01:54:12 +08:00
//
// MessageCell.m
// xplan-ios
//
// Created by zu on 2021/11/28.
//
#import "MessageCell.h"
#import "MessageContentProtocol.h"
#import "MessageContentText.h"
#import "MessageContentImage.h"
#import "MessageContentTextClickable.h"
2022-04-15 20:57:49 +08:00
#import "MessageContentCustomView.h"
#import "MessageContentGiftView.h"
2022-04-18 22:36:16 +08:00
#import "MessageContentGuildView.h"
#import "MessageContentUnSupportView.h"
#import "MessageContentOpenLiveView.h"
#import "MessageContentApplicationShareView.h"
2022-04-20 11:32:22 +08:00
#import "MessageContentLevelUpgradeView.h"
2022-04-22 14:31:30 +08:00
#import "MessageConentAudioView.h"
2022-04-15 20:57:49 +08:00
#import "AttachmentModel.h"
2021-11-29 01:54:12 +08:00
#import "NetImageView.h"
#import "ThemeColor.h"
#import "XPMacro.h"
#import <Masonry/Masonry.h>
@interface MessageCell()<MessageContentCustomViewDelegate>
2021-11-29 01:54:12 +08:00
@property (nonatomic, assign) NIMMessageType messageType;
2021-11-29 01:54:12 +08:00
/**
*/
@property (nonatomic, strong) NetImageView * leftAvatar;
/**
*/
@property (nonatomic, strong) NetImageView * rightAvatar;
/**
*/
@property (nonatomic, strong) UIView * messageBackground;
@property (nonatomic, strong) MASConstraint * messageBackgroundLeft;
@property (nonatomic, strong) MASConstraint * messageBackgroundRight;
/**
2021-11-29 01:54:12 +08:00
*/
@property (nonatomic, strong) UIView<MessageContentProtocol> * messageContent;
2021-11-29 01:54:12 +08:00
@end
@implementation MessageCell
+ (CGFloat)measureHeight:(NIMMessage *)message {
2022-04-20 11:32:22 +08:00
CGFloat minHeight = AVATAR_SIZE + AVATAR_MARGIN_H * 2;
if (!message) {
return minHeight;
}
CGFloat mesuredHeight = 0;
switch (message.messageType) {
case NIMMessageTypeText:
case NIMMessageTypeTip:
mesuredHeight = [MessageContentText measureHeight:message];
break;
case NIMMessageTypeImage:
mesuredHeight = [MessageContentImage measureHeight:message];
break;
2022-04-22 14:31:30 +08:00
case NIMMessageTypeAudio:
mesuredHeight = [MessageConentAudioView measureHeight:message];
break;
2022-04-20 11:32:22 +08:00
case NIMMessageTypeCustom:
mesuredHeight = [self customMessageMeasureHeight:message];
break;
default:
break;
}
return MAX(minHeight, mesuredHeight);
2021-11-29 01:54:12 +08:00
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
2022-04-20 11:32:22 +08:00
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self initViews];
[self initLayout];
}
return self;
2021-11-29 01:54:12 +08:00
}
- (void)initViews {
2022-04-20 11:32:22 +08:00
self.backgroundColor = UIColor.clearColor;
[self.contentView addSubview:self.leftAvatar];
[self.contentView addSubview:self.rightAvatar];
[self.contentView addSubview:self.messageBackground];
2021-11-29 01:54:12 +08:00
}
- (void)initLayout {
2022-04-20 11:32:22 +08:00
[self.leftAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.mas_equalTo(self).offset(15);
make.width.height.mas_equalTo(45);
}];
[self.rightAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self).offset(15);
make.right.mas_equalTo(self).offset(-15);
make.width.height.mas_equalTo(45);
}];
[self.messageBackground mas_makeConstraints:^(MASConstraintMaker *make) {
self.messageBackgroundLeft = make.left.mas_equalTo(self.leftAvatar.mas_right).offset(15);
self.messageBackgroundRight = make.right.mas_equalTo(self.rightAvatar.mas_left).offset(-15);
make.top.mas_equalTo(self).offset(20);
}];
2021-11-29 01:54:12 +08:00
}
- (void)renderWithMessage:(NIMMessage *)message {
2022-04-20 11:32:22 +08:00
NSString * avatarUrl = [[NIMSDK sharedSDK].userManager userInfo:message.from].userInfo.avatarUrl;
avatarUrl = [avatarUrl stringByReplacingOccurrencesOfString:@"https" withString:@"http"];
BOOL isSelf = [[NIMSDK sharedSDK].loginManager.currentAccount isEqualToString:message.from];
if (isSelf) {
self.leftAvatar.hidden = YES;
self.rightAvatar.hidden = NO;
[self.messageBackgroundLeft uninstall];
[self.messageBackgroundRight install];
self.rightAvatar.imageUrl = avatarUrl;
} else {
self.leftAvatar.hidden = NO;
self.rightAvatar.hidden = YES;
[self.messageBackgroundLeft install];
[self.messageBackgroundRight uninstall];
self.leftAvatar.imageUrl = avatarUrl;
}
if (self.messageContent) {
[self.messageContent removeFromSuperview];
}
switch (message.messageType) {
case NIMMessageTypeText:
case NIMMessageTypeTip:
2022-04-22 14:31:30 +08:00
if (![self.messageContent isKindOfClass:[MessageContentText class]]) {
self.messageContent = [[MessageContentText alloc] init];
}
break;
case NIMMessageTypeImage:
2022-04-22 14:31:30 +08:00
if (![self.messageContent isKindOfClass:[MessageContentImage class]]) {
self.messageContent = [[MessageContentImage alloc] init];
}
break;
case NIMMessageTypeAudio:
if (![self.messageContent isKindOfClass:[MessageConentAudioView class]]) {
self.messageContent = [[MessageConentAudioView alloc] init];
}
break;
case NIMMessageTypeCustom:
self.messageContent = [self getCustomMessageContentView:message];
break;
default:
2022-04-22 14:31:30 +08:00
if (![self.messageContent isKindOfClass:[MessageContentUnSupportView class]]) {
self.messageContent = [[MessageContentUnSupportView alloc] init];
}
break;
}
[self.messageBackground addSubview:self.messageContent];
[self.messageContent mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.right.bottom.mas_equalTo(self.messageBackground);
}];
2022-04-20 11:32:22 +08:00
[self.messageContent render:message];
2021-11-29 01:54:12 +08:00
}
2022-04-15 20:57:49 +08:00
+ (NSString *)cellContent:(NIMMessage *)message {
if (message.messageType == NIMMessageTypeText || message.messageType == NIMMessageTypeTip) {
return @"MessageContentText";
} else if(message.messageType == NIMMessageTypeImage) {
return @"MessageContentImage";
2022-04-22 14:31:30 +08:00
}else if(message.messageType == NIMMessageTypeAudio) {
return @"MessageConentAudioView";
} else if(message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
if (attachment.first == CustomMessageType_Gift && attachment.second == Custom_Message_Sub_Gift_Send) {
return @"MessageContentGiftView";
} else if(attachment.first == CustomMessageType_Secretary) {
return @"MessageContentTextClickable";
} else if(attachment.first == CustomMessageType_Hall) {
return @"MessageContentGuildView";
} else if(attachment.first == CustomMessageType_Member_Online && attachment.second == Custom_Message_Type_Attention_Member_Online) {
return @"MessageContentOpenLiveView";
} else if(attachment.first == CustomMessageType_Application_Share && attachment.second == Custom_Message_Sub_Application_Share_Room) {
return @"MessageContentOpenLiveView";
2022-04-20 11:32:22 +08:00
} else if(attachment.first == CustomMessageType_User_UpGrade && (attachment.second == Custom_Message_Sub_User_UpGrade_Charm || attachment.second == Custom_Message_Sub_User_UpGrade_Exper)) {
return @"MessageContentLevelUpgradeView";
} else {
return @"MessageContentOpenLiveView";
}
} else {
return @"MessageContentUnSupportView";
}
}
2022-04-15 20:57:49 +08:00
+ (CGFloat)customMessageMeasureHeight:(NIMMessage *)message {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
if (attachment.first == CustomMessageType_Gift && attachment.second == Custom_Message_Sub_Gift_Send) {
return [MessageContentGiftView measureHeight:message];
2022-04-20 11:32:22 +08:00
} else if(attachment.first == CustomMessageType_Secretary && attachment.second == Custom_Message_Sub_Secretary_Router) {
return [MessageContentTextClickable measureHeight:message];
} else if(attachment.first == CustomMessageType_Hall) {
return [MessageContentGuildView measureHeight:message];
} else if(attachment.first == CustomMessageType_Member_Online && attachment.second == Custom_Message_Type_Attention_Member_Online) {
return [MessageContentOpenLiveView measureHeight:message];
} else if(attachment.first == CustomMessageType_Application_Share && attachment.second == Custom_Message_Sub_Application_Share_Room) {
return [MessageContentApplicationShareView measureHeight:message];
2022-04-20 11:32:22 +08:00
} else if(attachment.first == CustomMessageType_User_UpGrade && (attachment.second == Custom_Message_Sub_User_UpGrade_Charm || attachment.second == Custom_Message_Sub_User_UpGrade_Exper)) {
return [MessageContentLevelUpgradeView measureHeight:message];
} else {
return [MessageContentUnSupportView measureHeight:message];
2022-04-15 20:57:49 +08:00
}
}
- (UIView<MessageContentProtocol> *)getCustomMessageContentView:(NIMMessage *)message {
2022-04-15 20:57:49 +08:00
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
if (attachment.first == CustomMessageType_Gift && attachment.second == Custom_Message_Sub_Gift_Send) {
if ([self.messageContent isKindOfClass:[MessageContentGiftView class]]) {
return self.messageContent;
}
return [[MessageContentGiftView alloc] init];
2022-04-20 11:32:22 +08:00
} else if(attachment.first == CustomMessageType_Secretary && attachment.second == Custom_Message_Sub_Secretary_Router) {
if ([self.messageContent isKindOfClass:[MessageContentTextClickable class]]) {
return self.messageContent;
}
return [[MessageContentTextClickable alloc] init];
} else if(attachment.first == CustomMessageType_Hall) {
if ([self.messageContent isKindOfClass:[MessageContentTextClickable class]]) {
return self.messageContent;
}
MessageContentGuildView * hallView =[[MessageContentGuildView alloc] init];
hallView.delegate = self;
return hallView;
} else if(attachment.first == CustomMessageType_Member_Online && attachment.second == Custom_Message_Type_Attention_Member_Online) {
if ([self.messageContent isKindOfClass:[MessageContentOpenLiveView class]]) {
return self.messageContent;
}
return [[MessageContentOpenLiveView alloc] init];
} else if(attachment.first == CustomMessageType_Application_Share && attachment.second == Custom_Message_Sub_Application_Share_Room) {
if ([self.messageContent isKindOfClass:[MessageContentApplicationShareView class]]) {
return self.messageContent;
}
return [[MessageContentApplicationShareView alloc] init];
2022-04-20 11:32:22 +08:00
}else if(attachment.first == CustomMessageType_User_UpGrade && (attachment.second == Custom_Message_Sub_User_UpGrade_Charm || attachment.second == Custom_Message_Sub_User_UpGrade_Exper)) {
if ([self.messageContent isKindOfClass:[MessageContentLevelUpgradeView class]]) {
return self.messageContent;
}
return [[MessageContentLevelUpgradeView alloc] init];
} else {
if ([self.messageContent isKindOfClass:[MessageContentUnSupportView class]]) {
return self.messageContent;
}
return [[MessageContentUnSupportView alloc] init];
}
}
#pragma mark - MessageContentCustomViewDelegate
- (void)updateMessageSuccess:(NIMMessage *)message {
if (self.delegate && [self.delegate respondsToSelector:@selector(updateMessageSuccess:)]) {
[self.delegate updateMessageSuccess:message];
2022-04-15 20:57:49 +08:00
}
}
#pragma mark - Getters And Setters
2021-11-29 01:54:12 +08:00
- (NetImageView *)leftAvatar {
2022-04-20 11:32:22 +08:00
if (!_leftAvatar) {
NetImageConfig * config = [[NetImageConfig alloc] init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
config.radius = MAXFLOAT;
config.imageType = ImageTypeUserIcon;
_leftAvatar = [[NetImageView alloc] initWithConfig:config];
_leftAvatar.layer.masksToBounds = YES;
_leftAvatar.layer.cornerRadius = 45.f / 2;
_leftAvatar.hidden = YES;
}
return _leftAvatar;
2021-11-29 01:54:12 +08:00
}
- (NetImageView *)rightAvatar {
2022-04-20 11:32:22 +08:00
if (!_rightAvatar) {
NetImageConfig * config = [[NetImageConfig alloc] init];
config.placeHolder = [UIImageConstant defaultAvatarPlaceholder];
config.radius = MAXFLOAT;
config.imageType = ImageTypeUserIcon;
_rightAvatar = [[NetImageView alloc] initWithConfig:config];
_rightAvatar.layer.masksToBounds = YES;
_rightAvatar.layer.cornerRadius = 45.f / 2;
_rightAvatar.hidden = YES;
}
return _rightAvatar;
2021-11-29 01:54:12 +08:00
}
- (UIView *)messageBackground {
2022-04-20 11:32:22 +08:00
if (!_messageBackground) {
_messageBackground = [[UIView alloc]init];
_messageBackground.backgroundColor = [ThemeColor appCellBackgroundColor];
_messageBackground.layer.masksToBounds = YES;
_messageBackground.layer.cornerRadius = 8.f;
}
return _messageBackground;
2021-11-29 01:54:12 +08:00
}
@end