From dae0d9a093a18d3da2efb1c749eadaec4b7ffceb Mon Sep 17 00:00:00 2001 From: Danmei Chen Date: Wed, 22 Aug 2018 11:59:22 +0200 Subject: [PATCH] complete resend function --- Classes/ChatConversationTableView.h | 2 + Classes/ChatConversationView.h | 4 +- Classes/ChatConversationView.m | 27 +------- Classes/LinphoneUI/UIChatBubblePhotoCell.m | 6 +- Classes/LinphoneUI/UIChatBubbleTextCell.h | 5 +- Classes/LinphoneUI/UIChatBubbleTextCell.m | 75 +++++++++++++++++++--- 6 files changed, 78 insertions(+), 41 deletions(-) diff --git a/Classes/ChatConversationTableView.h b/Classes/ChatConversationTableView.h index 551a22a4d..a29288367 100644 --- a/Classes/ChatConversationTableView.h +++ b/Classes/ChatConversationTableView.h @@ -28,6 +28,8 @@ @protocol ChatConversationDelegate - (BOOL)startImageUpload:(UIImage *)image assetId:(NSString *)phAssetId withQuality:(float)quality; +- (BOOL)startFileUpload:(NSData *)data assetId:(NSString *)phAssetId; +- (BOOL)startFileUpload:(NSData *)data withName:(NSString *)name; - (void)resendChat:(NSString *)message withExternalUrl:(NSString *)url; - (void)tableViewIsScrolling; diff --git a/Classes/ChatConversationView.h b/Classes/ChatConversationView.h index 422cad12a..3fa69519f 100644 --- a/Classes/ChatConversationView.h +++ b/Classes/ChatConversationView.h @@ -32,7 +32,7 @@ #include "linphone/linphonecore.h" @interface ChatConversationView - : TPMultiLayoutViewController { OrderedDictionary *imageQualities; BOOL scrollOnGrowingEnabled; @@ -61,7 +61,6 @@ @property (weak, nonatomic) IBOutlet UIIconButton *infoButton; @property (weak, nonatomic) IBOutlet UILabel *particpantsLabel; @property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController; -@property (nonatomic, strong) UIDocumentPickerViewController *documentPicker; @property NSMutableArray *imagesArray; @property NSMutableArray *assetIdsArray; @property NSMutableArray *qualitySettingsArray; @@ -81,7 +80,6 @@ - (IBAction)onDeleteClick:(id)sender; - (IBAction)onEditionChangeClick:(id)sender; - (void)update; -- (void)getIcloudFiles; - (void)openFile:(NSString *) filePath; - (void)clearMessageView; diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index 30a91a3b7..840cc3d7f 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -626,7 +626,7 @@ static UICompositeViewDescription *compositeDescription = nil; return TRUE; } -- (BOOL)startFileUpload:(NSData *)data assetId:phAssetId { +- (BOOL)startFileUpload:(NSData *)data assetId:(NSString *)phAssetId { FileTransferDelegate *fileTransfer = [[FileTransferDelegate alloc] init]; [fileTransfer uploadVideo:data withassetId:phAssetId forChatRoom:_chatRoom]; [_tableController scrollToBottom:true]; @@ -907,16 +907,6 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * [view.tableController scrollToBottom:true]; } -- (void)getIcloudFiles -{ - _documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] - inMode:UIDocumentPickerModeImport]; - _documentPicker.delegate = self; - - _documentPicker.modalPresentationStyle = UIModalPresentationOverCurrentContext ; - [self presentViewController:_documentPicker animated:YES completion:nil]; -} - - (void)openFile:(NSString *) filePath { // Open the controller. @@ -931,21 +921,6 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * } } -- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url { - - NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; - [fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL * _Nonnull newURL) { - - NSString *fileName = [newURL lastPathComponent]; - NSData *data = [NSData dataWithContentsOfURL:newURL]; - - NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; - - [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]; - [self openFile:filePath]; - }]; -} - - (void)deleteImageWithAssetId:(NSString *)assetId { NSUInteger key = [_assetIdsArray indexOfObject:assetId]; [_imagesArray removeObjectAtIndex:key]; diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index 18fe351d4..c9e4ed27c 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -279,8 +279,10 @@ } else { [self fileErrorBlock]; } - } else - [view getIcloudFiles]; + } else { + [LinphoneManager setValueInMessageAppData:@"onFileClick" forKey:@"icloudFileOption" inMessage:self.message]; + [super getIcloudFiles]; + } } diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.h b/Classes/LinphoneUI/UIChatBubbleTextCell.h index 18f5d879c..1d66bc5b8 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.h +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.h @@ -23,7 +23,7 @@ #import "ChatConversationTableView.h" #import "UIRoundedImageView.h" -@interface UIChatBubbleTextCell : UITableViewCell +@interface UIChatBubbleTextCell : UITableViewCell @property(readonly, nonatomic) LinphoneEventLog *event; @property(readonly, nonatomic) LinphoneChatMessage *message; @@ -40,11 +40,14 @@ @property(weak, nonatomic) IBOutlet UIImageView *imdmIcon; @property(weak, nonatomic) IBOutlet UILabel *imdmLabel; +@property (nonatomic, strong) UIDocumentPickerViewController *documentPicker; + + (CGSize)ViewSizeForMessage:(LinphoneChatMessage *)chat withWidth:(int)width; + (CGSize)getMediaMessageSizefromOriginalSize:(CGSize)originalSize withWidth:(int)width; - (void)setEvent:(LinphoneEventLog *)event; - (void)setChatMessage:(LinphoneChatMessage *)message; +- (void)getIcloudFiles; - (void)onDelete; - (void)onResend; diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index 85b20dce9..8e207f682 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -128,7 +128,7 @@ _statusInProgressSpinner.accessibilityLabel = @"Delivery in progress"; - if (_messageText) { + if (_messageText && ![LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:_message]) { LOGD(_messageText.text); [_messageText setHidden:FALSE]; /* We need to use an attributed string here so that data detector don't mess @@ -207,6 +207,36 @@ [PhoneMainView.instance presentViewController:errView animated:YES completion:nil]; } +- (void)getIcloudFiles { + _documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] + inMode:UIDocumentPickerModeImport]; + _documentPicker.delegate = self; + + _documentPicker.modalPresentationStyle = UIModalPresentationOverCurrentContext ; + ChatConversationView *view = VIEW(ChatConversationView); + [view presentViewController:_documentPicker animated:YES completion:nil]; + } + + - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url { + NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil]; + [fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL * _Nonnull newURL) { + + NSString *fileName = [newURL lastPathComponent]; + NSData *data = [NSData dataWithContentsOfURL:newURL]; + NSString *option = [LinphoneManager getMessageAppDataForKey:@"icloudFileOption" inMessage:self.message]; + + if ([option isEqualToString:@"onResend"]) + [_chatRoomDelegate startFileUpload:data withName:fileName]; + else if ([option isEqualToString:@"onFileClick"]) { + ChatConversationView *view = VIEW(ChatConversationView); + NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName]; + [[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil]; + [view openFile:filePath]; + } + }]; +} + + #pragma mark - Action Functions - (void)onDelete { @@ -235,10 +265,8 @@ if (linphone_chat_message_get_file_transfer_information(_message) != NULL) { NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:_message]; NSNumber *uploadQuality =[LinphoneManager getMessageAppDataForKey:@"uploadQuality" inMessage:_message]; - - // TODO: do resend for video and files - /*NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:_message]; - NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:_message];*/ + NSString *localVideo = [LinphoneManager getMessageAppDataForKey:@"localvideo" inMessage:_message]; + NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:_message]; [self onDelete]; if(localImage){ @@ -254,6 +282,9 @@ if (![assets firstObject]) return; PHAsset *asset = [assets firstObject]; + if (asset.mediaType != PHAssetMediaTypeImage) + return; + PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; options.synchronous = TRUE; [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:options @@ -268,10 +299,36 @@ } }]; } - } /*else if(fileName) { - NSString *filePath = [LinphoneManager documentFile:fileName]; - [_chatRoomDelegate startFileUpload:[NSData dataWithContentsOfFile:filePath] withUrl:[NSURL URLWithString:filePath]]; - }*/ + } else if (localVideo) { + PHFetchResult *assets = [PHAsset fetchAssetsWithLocalIdentifiers:[NSArray arrayWithObject:localVideo] options:nil]; + if (![assets firstObject]) + return; + PHAsset *asset = [assets firstObject]; + if (asset.mediaType != PHAssetMediaTypeVideo) + return; + + PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init]; + options.version = PHImageRequestOptionsVersionCurrent; + options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic; + + [[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) { + AVURLAsset *urlAsset = (AVURLAsset *)asset; + + NSURL *url = urlAsset.URL; + NSData *data = [NSData dataWithContentsOfURL:url]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), + ^(void) { + [_chatRoomDelegate startFileUpload:data assetId:localVideo]; + }); + }]; + + } else if (localFile) { + [LinphoneManager setValueInMessageAppData:@"onResend" forKey:@"icloudFileOption" inMessage:_message]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), + ^(void) { + [self getIcloudFiles]; + }); + } } else { [self onDelete]; double delayInSeconds = 0.4;