Skip to content

Instantly share code, notes, and snippets.

@w-i-n-s
Created September 25, 2015 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w-i-n-s/ba53ef49818ea6db79be to your computer and use it in GitHub Desktop.
Save w-i-n-s/ba53ef49818ea6db79be to your computer and use it in GitHub Desktop.
MailCore tip2
- (void)updateDownloadedAttachment:(Attachment*)attachment withFilePath:(NSString*)filePath andMessage:(Message*)aMessage{
NSManagedObjectContext *moc = [self getContextForBGTask];
[moc performBlock:^{
Attachment *__attachment = (Attachment*)[moc objectWithID:[attachment objectID]];
if(!__attachment ){ //(error){
NSLog(@"Error get attach %s - %@",__func__,__attachment);
return;
}
[__attachment setIsDownloaded:@(YES)];
[__attachment setFilePath:filePath];
[self saveContextForBGTask:moc];
Message *message = (Message*)[moc objectWithID:[aMessage objectID]];
if (!message || ![message mcUid]) {
NSLog(@"Something wrong with attach %@",filePath);
return;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isDownloaded == nil || isDownloaded == %@",@(NO)];
NSArray *unloadedAttaches = [[[message attachments] allObjects] filteredArrayUsingPredicate:predicate];
if (![unloadedAttaches count]){
[message setIsDownloadAllAtachments:@(YES)];
[[NSNotificationCenter defaultCenter] postNotificationName:NMNotif_dataSingleton_updateMessageBody
object:self
userInfo:[NSDictionary dictionaryWithObject:[message mcUid]
forKey:@"uid"]];
predicate = [NSPredicate predicateWithFormat:@"type == %@",@(NMAttachmentTypeCalendarInvite)];
NSArray *invitations = [[[message attachments] allObjects] filteredArrayUsingPredicate:predicate];
if ([invitations count]){
Attachment *invite = [invitations firstObject];
if (invite &&
[invite filePath]) {
NSError *error;
NSURL *url = [[NSURL alloc] initFileURLWithPath:[invite filePath]];
NSString *icsString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:&error];
BMIcsToHtmlConverter *converter = [[BMIcsToHtmlConverter alloc] init];
NSString *htmlHeader = [converter htmlHeaderStringFromICSString:icsString];
icsString = nil;
icsString = [message bodyHTML];
NSRange range = [icsString rangeOfString:@"reply="];
if (range.location==NSNotFound){
if(!icsString)
[message setBodyHTML:htmlHeader];
else{
NSRange range = [icsString rangeOfString:@"<body>"];
if (range.location==NSNotFound) {
[message setBodyHTML:htmlHeader];
}else{
icsString = nil;
icsString = [NSString stringWithFormat:@"%@%@%@",
[[message bodyHTML] substringToIndex:range.location],
htmlHeader,
[[message bodyHTML] substringFromIndex:range.location]];
if(icsString)
[message setBodyHTML:icsString];
[self saveContextForBGTask:moc];
[[NSNotificationCenter defaultCenter] postNotificationName:NMNotif_dataSingleton_updateMessageBody
object:self
userInfo:[NSDictionary dictionaryWithObject:[message mcUid]
forKey:@"uid"]];
}
}
}
}
}
[self saveContextForBGTask:moc];
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment