/************************************************************ * * Hyphenate CONFIDENTIAL * __________________ * Copyright (C) 2016 Hyphenate Inc. All rights reserved. * * NOTICE: All information contained herein is, and remains * the property of Hyphenate Inc. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Hyphenate Inc. */ #import "EaseMessageReadManager.h" #import "UIImageView+WebCache.h" #import "EMCDDeviceManager.h" #import "MWPhotoBrowser.h" #define IMAGE_MAX_SIZE_5k 5120*2880 static EaseMessageReadManager *detailInstance = nil; @interface EaseMessageReadManager() @property (strong, nonatomic) UIWindow *keyWindow; @property (strong, nonatomic) NSMutableArray *photos; @property (strong, nonatomic) UINavigationController *photoNavigationController; @property (strong, nonatomic) UIAlertView *textAlertView; @end @implementation EaseMessageReadManager + (id)defaultManager { @synchronized(self){ static dispatch_once_t pred; dispatch_once(&pred, ^{ detailInstance = [[self alloc] init]; }); } return detailInstance; } #pragma mark - getter - (UIWindow *)keyWindow { if(_keyWindow == nil) { _keyWindow = [[UIApplication sharedApplication] keyWindow]; } return _keyWindow; } - (NSMutableArray *)photos { if (_photos == nil) { _photos = [[NSMutableArray alloc] init]; } return _photos; } - (MWPhotoBrowser *)photoBrowser { if (_photoBrowser == nil) { _photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self]; _photoBrowser.displayActionButton = YES; _photoBrowser.displayNavArrows = YES; _photoBrowser.displaySelectionButtons = NO; _photoBrowser.alwaysShowControls = NO; _photoBrowser.wantsFullScreenLayout = YES; _photoBrowser.zoomPhotosToFill = YES; _photoBrowser.enableGrid = NO; _photoBrowser.startOnGrid = NO; [_photoBrowser setCurrentPhotoIndex:0]; } return _photoBrowser; } - (UINavigationController *)photoNavigationController { if (_photoNavigationController == nil) { _photoNavigationController = [[UINavigationController alloc] initWithRootViewController:self.photoBrowser]; _photoNavigationController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; } [self.photoBrowser reloadData]; return _photoNavigationController; } #pragma mark - MWPhotoBrowserDelegate - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser { return [self.photos count]; } - (id )photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index { if (index < self.photos.count) { return [self.photos objectAtIndex:index]; } return nil; } #pragma mark - private #pragma mark - public - (void)showBrowserWithImages:(NSArray *)imageArray { if (imageArray && [imageArray count] > 0) { NSMutableArray *photoArray = [NSMutableArray array]; for (id object in imageArray) { MWPhoto *photo; if ([object isKindOfClass:[UIImage class]]) { CGFloat imageSize = ((UIImage*)object).size.width * ((UIImage*)object).size.height; if (imageSize > IMAGE_MAX_SIZE_5k) { photo = [MWPhoto photoWithImage:[self scaleImage:object toScale:(IMAGE_MAX_SIZE_5k)/imageSize]]; } else { photo = [MWPhoto photoWithImage:object]; } } else if ([object isKindOfClass:[NSURL class]]) { photo = [MWPhoto photoWithURL:object]; } else if ([object isKindOfClass:[NSString class]]) { } [photoArray addObject:photo]; } self.photos = photoArray; } UIViewController *rootController = [self.keyWindow rootViewController]; [rootController presentViewController:self.photoNavigationController animated:YES completion:nil]; } - (BOOL)prepareMessageAudioModel:(EaseMessageModel *)messageModel updateViewCompletion:(void (^)(EaseMessageModel *prevAudioModel, EaseMessageModel *currentAudioModel))updateCompletion { BOOL isPrepare = NO; if(messageModel.bodyType == EMMessageBodyTypeVoice) { EaseMessageModel *prevAudioModel = self.audioMessageModel; EaseMessageModel *currentAudioModel = messageModel; self.audioMessageModel = messageModel; BOOL isPlaying = messageModel.isMediaPlaying; if (isPlaying) { messageModel.isMediaPlaying = NO; self.audioMessageModel = nil; currentAudioModel = nil; [[EMCDDeviceManager sharedInstance] stopPlaying]; } else { messageModel.isMediaPlaying = YES; prevAudioModel.isMediaPlaying = NO; isPrepare = YES; if (!messageModel.isMediaPlayed) { messageModel.isMediaPlayed = YES; EMMessage *chatMessage = messageModel.message; if (chatMessage.ext) { NSMutableDictionary *dict = [chatMessage.ext mutableCopy]; if (![[dict objectForKey:@"isPlayed"] boolValue]) { [dict setObject:@YES forKey:@"isPlayed"]; chatMessage.ext = dict; [[EMClient sharedClient].chatManager updateMessage:chatMessage completion:nil]; } } else { NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:chatMessage.ext]; [dic setObject:@YES forKey:@"isPlayed"]; chatMessage.ext = dic; [[EMClient sharedClient].chatManager updateMessage:chatMessage completion:nil]; } } } if (updateCompletion) { updateCompletion(prevAudioModel, currentAudioModel); } } return isPrepare; } - (EaseMessageModel *)stopMessageAudioModel { EaseMessageModel *model = nil; if (self.audioMessageModel.bodyType == EMMessageBodyTypeVoice) { if (self.audioMessageModel.isMediaPlaying) { model = self.audioMessageModel; } self.audioMessageModel.isMediaPlaying = NO; self.audioMessageModel = nil; } return model; } - (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize)); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } @end