EaseMessageModel.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EaseMessageModel.h"
  13. #import "EaseEmotionEscape.h"
  14. #import "EaseConvertToCommonEmoticonsHelper.h"
  15. @implementation EaseMessageModel
  16. - (instancetype)initWithMessage:(EMMessage *)message
  17. {
  18. self = [super init];
  19. if (self) {
  20. _cellHeight = -1;
  21. _message = message;
  22. _firstMessageBody = message.body;
  23. _isMediaPlaying = NO;
  24. _nickname = message.from;
  25. _isSender = message.direction == EMMessageDirectionSend ? YES : NO;
  26. switch (_firstMessageBody.type) {
  27. case EMMessageBodyTypeText:
  28. {
  29. EMTextMessageBody *textBody = (EMTextMessageBody *)_firstMessageBody;
  30. NSString *didReceiveText = [EaseConvertToCommonEmoticonsHelper convertToSystemEmoticons:textBody.text];
  31. self.text = didReceiveText;
  32. }
  33. break;
  34. case EMMessageBodyTypeImage:
  35. {
  36. EMImageMessageBody *imgMessageBody = (EMImageMessageBody *)_firstMessageBody;
  37. NSData *imageData = [NSData dataWithContentsOfFile:imgMessageBody.localPath];
  38. if (imageData.length) {
  39. self.image = [UIImage imageWithData:imageData];
  40. }
  41. if ([imgMessageBody.thumbnailLocalPath length] > 0) {
  42. self.thumbnailImage = [UIImage imageWithContentsOfFile:imgMessageBody.thumbnailLocalPath];
  43. }
  44. else{
  45. CGSize size = self.image.size;
  46. self.thumbnailImage = size.width * size.height > 200 * 200 ? [self scaleImage:self.image toScale:sqrt((200 * 200) / (size.width * size.height))] : self.image;
  47. }
  48. self.thumbnailImageSize = self.thumbnailImage.size;
  49. self.imageSize = imgMessageBody.size;
  50. if (!_isSender) {
  51. self.fileURLPath = imgMessageBody.remotePath;
  52. }
  53. }
  54. break;
  55. case EMMessageBodyTypeLocation:
  56. {
  57. EMLocationMessageBody *locationBody = (EMLocationMessageBody *)_firstMessageBody;
  58. self.address = locationBody.address;
  59. self.latitude = locationBody.latitude;
  60. self.longitude = locationBody.longitude;
  61. }
  62. break;
  63. case EMMessageBodyTypeVoice:
  64. {
  65. EMVoiceMessageBody *voiceBody = (EMVoiceMessageBody *)_firstMessageBody;
  66. self.mediaDuration = voiceBody.duration;
  67. self.isMediaPlayed = NO;
  68. if (message.ext) {
  69. self.isMediaPlayed = [[message.ext objectForKey:@"isPlayed"] boolValue];
  70. }
  71. // audio file path
  72. self.fileURLPath = voiceBody.remotePath;
  73. }
  74. break;
  75. case EMMessageBodyTypeVideo:
  76. {
  77. EMVideoMessageBody *videoBody = (EMVideoMessageBody *)message.body;
  78. self.thumbnailImageSize = videoBody.thumbnailSize;
  79. if ([videoBody.thumbnailLocalPath length] > 0) {
  80. NSData *thumbnailImageData = [NSData dataWithContentsOfFile:videoBody.thumbnailLocalPath];
  81. if (thumbnailImageData.length) {
  82. self.thumbnailImage = [UIImage imageWithData:thumbnailImageData];
  83. }
  84. self.image = self.thumbnailImage;
  85. }
  86. // video file path
  87. self.fileURLPath = videoBody.remotePath;
  88. }
  89. break;
  90. case EMMessageBodyTypeFile:
  91. {
  92. EMFileMessageBody *fileMessageBody = (EMFileMessageBody *)_firstMessageBody;
  93. self.fileIconName = @"EaseUIResource.bundle/chat_item_file";
  94. self.fileName = fileMessageBody.displayName;
  95. self.fileSize = fileMessageBody.fileLength;
  96. if (self.fileSize < 1024) {
  97. self.fileSizeDes = [NSString stringWithFormat:@"%fB", self.fileSize];
  98. }
  99. else if(self.fileSize < 1024 * 1024){
  100. self.fileSizeDes = [NSString stringWithFormat:@"%.2fkB", self.fileSize / 1024];
  101. }
  102. else if (self.fileSize < 2014 * 1024 * 1024){
  103. self.fileSizeDes = [NSString stringWithFormat:@"%.2fMB", self.fileSize / (1024 * 1024)];
  104. }
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. return self;
  112. }
  113. - (NSString *)messageId
  114. {
  115. return _message.messageId;
  116. }
  117. - (EMMessageStatus)messageStatus
  118. {
  119. return _message.status;
  120. }
  121. - (EMChatType)messageType
  122. {
  123. return _message.chatType;
  124. }
  125. - (EMMessageBodyType)bodyType
  126. {
  127. return self.firstMessageBody.type;
  128. }
  129. - (BOOL)isMessageRead
  130. {
  131. return _message.isReadAcked;
  132. }
  133. - (NSString *)fileLocalPath
  134. {
  135. if (_firstMessageBody) {
  136. switch (_firstMessageBody.type) {
  137. case EMMessageBodyTypeVideo:
  138. case EMMessageBodyTypeImage:
  139. case EMMessageBodyTypeVoice:
  140. case EMMessageBodyTypeFile:
  141. {
  142. EMFileMessageBody *fileBody = (EMFileMessageBody *)_firstMessageBody;
  143. return fileBody.localPath;
  144. }
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. return nil;
  151. }
  152. - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
  153. {
  154. UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
  155. [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
  156. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  157. UIGraphicsEndImageContext();
  158. return scaledImage;
  159. }
  160. @end