TUIMessageCellData.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // TUIMessageCellData.m
  3. // TXIMSDK_TUIKit_iOS
  4. //
  5. // Created by annidyfeng on 2019/5/21.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import "TUIMessageCellData.h"
  9. #import <TIMCommon/TIMDefine.h>
  10. @interface TUIMessageCellData ()
  11. @end
  12. @implementation TUIMessageCellData
  13. {
  14. NSString *_msgID;
  15. NSString *_identifier;
  16. NSURL *_avatarUrl;
  17. }
  18. + (TUIMessageCellData *)getCellData:(V2TIMMessage *)message {
  19. return nil;
  20. }
  21. + (NSString *)getDisplayString:(V2TIMMessage *)message {
  22. return nil;
  23. }
  24. - (Class)getReplyQuoteViewDataClass {
  25. return nil;
  26. }
  27. - (Class)getReplyQuoteViewClass {
  28. return nil;
  29. }
  30. - (instancetype)initWithDirection:(TMsgDirection)direction {
  31. self = [super init];
  32. if (self) {
  33. _direction = direction;
  34. _status = Msg_Status_Init;
  35. _source = Msg_Source_Unkown;
  36. _showReadReceipt = YES;
  37. _sameToNextMsgSender = NO;
  38. _showAvatar = YES;
  39. _cellLayout = [self cellLayout:direction];
  40. _additionalUserInfoResult = @{};
  41. }
  42. return self;
  43. }
  44. - (TUIMessageCellLayout *)cellLayout:(TMsgDirection)direction {
  45. if (direction == MsgDirectionIncoming) {
  46. return [TUIMessageCellLayout incommingMessageLayout];
  47. } else {
  48. return [TUIMessageCellLayout outgoingMessageLayout];
  49. }
  50. }
  51. - (void)setMsgID:(NSString *)msgID {
  52. _msgID = msgID;
  53. }
  54. - (NSString *)msgID {
  55. if (_msgID) {
  56. return _msgID;
  57. }
  58. if (self.innerMessage) {
  59. return self.innerMessage.msgID;
  60. }
  61. return nil;
  62. }
  63. - (void)setIdentifier:(NSString *)identifier {
  64. _identifier = identifier;
  65. }
  66. - (NSString *)identifier {
  67. if (_identifier) {
  68. return _identifier;
  69. }
  70. if (self.innerMessage) {
  71. return self.innerMessage.sender;
  72. }
  73. return nil;
  74. }
  75. - (NSString *)senderName {
  76. if (self.innerMessage) {
  77. if(self.innerMessage.localCustomData){
  78. NSDictionary *dict = [self dictionaryFromData:self.innerMessage.localCustomData];
  79. NSString *nickName = [TUIMessageCellData objectOrNilForKey:@"showName" fromDictionary:dict];
  80. if(nickName.length > 0){
  81. return nickName;
  82. }
  83. }
  84. return self.innerMessage.nameCard ? : (self.innerMessage.friendRemark ? : (self.innerMessage.nickName ? : self.innerMessage.sender));
  85. }
  86. return nil;
  87. }
  88. - (void)setAvatarUrl:(NSURL *)avatarUrl {
  89. _avatarUrl = avatarUrl;
  90. }
  91. - (NSDictionary *)dictionaryFromData:(NSData *)data {
  92. NSError *error = nil;
  93. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  94. if (error) {
  95. NSLog(@"Error converting NSData to NSDictionary: %@", error.localizedDescription);
  96. return nil;
  97. }
  98. return dictionary;
  99. }
  100. + (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
  101. {
  102. id object = [dict objectForKey:aKey];
  103. return [object isEqual:[NSNull null]] ? nil : object;
  104. }
  105. - (NSURL *)avatarUrl {
  106. if (_avatarUrl) {
  107. return _avatarUrl;
  108. }
  109. if (self.innerMessage) {
  110. if(self.innerMessage.localCustomData){
  111. NSDictionary *dict = [self dictionaryFromData:self.innerMessage.localCustomData];
  112. NSString *avatarStr = [TUIMessageCellData objectOrNilForKey:@"avatarURL" fromDictionary:dict];
  113. if(avatarStr.length > 0){
  114. return [NSURL URLWithString:avatarStr];
  115. }
  116. }
  117. return [NSURL URLWithString:self.innerMessage.faceURL];;
  118. }
  119. return nil;
  120. }
  121. - (NSInteger)headgearType{
  122. if (self.innerMessage){
  123. if(self.innerMessage.localCustomData){
  124. NSDictionary *dict = [self dictionaryFromData:self.innerMessage.localCustomData];
  125. NSInteger type = [[TUIMessageCellData objectOrNilForKey:@"headgearType" fromDictionary:dict] integerValue];
  126. return type;
  127. }
  128. }
  129. return 0;
  130. }
  131. - (NSString *)headdress{
  132. if (self.innerMessage){
  133. if(self.innerMessage.localCustomData){
  134. NSDictionary *dict = [self dictionaryFromData:self.innerMessage.localCustomData];
  135. NSString *headdressStr = [TUIMessageCellData objectOrNilForKey:@"headdress" fromDictionary:dict];
  136. return headdressStr;
  137. }
  138. }
  139. return @"";
  140. }
  141. - (BOOL)canForward {
  142. return YES;
  143. }
  144. - (BOOL)canLongPress {
  145. return YES;
  146. }
  147. - (BOOL)shouldHide {
  148. return NO;
  149. }
  150. - (BOOL)customReloadCellWithNewMsg:(V2TIMMessage *)newMessage {
  151. return NO;
  152. }
  153. - (CGSize)msgStatusSize {
  154. if (self.showReadReceipt && self.innerMessage.needReadReceipt &&
  155. (self.innerMessage.userID || self.innerMessage.groupID)) {
  156. if (self.direction == MsgDirectionOutgoing) {
  157. return CGSizeMake(54, 14);
  158. } else {
  159. return CGSizeMake(38, 14);
  160. }
  161. }
  162. else {
  163. //The community type does not require read receipt markers, only the time is needed.
  164. return CGSizeMake(26, 14);
  165. }
  166. }
  167. - (NSDictionary *)messageModifyUserInfos {
  168. return self.additionalUserInfoResult;
  169. }
  170. - (NSArray<NSString *> *)requestForAdditionalUserInfo {
  171. return @[];
  172. }
  173. @end