From 25ff20111675d2a3f44a42e878b23f0f89db2518 Mon Sep 17 00:00:00 2001 From: Danmei Chen Date: Thu, 20 Dec 2018 15:23:38 +0100 Subject: [PATCH] simplify the code about local file in chat cell --- Classes/ChatConversationView.h | 2 +- Classes/ChatConversationView.m | 10 ++++---- Classes/LinphoneUI/UIChatBubblePhotoCell.m | 6 ++--- Classes/LinphoneUI/UIChatBubbleTextCell.m | 6 ++--- Classes/Utils/FileTransferDelegate.m | 27 +++++++++++----------- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Classes/ChatConversationView.h b/Classes/ChatConversationView.h index bca159e8d..616e1457a 100644 --- a/Classes/ChatConversationView.h +++ b/Classes/ChatConversationView.h @@ -90,6 +90,6 @@ - (void)autoDownload:(LinphoneChatMessage *)message view:(ChatConversationView *)view inChat:(BOOL)inChat; - (NSURL *)getICloudFileUrl:(NSString *)name; -- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL; +- (BOOL)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL; @end diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index 35c550aad..38e239576 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -1033,17 +1033,17 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * return nil; } -- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL { +- (BOOL)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL { NSFileManager *fileManager = [NSFileManager defaultManager]; if (![[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"]) { //notify : set configuration to use icloud [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", nil) message:NSLocalizedString(@"ICloud Drive is unavailable.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:nil, nil] show]; - return; + return FALSE; } if ([fileManager isUbiquitousItemAtURL:fileURL]) { // if it exists, replace the file - [data writeToURL:fileURL atomically:TRUE]; + return [data writeToURL:fileURL atomically:TRUE]; } else { // get the url of localfile NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileURL.lastPathComponent]; @@ -1053,9 +1053,7 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog * } NSError *error; - if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:fileURL error:&error]) { - LOGE(@"ICloud file error : %@", error); - } + return [[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:fileURL error:&error]; } } diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index 259c1f23e..5125dc7f7 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -193,8 +193,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; } else if (localFile) { if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) { - ChatConversationView *view = VIEW(ChatConversationView); - NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]]; + NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]]; UIImage *image = [[UIImage alloc] initWithData:data]; [self loadImageAsset:nil image:image]; } else { @@ -316,8 +315,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; if (!asset) { NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) { - ChatConversationView *chatView = VIEW(ChatConversationView); - NSData *data = [NSData dataWithContentsOfURL:[chatView getICloudFileUrl:localFile]]; + NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]]; UIImage *image = [[UIImage alloc] initWithData:data]; if (image) [view setImage:image]; diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index 1b87fbb3f..41dff38a5 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -349,8 +349,7 @@ }]; } else if (localFile) { - ChatConversationView *view = VIEW(ChatConversationView); - NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]]; + NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]]; [_chatRoomDelegate startFileUpload:data withName:localFile]; } } else { @@ -467,8 +466,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; if(localFile) { if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) { - ChatConversationView *view = VIEW(ChatConversationView); - NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]]; + NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]]; UIImage *image = [[UIImage alloc] initWithData:data]; size = [self getMediaMessageSizefromOriginalSize:image.size withWidth:width]; diff --git a/Classes/Utils/FileTransferDelegate.m b/Classes/Utils/FileTransferDelegate.m index 07b6cbae6..34475f575 100644 --- a/Classes/Utils/FileTransferDelegate.m +++ b/Classes/Utils/FileTransferDelegate.m @@ -178,17 +178,17 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con //write file to path dispatch_async(dispatch_get_main_queue(), ^{ - [view writeFileInICloud:thiz.data fileURL:[view getICloudFileUrl:name]]; - [LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message]; - - [NSNotificationCenter.defaultCenter - postNotificationName:kLinphoneFileTransferRecvUpdate - object:thiz - userInfo:@{ - @"state" : @(LinphoneChatMessageStateDelivered), // we dont want to trigger - @"progress" : @(1.f), // FileTransferDone here - }]; - + if([view writeFileInICloud:thiz.data fileURL:[view getICloudFileUrl:name]]) { + [LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message]; + + [NSNotificationCenter.defaultCenter + postNotificationName:kLinphoneFileTransferRecvUpdate + object:thiz + userInfo:@{ + @"state" : @(LinphoneChatMessageStateDelivered), // we dont want to trigger + @"progress" : @(1.f), // FileTransferDone here + }]; + } [thiz stopAndDestroy]; }); } @@ -296,9 +296,8 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m - (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name { // we will write local files into ours folder of icloud ChatConversationView *view = VIEW(ChatConversationView); - [view writeFileInICloud:data fileURL:[view getICloudFileUrl:name]]; - - [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil]; + if ([view writeFileInICloud:data fileURL:[view getICloudFileUrl:name]]) + [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil]; }