EaseMessageReadManager.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "EaseMessageReadManager.h"
  13. #import "UIImageView+EMWebCache.h"
  14. #import "EMCDDeviceManager.h"
  15. #define IMAGE_MAX_SIZE_5k 5120*2880
  16. static EaseMessageReadManager *detailInstance = nil;
  17. @interface EaseMessageReadManager()
  18. @property (strong, nonatomic) UIWindow *keyWindow;
  19. @property (strong, nonatomic) NSMutableArray *photos;
  20. @property (strong, nonatomic) UINavigationController *photoNavigationController;
  21. @property (strong, nonatomic) UIAlertView *textAlertView;
  22. @end
  23. @implementation EaseMessageReadManager
  24. + (id)defaultManager
  25. {
  26. @synchronized(self){
  27. static dispatch_once_t pred;
  28. dispatch_once(&pred, ^{
  29. detailInstance = [[self alloc] init];
  30. });
  31. }
  32. return detailInstance;
  33. }
  34. #pragma mark - getter
  35. - (UIWindow *)keyWindow
  36. {
  37. if(_keyWindow == nil)
  38. {
  39. _keyWindow = [[UIApplication sharedApplication] keyWindow];
  40. }
  41. return _keyWindow;
  42. }
  43. - (NSMutableArray *)photos
  44. {
  45. if (_photos == nil) {
  46. _photos = [[NSMutableArray alloc] init];
  47. }
  48. return _photos;
  49. }
  50. - (MWPhotoBrowser *)photoBrowser
  51. {
  52. if (_photoBrowser == nil) {
  53. _photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  54. _photoBrowser.displayActionButton = YES;
  55. _photoBrowser.displayNavArrows = YES;
  56. _photoBrowser.displaySelectionButtons = NO;
  57. _photoBrowser.alwaysShowControls = NO;
  58. _photoBrowser.wantsFullScreenLayout = YES;
  59. _photoBrowser.zoomPhotosToFill = YES;
  60. _photoBrowser.enableGrid = NO;
  61. _photoBrowser.startOnGrid = NO;
  62. [_photoBrowser setCurrentPhotoIndex:0];
  63. }
  64. return _photoBrowser;
  65. }
  66. - (UINavigationController *)photoNavigationController
  67. {
  68. if (_photoNavigationController == nil) {
  69. _photoNavigationController = [[UINavigationController alloc] initWithRootViewController:self.photoBrowser];
  70. _photoNavigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  71. }
  72. [self.photoBrowser reloadData];
  73. return _photoNavigationController;
  74. }
  75. #pragma mark - MWPhotoBrowserDelegate
  76. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
  77. {
  78. return [self.photos count];
  79. }
  80. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
  81. {
  82. if (index < self.photos.count)
  83. {
  84. return [self.photos objectAtIndex:index];
  85. }
  86. return nil;
  87. }
  88. #pragma mark - private
  89. #pragma mark - public
  90. - (void)showBrowserWithImages:(NSArray *)imageArray
  91. {
  92. if (imageArray && [imageArray count] > 0) {
  93. NSMutableArray *photoArray = [NSMutableArray array];
  94. for (id object in imageArray) {
  95. MWPhoto *photo;
  96. if ([object isKindOfClass:[UIImage class]]) {
  97. CGFloat imageSize = ((UIImage*)object).size.width * ((UIImage*)object).size.height;
  98. if (imageSize > IMAGE_MAX_SIZE_5k) {
  99. photo = [MWPhoto photoWithImage:[self scaleImage:object toScale:(IMAGE_MAX_SIZE_5k)/imageSize]];
  100. } else {
  101. photo = [MWPhoto photoWithImage:object];
  102. }
  103. }
  104. else if ([object isKindOfClass:[NSURL class]])
  105. {
  106. photo = [MWPhoto photoWithURL:object];
  107. }
  108. else if ([object isKindOfClass:[NSString class]])
  109. {
  110. }
  111. [photoArray addObject:photo];
  112. }
  113. self.photos = photoArray;
  114. }
  115. UIViewController *rootController = [self.keyWindow rootViewController];
  116. [rootController presentViewController:self.photoNavigationController animated:YES completion:nil];
  117. }
  118. - (BOOL)prepareMessageAudioModel:(EaseMessageModel *)messageModel
  119. updateViewCompletion:(void (^)(EaseMessageModel *prevAudioModel, EaseMessageModel *currentAudioModel))updateCompletion
  120. {
  121. BOOL isPrepare = NO;
  122. if(messageModel.bodyType == EMMessageBodyTypeVoice)
  123. {
  124. EaseMessageModel *prevAudioModel = self.audioMessageModel;
  125. EaseMessageModel *currentAudioModel = messageModel;
  126. self.audioMessageModel = messageModel;
  127. BOOL isPlaying = messageModel.isMediaPlaying;
  128. if (isPlaying) {
  129. messageModel.isMediaPlaying = NO;
  130. self.audioMessageModel = nil;
  131. currentAudioModel = nil;
  132. [[EMCDDeviceManager sharedInstance] stopPlaying];
  133. }
  134. else {
  135. messageModel.isMediaPlaying = YES;
  136. prevAudioModel.isMediaPlaying = NO;
  137. isPrepare = YES;
  138. if (!messageModel.isMediaPlayed) {
  139. messageModel.isMediaPlayed = YES;
  140. EMMessage *chatMessage = messageModel.message;
  141. if (chatMessage.ext) {
  142. NSMutableDictionary *dict = [chatMessage.ext mutableCopy];
  143. if (![[dict objectForKey:@"isPlayed"] boolValue]) {
  144. [dict setObject:@YES forKey:@"isPlayed"];
  145. chatMessage.ext = dict;
  146. [[EMClient sharedClient].chatManager updateMessage:chatMessage completion:nil];
  147. }
  148. } else {
  149. NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:chatMessage.ext];
  150. [dic setObject:@YES forKey:@"isPlayed"];
  151. chatMessage.ext = dic;
  152. [[EMClient sharedClient].chatManager updateMessage:chatMessage completion:nil];
  153. }
  154. }
  155. }
  156. if (updateCompletion) {
  157. updateCompletion(prevAudioModel, currentAudioModel);
  158. }
  159. }
  160. return isPrepare;
  161. }
  162. - (EaseMessageModel *)stopMessageAudioModel
  163. {
  164. EaseMessageModel *model = nil;
  165. if (self.audioMessageModel.bodyType == EMMessageBodyTypeVoice) {
  166. if (self.audioMessageModel.isMediaPlaying) {
  167. model = self.audioMessageModel;
  168. }
  169. self.audioMessageModel.isMediaPlaying = NO;
  170. self.audioMessageModel = nil;
  171. }
  172. return model;
  173. }
  174. - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize
  175. {
  176. UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize));
  177. [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
  178. UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  179. UIGraphicsEndImageContext();
  180. return scaledImage;
  181. }
  182. @end