simplify the code about local file in chat cell

This commit is contained in:
Danmei Chen 2018-12-20 15:23:38 +01:00
parent 445b620be5
commit 25ff201116
5 changed files with 22 additions and 29 deletions

View file

@ -90,6 +90,6 @@
- (void)autoDownload:(LinphoneChatMessage *)message view:(ChatConversationView *)view inChat:(BOOL)inChat; - (void)autoDownload:(LinphoneChatMessage *)message view:(ChatConversationView *)view inChat:(BOOL)inChat;
- (NSURL *)getICloudFileUrl:(NSString *)name; - (NSURL *)getICloudFileUrl:(NSString *)name;
- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL; - (BOOL)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL;
@end @end

View file

@ -1033,17 +1033,17 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog *
return nil; return nil;
} }
- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL { - (BOOL)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL {
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
if (![[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"]) { if (![[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"]) {
//notify : set configuration to use icloud //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]; [[[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 ([fileManager isUbiquitousItemAtURL:fileURL]) {
// if it exists, replace the file // if it exists, replace the file
[data writeToURL:fileURL atomically:TRUE]; return [data writeToURL:fileURL atomically:TRUE];
} else { } else {
// get the url of localfile // get the url of localfile
NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileURL.lastPathComponent]; NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileURL.lastPathComponent];
@ -1053,9 +1053,7 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog *
} }
NSError *error; NSError *error;
if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:fileURL error:&error]) { return [[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:fileURL error:&error];
LOGE(@"ICloud file error : %@", error);
}
} }
} }

View file

@ -193,8 +193,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100;
} }
else if (localFile) { else if (localFile) {
if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) { if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) {
ChatConversationView *view = VIEW(ChatConversationView); NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]];
NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]];
UIImage *image = [[UIImage alloc] initWithData:data]; UIImage *image = [[UIImage alloc] initWithData:data];
[self loadImageAsset:nil image:image]; [self loadImageAsset:nil image:image];
} else { } else {
@ -316,8 +315,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100;
if (!asset) { if (!asset) {
NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message]; NSString *localFile = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message];
if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) { if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) {
ChatConversationView *chatView = VIEW(ChatConversationView); NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]];
NSData *data = [NSData dataWithContentsOfURL:[chatView getICloudFileUrl:localFile]];
UIImage *image = [[UIImage alloc] initWithData:data]; UIImage *image = [[UIImage alloc] initWithData:data];
if (image) if (image)
[view setImage:image]; [view setImage:image];

View file

@ -349,8 +349,7 @@
}]; }];
} else if (localFile) { } else if (localFile) {
ChatConversationView *view = VIEW(ChatConversationView); NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]];
NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]];
[_chatRoomDelegate startFileUpload:data withName:localFile]; [_chatRoomDelegate startFileUpload:data withName:localFile];
} }
} else { } else {
@ -467,8 +466,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100;
if(localFile) { if(localFile) {
if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) { if ([localFile hasSuffix:@"JPG"] || [localFile hasSuffix:@"PNG"]) {
ChatConversationView *view = VIEW(ChatConversationView); NSData *data = [NSData dataWithContentsOfURL:[VIEW(ChatConversationView) getICloudFileUrl:localFile]];
NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]];
UIImage *image = [[UIImage alloc] initWithData:data]; UIImage *image = [[UIImage alloc] initWithData:data];
size = [self getMediaMessageSizefromOriginalSize:image.size withWidth:width]; size = [self getMediaMessageSizefromOriginalSize:image.size withWidth:width];

View file

@ -178,7 +178,7 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con
//write file to path //write file to path
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[view writeFileInICloud:thiz.data fileURL:[view getICloudFileUrl:name]]; if([view writeFileInICloud:thiz.data fileURL:[view getICloudFileUrl:name]]) {
[LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message]; [LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message];
[NSNotificationCenter.defaultCenter [NSNotificationCenter.defaultCenter
@ -188,7 +188,7 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con
@"state" : @(LinphoneChatMessageStateDelivered), // we dont want to trigger @"state" : @(LinphoneChatMessageStateDelivered), // we dont want to trigger
@"progress" : @(1.f), // FileTransferDone here @"progress" : @(1.f), // FileTransferDone here
}]; }];
}
[thiz stopAndDestroy]; [thiz stopAndDestroy];
}); });
} }
@ -296,8 +296,7 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m
- (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name { - (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name {
// we will write local files into ours folder of icloud // we will write local files into ours folder of icloud
ChatConversationView *view = VIEW(ChatConversationView); ChatConversationView *view = VIEW(ChatConversationView);
[view writeFileInICloud:data fileURL:[view getICloudFileUrl:name]]; 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]; [self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil];
} }