From c1b7d672fc9d290f5fb8a3e8dfd76867ded09614 Mon Sep 17 00:00:00 2001 From: fengshuo <963787902@qq.com> Date: Fri, 29 Oct 2021 10:50:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=B1=8F=E7=9A=84=E7=82=B9=E5=87=BB?= =?UTF-8?q?=20=E5=B7=B2=E7=BB=8F=E5=85=AC=E5=B1=8F=E7=9A=84=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E5=9B=BE=E7=89=87=E8=87=AA=E9=80=82=E5=BA=94=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common_new_user.imageset/Contents.json | 22 ++++ .../common_new_user@2x.png | Bin 0 -> 709 bytes .../common_new_user@3x.png | Bin 0 -> 1477 bytes .../common_offical.imageset/Contents.json | 22 ++++ .../common_offical@2x.png | Bin 0 -> 451 bytes .../common_offical@3x.png | Bin 0 -> 817 bytes .../Base/UI/UIImageView/UIImageConstant.h | 3 + .../UI/UIImageView/UIImageView+LoadImage.h | 10 +- .../UI/UIImageView/UIImageView+LoadImage.m | 22 ++-- xplan-ios/Global/XPMacro.h | 3 + .../MineInfo/XPMineUserInfoHeaderView.m | 2 +- .../Main/Mine/View/XPMineViewController.m | 2 +- .../Model/XPRoomMessageDisplayModel.h | 16 ++- .../Model/XPRoomMessageDisplayModel.m | 32 ++++- .../Tool/XPRoomMessageAttributeHelper.h | 8 +- .../Tool/XPRoomMessageAttributeHelper.m | 123 ++++++++++++++++-- .../View/XPRoomMessageTableViewCell.h | 4 +- .../View/XPRoomMessageTableViewCell.m | 7 + .../XPRoomMessageContainerView.h | 6 +- .../XPRoomMessageContainerView.m | 84 +++++++++++- .../Room/View/RoomHeaderView/RoomHeaderView.m | 2 +- .../Main/Room/View/StageView/StageView.m | 2 +- .../Main/Room/View/XPRoomViewController.m | 9 +- 23 files changed, 323 insertions(+), 56 deletions(-) create mode 100644 xplan-ios/Assets.xcassets/Common/common_new_user.imageset/Contents.json create mode 100644 xplan-ios/Assets.xcassets/Common/common_new_user.imageset/common_new_user@2x.png create mode 100644 xplan-ios/Assets.xcassets/Common/common_new_user.imageset/common_new_user@3x.png create mode 100644 xplan-ios/Assets.xcassets/Common/common_offical.imageset/Contents.json create mode 100644 xplan-ios/Assets.xcassets/Common/common_offical.imageset/common_offical@2x.png create mode 100644 xplan-ios/Assets.xcassets/Common/common_offical.imageset/common_offical@3x.png diff --git a/xplan-ios/Assets.xcassets/Common/common_new_user.imageset/Contents.json b/xplan-ios/Assets.xcassets/Common/common_new_user.imageset/Contents.json new file mode 100644 index 00000000..afe3844f --- /dev/null +++ b/xplan-ios/Assets.xcassets/Common/common_new_user.imageset/Contents.json @@ -0,0 +1,22 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "common_new_user@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "common_new_user@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/xplan-ios/Assets.xcassets/Common/common_new_user.imageset/common_new_user@2x.png b/xplan-ios/Assets.xcassets/Common/common_new_user.imageset/common_new_user@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..e76f047ac96a376e854d38fd3ebbfcceb2c2de12 GIT binary patch literal 709 zcmV;$0y_PPP)5n+%e zNg+g1Z6u>Y#3W2Atgx&Qzk-O~yK&$6W*B{wgO1YM%-nPDJOA@P=YP(-0-zf63oOIF z2k8x#d^qEhXjj|P%&z3 z*z>F>kWt-`Bo<=U07w15@8nBfY4hHuyD?8Y2mmgKwo1DU+Y#FU_pg z#5;`eVUjZS2r+5uWZPLJcSk}R%(?;WrK_p!16o2-k_f>ymg4r{5u^Q>4 z*;_D5j$_|H8GF&KkreDg4Ly$9b0SVKdhOK?^U7O7RT%rPU^V9@L^wNj^QF?8QK$-8 zyFKbSBY+f?;YwPY?19!fs{6lz942npYlY3EPDgEbpNgukx)Z+i$Jg`7J!y>`WJ;TnqRQ$)MyVzX#*18 zhZ_5c-C7p)=?h|1wqQS~O_hPP)3$g6o9{T-^_HQol;7XR!4Amu-RwyEnNQ^rthv4Pzi%Iymaox#wX7pj6;zQ0&4ylK~dIP zYajDJ@bebYd40OO()d0w7BFWFEc#&!eCnugiZmAWnhfSW!mjN+L%~j!hIzRqB8=`l zTpo!k>!tCP+&Z*FgFiky2uGx`C|Bc?1_BvKzez}1Hnwao{JlT!tuccBH1H?Fkpr#n zKQ*{;V2*qmGk79)*teFY?U!+tgsYWAH_$0@@R?2R+wJ@6&q zF1WO1#fjZ8=NEWuU9uqkY)H#N5`qv&0bc;>Yq9GdM=MsICS34*oSWW()U)EYkXxR8 z&PTF`;4aFCnhNib2^pBNMM%yixJxI(!L4TiE^MJecVI@$L`z@Cmc8h~WnGGM(?WFD z8tnRqj(Md+EGOrUM}|%VCxG~qATQ`_>ktyB$CRBavCB)Hc zw6?OS!))gSxG!|49cV-<&LkhB~x zA$D)TemN@&*KZQ$vb(UQZ=gRd^5&0LL8$sP;9A;}+yisn+eps~vC9h4-`6&iI#JBY zz-gEh4I^pRDoAS(k%9C8{Vf9T|2nl97wuI2R6WkFJH?b?`eR*sH}- z(&ljHmJgAxeVUw7BJqZCpuP^&J+`L%cVO4vk0hmc5M2Hn5X_CnjX02+6KVWZ|Bf~` zX-9wU#%_E9=`+!D-xu@Ivgf0F+uE(o0^|6Y`*Q=N^@YPfL?MPeh#B=jv)F2Lw?@9$ zr6=afIdEt%wyY?c5X+|{*@KV)6R^t*z1|wWk&Ky#3>=SLax;3MtbHvfy{^Gbcn#gU z4ExPZ+)b<4SZBU-^xT^|0%QUTXe|&}+JqH<(kKOPT zGzwD;M=qYn7h%c18k7GrcKcGK#}K6F zK0%-xp}rkFioF0GSuTfv;QG4U#nsa<72BU!n&#wOdrkib)sEC?WKfWz00000NkvXXu0mjf>8ZaY literal 0 HcmV?d00001 diff --git a/xplan-ios/Assets.xcassets/Common/common_offical.imageset/Contents.json b/xplan-ios/Assets.xcassets/Common/common_offical.imageset/Contents.json new file mode 100644 index 00000000..b724b1bc --- /dev/null +++ b/xplan-ios/Assets.xcassets/Common/common_offical.imageset/Contents.json @@ -0,0 +1,22 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "filename" : "common_offical@2x.png", + "idiom" : "universal", + "scale" : "2x" + }, + { + "filename" : "common_offical@3x.png", + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/xplan-ios/Assets.xcassets/Common/common_offical.imageset/common_offical@2x.png b/xplan-ios/Assets.xcassets/Common/common_offical.imageset/common_offical@2x.png new file mode 100644 index 0000000000000000000000000000000000000000..d8eb02b0ccb2568f85bc8524333701e56c972564 GIT binary patch literal 451 zcmV;!0X+VRP)||%dun?2jS(s947Lz7n zWkG6;k!cp@>drK!yf^Q@ym{p=-#zDi_nh;;dyfE+$AMmaaudNg5K`YC{9B6hSxLJrdgop@v;*TPjsS>0VR&prhsXyalxvb9np+HhQn}#vyT876h z_`KA1Q-;n!$|G_Exo1ciE`XY*p?}RhP<=bpwLv;*zMs8^ zmZ6e;eApo<7H|d%?^1+mgUY2-4g>oT?6C*lu0zL^L(tE#s`+ZA6V|ZqIk;Ye*Bwh6 zd(|nBf~4lHK}OyskY^bwN#|ZF_eu3Ab5!Nh*-J&^X3!Bs8C0Q* tb_KbCU;+s0)Lg?Ri{u=iSQSP-^$p^lxFh2r81(=E002ovPDHLkV1lL(%0>VH literal 0 HcmV?d00001 diff --git a/xplan-ios/Assets.xcassets/Common/common_offical.imageset/common_offical@3x.png b/xplan-ios/Assets.xcassets/Common/common_offical.imageset/common_offical@3x.png new file mode 100644 index 0000000000000000000000000000000000000000..bf95388312955712f19811521803fc5898cab44d GIT binary patch literal 817 zcmV-11J3-3P)Vj^I*m<&t-u)P}A9{dmI zo7T2N;C^o*b8)cg2BaPcW9H1U<}1Rs=DP2}_`u>2`)k4T&azGD3&JwWeBnO|=l%%# z_~P@@Qw3kEEZGH~InR*qcw}{GF<_5}uFLSr_zE>2fy1?xz-X6w1ivD!6gHNH3C`*p z!Vl*^7ltKs5e?UM9VU9MPit~Mq~(WcTD_AXx^+049c+#;!C5^r`SBBWLdss~z6#yf zEIriXt2O1A?`&r5SDt=LL1skp*^smgTy5ZLw`8|?I7@DWBT`c57(=Pp;J6E-7m<<3 z?J^CyszDv)n_=(^PWN!`i-|aS00000NkvXXu0mjfK #import NS_ASSUME_NONNULL_BEGIN +@class XPRoomMessageDisplayModel; +@protocol RooMessageDelegte + +///加载图片完成 一般是第一次加载的时候 +- (void)xpRoomMessageDisplayModel:(XPRoomMessageDisplayModel *)displayModel loadImageSuccess:(UIImageView *)imageView; +///点击了某个用户 +- (void)xpRoomMessageDisplayModel:(XPRoomMessageDisplayModel *)displayModel didClickUser:(NSString *)userId; +@end @interface XPRoomMessageDisplayModel : NSObject ///消息实体 @@ -16,8 +24,14 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic,assign, readonly) CGFloat cellHeight; ///所要展示的富文本 @property (nonatomic,strong, readonly) NSMutableAttributedString *contentAttributed; +///选择了某个item +@property (nonatomic,copy) void(^didSelectItem)(NSString * uid); +///加载网络图片成功 +@property (nonatomic,copy) void(^loadUrlImage)(UIImageView *imageView); -- (instancetype)initDisplayModel:(NIMMessage *)message; + + +- (instancetype)initDisplayModel:(NIMMessage *)message delefate:(id)delegate; @end diff --git a/xplan-ios/Main/Room/View/MessageContainerView/Model/XPRoomMessageDisplayModel.m b/xplan-ios/Main/Room/View/MessageContainerView/Model/XPRoomMessageDisplayModel.m index aad42703..d78f9441 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/Model/XPRoomMessageDisplayModel.m +++ b/xplan-ios/Main/Room/View/MessageContainerView/Model/XPRoomMessageDisplayModel.m @@ -12,6 +12,7 @@ #import "XPRoomMessageConstant.h" #import "AccountInfoStorage.h" #import "XPRoomMessageAttributeHelper.h" +#import "XPMacro.h" ///Model #import "XPMessageRemoteExtModel.h" @@ -22,13 +23,16 @@ @property (nonatomic,assign) CGFloat cellHeight; /// @property (nonatomic,strong) NSMutableAttributedString *contentAttributed; +/// +@property (nonatomic,assign) id delegate; @end @implementation XPRoomMessageDisplayModel -- (instancetype)initDisplayModel:(NIMMessage *)message { +- (instancetype)initDisplayModel:(NIMMessage *)message delefate:(id)delegate { if (self = [super init]) { self.message = message; + self.delegate = delegate; [self makeMessageAttribute:message]; } return self; @@ -55,14 +59,27 @@ /// 房间文本类消息 /// @param message 消息的实体 -- (void)makeTextAttribute:(NIMMessage *)message { +- (void)makeTextAttribute:(NIMMessage *)message{ NSString * uid = [AccountInfoStorage instance].getUid; XPMessageRemoteExtModel * model = [XPMessageRemoteExtModel modelWithJSON:message.remoteExt[message.from]]; NSString * nick = [NSString stringWithFormat:@"%@:", ((NIMMessageChatroomExtension *)message.messageExt).roomNickname]; if ([message.from isEqualToString:uid]) { nick = @"我:"; } - self.contentAttributed = [XPRoomMessageAttributeHelper createLevleTextAttribute:model nick:nick text:message.text]; + @kWeakify(self); + self.contentAttributed = [XPRoomMessageAttributeHelper createLevleTextAttribute:model nick:nick text:message.text complection:^(UIImageView * _Nonnull imageView) { + @kStrongify(self); + if (self.delegate && [self.delegate respondsToSelector:@selector(xpRoomMessageDisplayModel:loadImageSuccess:)]) { + [self.delegate xpRoomMessageDisplayModel:self loadImageSuccess:imageView]; + } + }]; + + [self.contentAttributed yy_setTextHighlightRange:NSMakeRange(0, self.contentAttributed.length) color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) { + @kStrongify(self); + if (self.delegate && [self.delegate respondsToSelector:@selector(xpRoomMessageDisplayModel:didClickUser:)]) { + [self.delegate xpRoomMessageDisplayModel:self didClickUser:message.from]; + } + }]; } /// 房间tips消息 @@ -79,7 +96,16 @@ NIMChatroomNotificationMember *member = content.targets[0]; switch (content.eventType) { case NIMChatroomEventTypeEnter:///进入房间 + { self.contentAttributed = [XPRoomMessageAttributeHelper createUserEnterRoomAttribute:member.nick]; + @kWeakify(self); + [self.contentAttributed yy_setTextHighlightRange:NSMakeRange(0, self.contentAttributed.length) color:nil backgroundColor:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) { + @kStrongify(self); + if (self.delegate && [self.delegate respondsToSelector:@selector(xpRoomMessageDisplayModel:didClickUser:)]) { + [self.delegate xpRoomMessageDisplayModel:self didClickUser:member.userId]; + } + }]; + } break; case NIMChatroomEventTypeInfoUpdated:///房间信息更新 [self createRoomInfoUpdateAttribute:content.notifyExt]; diff --git a/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.h b/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.h index aa012736..deb548f6 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.h +++ b/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.h @@ -8,13 +8,15 @@ #import NS_ASSUME_NONNULL_BEGIN +typedef void(^AttributeLoadImageComplection)(UIImageView *); + @class XPMessageRemoteExtModel; @interface XPRoomMessageAttributeHelper : NSObject #pragma mark - 生成某个消息类型的富文本 /// 文本类的消息的富文本 NIMMessageTypeText /// @param remoteModel 扩展字段中的内容 /// @param nick 名字 -+ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text; ++ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text complection:(nullable AttributeLoadImageComplection)complection; /// 进入房间的富文本 NIMChatroomEventTypeEnter /// @param nick 进入房间的名称 @@ -28,8 +30,8 @@ NS_ASSUME_NONNULL_BEGIN /// 生成一个图片的富文本 /// @param imageUrl 网络图片的地址 /// @param size 网络图片的大小 -/// @param success 成功 可以拿到图片的大小刷新富文本的大小 -+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size success:(void(^)(UIImage *))success; +/// @param complection 成功 可以拿到图片的大小刷新富文本的大小 ++ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size complection:(nullable AttributeLoadImageComplection)complection; /// 生成本地一个图片的富文本 /// @param imageName 网络图片的地址 diff --git a/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.m b/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.m index 02ad239f..1ee047b2 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.m +++ b/xplan-ios/Main/Room/View/MessageContainerView/Tool/XPRoomMessageAttributeHelper.m @@ -7,7 +7,8 @@ #import "XPRoomMessageAttributeHelper.h" ///Third -#import "YYText/YYText.h" +#import +#import ///Tool #import "UIImageView+LoadImage.h" ///Model @@ -19,22 +20,33 @@ /// 文本类的消息的富文本 /// @param remoteModel 扩展字段中的内容 /// @param nick 名字 -+ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text { ++ (NSMutableAttributedString *)createLevleTextAttribute:(XPMessageRemoteExtModel *)remoteModel nick:(NSString *)nick text:(NSString *)text complection:(void (^)(UIImageView *))complection { NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] init]; if (remoteModel.defUser == UserLevelType_Offical) { - NSMutableAttributedString * offcialAttribute = [self createLocalImageAttribute:@"" size:CGSizeMake(13, 13)]; + NSMutableAttributedString * offcialAttribute = [self createLocalImageAttribute:@"common_offical" size:CGSizeMake(13, 13)]; [attribute appendAttributedString:offcialAttribute]; [attribute appendAttributedString:[self createSapceAttribute:2]]; } if (remoteModel.newUser) { - NSMutableAttributedString * newUserAttribute = [self createLocalImageAttribute:@"" size:CGSizeMake(13, 13)]; + NSMutableAttributedString * newUserAttribute = [self createLocalImageAttribute:@"common_new_user" size:CGSizeMake(13, 13)]; [attribute appendAttributedString:newUserAttribute]; [attribute appendAttributedString:[self createSapceAttribute:2]]; } if (remoteModel.experUrl) { - NSMutableAttributedString * experAttribute = [self createUrlImageAttribute:remoteModel.experUrl size:CGSizeMake(34, 13)]; + UIImage * image = [self cacheImage:remoteModel.experUrl type:ImageTypeUserIcon]; + NSMutableAttributedString * experAttribute; + if (image) { + CGFloat kscale = image.size.width / image.size.height; + experAttribute = [self createImageAttribute:image size:CGSizeMake(20 * kscale, 20)]; + } else { + experAttribute = [self createUrlImageAttribute:remoteModel.experUrl size:CGSizeMake(25, 20) complection:^(UIImageView * _Nonnull imageView) { + if (complection) { + complection(imageView); + } + }]; + } [attribute appendAttributedString:experAttribute]; [attribute appendAttributedString:[self createSapceAttribute:2]]; } @@ -58,6 +70,7 @@ NSMutableAttributedString * enterRoomAttribute = [self createTextAttribute:@"进入了房间" titleColor:[ThemeColor messageDefaultTextColor] font:kRoomMessageDefalutFont]; [attribute appendAttributedString:nickAttribute]; [attribute appendAttributedString:enterRoomAttribute]; + return attribute; } @@ -68,22 +81,24 @@ /// @param imageUrl 网络图片的地址 /// @param size 网络图片的大小 + (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size { - return [self createUrlImageAttribute:imageUrl size:size success:nil]; + return [self createUrlImageAttribute:imageUrl size:size complection:nil]; } /// 生成一个图片的富文本 /// @param imageUrl 网络图片的地址 /// @param size 网络图片的大小 -/// @param success 成功 可以拿到图片的大小刷新富文本的大小 -+ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size success:(void(^)(UIImage *))success { +/// @param complection 成功 可以拿到图片的大小刷新富文本的大小 ++ (NSMutableAttributedString *)createUrlImageAttribute:(NSString *)imageUrl size:(CGSize)size complection:(nullable AttributeLoadImageComplection)complection { UIImageView *genderImageView = [[UIImageView alloc]init]; - [genderImageView load_imageWithUrl:imageUrl placeholderImage:[UIImageConstant defaultAvatarPlaceholder] type:ImageTypeUserIcon success:^(UIImage * _Nonnull image) { - if (image && success) { - success(image); + [genderImageView load_imageWithUrl:imageUrl placeholderImage:[UIImageConstant defaultAvatarPlaceholder] type:ImageTypeUserIcon complection:^(UIImage * _Nonnull image, NSURL * _Nonnull url) { + if (image && complection) { + genderImageView.image = image; + complection(genderImageView); } }]; - genderImageView.bounds = CGRectMake(0, 0, size.width, size.height); + + genderImageView.bounds = CGRectMake(0, 0, 25, 20); NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:genderImageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(genderImageView.frame.size.width, genderImageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter]; return genderString; } @@ -94,7 +109,20 @@ + (NSMutableAttributedString *)createLocalImageAttribute:(NSString *)imageName size:(CGSize)size { UIImageView *genderImageView = [[UIImageView alloc]init]; genderImageView.image = [UIImage imageNamed:imageName]; - genderImageView.bounds = CGRectMake(0, 0, size.width, size.height); + CGFloat kscale = genderImageView.image.size.width / genderImageView.image.size.height; + genderImageView.bounds = CGRectMake(0, 0, 20 * kscale, 20); + NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:genderImageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(genderImageView.frame.size.width, genderImageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter]; + return genderString; +} + +/// 生成本地一个图片的富文本 +/// @param image 本地的图片 +/// @param size 本地图片的大小 ++ (NSMutableAttributedString *)createImageAttribute:(UIImage *)image size:(CGSize)size { + UIImageView *genderImageView = [[UIImageView alloc]init]; + genderImageView.image = image; + CGFloat kscale = genderImageView.image.size.width / genderImageView.image.size.height; + genderImageView.bounds = CGRectMake(0, 0, 20 * kscale,20); NSMutableAttributedString * genderString = [NSMutableAttributedString yy_attachmentStringWithContent:genderImageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(genderImageView.frame.size.width, genderImageView.frame.size.height) alignToFont:[UIFont systemFontOfSize:15.0] alignment:YYTextVerticalAlignmentCenter]; return genderString; } @@ -133,4 +161,73 @@ return genderString; } + ++ (UIImage *)cacheImage:(NSString *)urlStr type:(ImageType)type { + NSString * key = [self getCacheImageKey:urlStr type:type]; + // 缓存的图片(内存) + UIImage *image = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:key]; + + // 缓存的图片(硬盘) + if (!image) { + image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key]; + } + return image; +} + + ++ (NSString *)getCacheImageKey:(NSString *)imageUrl type:(ImageType)type { + NSMutableString *urlString = [NSMutableString stringWithString:imageUrl]; + NSString * configUrl; + switch (type) { + case ImageTypeUserIcon: + { + configUrl = kImageTypeUserIcon; + } + break; + case ImageTypeCornerAvatar: + { + configUrl = kImageTypeCornerAvatar; + } + break; + case ImageTypeRoomFace: + { + configUrl = kImageTypeRoomFace; + } + break; + case ImageTypeUserLibaryDetail: + { + configUrl = kImageTypeUserLibaryDetail; + } + break; + case ImageTypeRoomGift: + { + configUrl = kImageTypeRoomGift; + } + break; + case ImageTypeUserInfoAlbum: + { + configUrl = kImageTypeUserInfoAlbum; + } + break; + default: + break; + } + if (configUrl) { + if ([imageUrl containsString:@"?"]) { + [urlString appendString:@"|"]; + }else{ + [urlString appendString:@"?"]; + } + [urlString appendString:configUrl]; + } + + NSString *encodedString = (NSString *) + CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, + (CFStringRef)urlString, + NULL, + (CFStringRef)@"|", + kCFStringEncodingUTF8)); + + return encodedString; +} @end diff --git a/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.h b/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.h index 5eeec179..75ecc733 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.h +++ b/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.h @@ -8,8 +8,10 @@ #import NS_ASSUME_NONNULL_BEGIN -@class XPRoomMessageDisplayModel;; +@class XPRoomMessageDisplayModel, YYLabel; @interface XPRoomMessageTableViewCell : UITableViewCell +///展示的内容 +@property (nonatomic,strong, readonly) YYLabel *contentLabel; /// @property (nonatomic,strong) XPRoomMessageDisplayModel *displayModel; @end diff --git a/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.m b/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.m index eeb6a834..e3b1cd39 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.m +++ b/xplan-ios/Main/Room/View/MessageContainerView/View/XPRoomMessageTableViewCell.m @@ -59,6 +59,13 @@ - (void)setDisplayModel:(XPRoomMessageDisplayModel *)displayModel { _displayModel = displayModel; self.contentLabel.attributedText = displayModel.contentAttributed; + displayModel.didSelectItem = ^(NSString * _Nonnull uid) { + NSLog(@"你好"); + }; + + displayModel.loadUrlImage = ^(UIImageView * _Nonnull imageView) { + NSLog(@"%@", imageView); + }; } - (UIImageView *)bubbleImaegeView { diff --git a/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.h b/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.h index 7a1b13f2..e2cadcf8 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.h +++ b/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.h @@ -6,10 +6,12 @@ // #import - +#import "RoomHostDelegate.h" NS_ASSUME_NONNULL_BEGIN -@interface XPRoomMessageContainerView : UIView +@interface XPRoomMessageContainerView : UIView + +- (instancetype)initWithDelegate:(id)delegate; @end diff --git a/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.m b/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.m index c28c6cbc..582195b5 100644 --- a/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.m +++ b/xplan-ios/Main/Room/View/MessageContainerView/XPRoomMessageContainerView.m @@ -9,10 +9,14 @@ ///Third #import #import +#import ///Tool #import "ThemeColor.h" +#import "XPMacro.h" +#import "XPRoomMessageConstant.h" ///Model #import "XPRoomMessageDisplayModel.h" +#import "RoomInfoModel.h" ///View #import "XPRoomMessageTableViewCell.h" #import "XPRoomMessageHeaderView.h" @@ -21,9 +25,7 @@ #import "XPIMRoomDelegate.h" -#import "XPIMManager.h" - -@interface XPRoomMessageContainerView () +@interface XPRoomMessageContainerView () ///列表 @property (nonatomic,strong) UITableView *messageTableView; ///数据源 @@ -36,15 +38,18 @@ @property (nonatomic,strong) UIButton *messageTipsBtn; ///是否处于正在爬楼 @property (nonatomic,assign) BOOL isPending; +///房间的代理 +@property (nonatomic,weak) id hostDelegate; @end @implementation XPRoomMessageContainerView -- (instancetype)initWithFrame:(CGRect)frame { - self = [super initWithFrame:frame]; +- (instancetype)initWithDelegate:(id)delegate { + self = [super init]; if (self) { - [[XPIMManager shareManager].chatRoomManager addDelegate:self]; + self.hostDelegate = delegate; + [[NIMSDK sharedSDK].chatManager addDelegate:self]; [self initSubViews]; [self initSubViewConstraints]; } @@ -78,10 +83,18 @@ }]; } +///是否是当前房间 +- (BOOL)isCurrentRoom:(NSString *)sessionId { + if ([sessionId isEqualToString:[NSString stringWithFormat:@"%ld", self.hostDelegate.getRoomInfo.roomId]]) { + return YES; + } + return NO; +} + #pragma mark - 添加数据并且做自动滚动 ///添加信息 - (void)addRoomMessage:(NIMMessage *)message { - XPRoomMessageDisplayModel * model = [[XPRoomMessageDisplayModel alloc] initDisplayModel:message]; + XPRoomMessageDisplayModel * model = [[XPRoomMessageDisplayModel alloc] initDisplayModel:message delefate:self]; [self.tempArray addObject:model]; ///开始加入数据 [self tryToappendAndScrollToBottom]; @@ -134,6 +147,29 @@ [self.messageTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionNone animated:animated]; //滚动到最后一行 } +#pragma mark - NIMChatManagerDelegate +- (void)onRecvMessages:(NSArray *)messages { + for (NIMMessage * message in messages) { + ///房间内的消息 + if (message.session.sessionType == NIMSessionTypeChatroom && [self isCurrentRoom:message.session.sessionId]) { + if (message.messageType == NIMMessageTypeText || message.messageType == NIMMessageTypeTip) { + [self addRoomMessage:message]; + } else if (message.messageType == NIMMessageTypeNotification) { + NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject; + NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content; + if (content.eventType == NIMChatroomEventTypeEnter) { + [self addRoomMessage:message]; + } + }else if (message.messageType == NIMMessageTypeCustom) {// 如果是自定义消息 + ///TODO : 处理自定义消息 + + } + } else { + + } + } +} + #pragma mark - ScrollViewDelegate - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 手动拖拽开始 @@ -182,6 +218,40 @@ } +- (void)xpRoomMessageDisplayModel:(XPRoomMessageDisplayModel *)displayModel loadImageSuccess:(UIImageView *)imageView { + NSInteger index = [self.datasource indexOfObject:displayModel]; + XPRoomMessageTableViewCell * cell = [self.messageTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; + [self loadUrlImageSuccess:imageView cell:cell]; +} + +- (void)xpRoomMessageDisplayModel:(XPRoomMessageDisplayModel *)displayModel didClickUser:(NSString *)userId { + NSLog(@"点击的用户%@", userId); +} + +- (void)loadUrlImageSuccess:(UIImageView *)imageView cell:(XPRoomMessageTableViewCell *)cell { + CGSize size = CGSizeMake(kRoomMessageMaxWidth, CGFLOAT_MAX); + YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:cell.contentLabel.attributedText]; + NSMutableAttributedString * attribute = [cell.contentLabel.attributedText mutableCopy]; + if (layout.attachments.count > 0) { + for (int i = 0; i < layout.attachments.count; i++) { + YYTextAttachment * attachment = [layout.attachments objectAtIndex:i]; + if (imageView == attachment.content) { + CGFloat kscale = (CGFloat)imageView.image.size.width / (CGFloat)imageView.image.size.height; + imageView.frame = CGRectMake(0, 0, 40 * kscale, 40); + attachment.content = imageView; + NSValue * value = [layout.attachmentRanges objectAtIndex:i]; + NSRange range = value.rangeValue; + NSMutableAttributedString * resultString = [NSMutableAttributedString yy_attachmentStringWithContent:imageView contentMode:UIViewContentModeScaleAspectFit attachmentSize:imageView.image.size alignToFont:[UIFont systemFontOfSize:14.0] alignment:YYTextVerticalAlignmentCenter]; + [attribute replaceCharactersInRange:range withAttributedString:resultString]; + break; + } + } + cell.contentLabel.attributedText = nil; + cell.contentLabel.attributedText = attribute; + } +} + + #pragma mark - Getters And Setters - (UITableView *)messageTableView { if (!_messageTableView) { diff --git a/xplan-ios/Main/Room/View/RoomHeaderView/RoomHeaderView.m b/xplan-ios/Main/Room/View/RoomHeaderView/RoomHeaderView.m index 06392c87..53a7825c 100644 --- a/xplan-ios/Main/Room/View/RoomHeaderView/RoomHeaderView.m +++ b/xplan-ios/Main/Room/View/RoomHeaderView/RoomHeaderView.m @@ -45,7 +45,7 @@ ///设置 @property (nonatomic,strong) UIButton *settingButton; -@property (nonatomic, strong) id hostDelegate; +@property (nonatomic, weak) id hostDelegate; @end diff --git a/xplan-ios/Main/Room/View/StageView/StageView.m b/xplan-ios/Main/Room/View/StageView/StageView.m index e788bcb6..f7fed240 100644 --- a/xplan-ios/Main/Room/View/StageView/StageView.m +++ b/xplan-ios/Main/Room/View/StageView/StageView.m @@ -32,7 +32,7 @@ @property (nonatomic, strong) NSMutableArray *microViews; @property (nonatomic, strong) NSMutableDictionary *micQueue; -@property (nonatomic, strong) id hostDelegate; +@property (nonatomic, weak) id hostDelegate; @end diff --git a/xplan-ios/Main/Room/View/XPRoomViewController.m b/xplan-ios/Main/Room/View/XPRoomViewController.m index 85419058..62cd0263 100644 --- a/xplan-ios/Main/Room/View/XPRoomViewController.m +++ b/xplan-ios/Main/Room/View/XPRoomViewController.m @@ -56,7 +56,7 @@ - (void)dealloc { -#warning to do - 时刻注意这个方法 功能做完的时候删除此方法 +#warning to dox - 时刻注意这个方法 功能做完的时候删除此方法 } @@ -70,7 +70,6 @@ - (void)viewDidLoad { [super viewDidLoad]; - [self addDelegate]; [self initSubViews]; [self initSubViewConstraints]; } @@ -82,10 +81,6 @@ #pragma mark - Private Method -- (void)addDelegate { - [[XPIMManager shareManager].chatRoomManager addDelegate:self]; -} - - (void)initSubViews { [self.view addSubview:self.backContainerView]; [self.view addSubview:self.roomHeaderView]; @@ -211,7 +206,7 @@ - (XPRoomMessageContainerView *)messageContainerView { if (!_messageContainerView) { - _messageContainerView = [[XPRoomMessageContainerView alloc] init]; + _messageContainerView = [[XPRoomMessageContainerView alloc] initWithDelegate:self]; } return _messageContainerView; }