From 27d1f512f0899a0f04169a8846b9188811cf45db Mon Sep 17 00:00:00 2001 From: Michael Starke Date: Fri, 17 Aug 2018 12:19:16 +0200 Subject: [PATCH] Moved favIconFetching to MPIconHelper --- MacPass/MPIconHelper.h | 2 ++ MacPass/MPIconHelper.m | 44 ++++++++++++++++++++---- MacPass/MPIconSelectViewController.m | 51 ++++++++-------------------- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/MacPass/MPIconHelper.h b/MacPass/MPIconHelper.h index f9c554d1..3aeab3e4 100644 --- a/MacPass/MPIconHelper.h +++ b/MacPass/MPIconHelper.h @@ -144,4 +144,6 @@ typedef NS_ENUM(NSUInteger, MPIconType) { */ + (NSArray *)databaseIconTypes; ++ (void)fetchIconDataForURL:(NSURL *)url completionHandler:(void (^)(NSData *iconData))handler; + @end diff --git a/MacPass/MPIconHelper.m b/MacPass/MPIconHelper.m index f6ee3cf5..7d133ace 100644 --- a/MacPass/MPIconHelper.m +++ b/MacPass/MPIconHelper.m @@ -167,13 +167,45 @@ @(MPIconExpiredEntry): NSImageNameCaution, @(MPIconExpiredGroup): NSImageNameCaution }; - - - - - - }); return imageNames; } + ++ (void)fetchIconDataForURL:(NSURL *)url completionHandler:(void (^)(NSData *iconData))handler { + + if(!url || !handler) { + return; // no url, no handler so no need to do anything + } + + NSString *urlString = [NSString stringWithFormat:@"%@://%@/favicon.ico", url.scheme, url.host ? url.host : @""]; + NSURL *favIconURL = [NSURL URLWithString:urlString]; + if(!favIconURL) { + /* call the handler with nil data */ + handler(nil); + return; + } + + NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:favIconURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { + if(error) { + //dispatch_async(dispatch_get_main_queue(), ^{ + handler(nil); + //}); + } + else if([response respondsToSelector:@selector(statusCode)] + && (200 == [(id)response statusCode]) + && data.length > 0) { + //dispatch_async(dispatch_get_main_queue(), ^{ + handler(data); + //}); + } + else { + //dispatch_async(dispatch_get_main_queue(), ^{ + handler(nil); + //}); + } + }]; + [task resume]; +} + + @end diff --git a/MacPass/MPIconSelectViewController.m b/MacPass/MPIconSelectViewController.m index 82563ab3..940f8952 100644 --- a/MacPass/MPIconSelectViewController.m +++ b/MacPass/MPIconSelectViewController.m @@ -117,48 +117,27 @@ typedef NS_ENUM(NSInteger, MPIconDownloadStatus) { } NSURL *url = [NSURL URLWithString:URLString]; - if(!url) { - self.downloadStatus = MPIconDownloadStatusError; - return; - } - - NSString *urlString = [NSString stringWithFormat:@"%@://%@/favicon.ico", url.scheme, url.host ? url.host : @""]; - NSURL *favIconURL = [NSURL URLWithString:urlString]; - if(!favIconURL) { - self.downloadStatus = MPIconDownloadStatusError; - return; - } - KPKMetaData *metaData = ((MPDocument *)[NSDocumentController sharedDocumentController].currentDocument).tree.metaData; - NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:favIconURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { - if(error) { - dispatch_async(dispatch_get_main_queue(), ^{ - self.downloadStatus = MPIconDownloadStatusError; - //[NSApp presentError:error]; - }); - } - if([response respondsToSelector:@selector(statusCode)] && - (200 == [(id)response statusCode]) - && data.length > 0) { - dispatch_async(dispatch_get_main_queue(), ^{ - KPKIcon *newIcon = [[KPKIcon alloc] initWithImageData:data]; - if(newIcon && newIcon.image) { - self.downloadStatus = MPIconDownloadStatusNone; - [metaData addCustomIcon:newIcon]; - [self _updateCollectionViewContent]; - } - else { - self.downloadStatus = MPIconDownloadStatusError; - } - }); - } - else { + + [MPIconHelper fetchIconDataForURL:url completionHandler:^(NSData *iconData) { + if(!iconData || iconData.length == 0) { dispatch_async(dispatch_get_main_queue(), ^{ self.downloadStatus = MPIconDownloadStatusError; }); + return; } + dispatch_async(dispatch_get_main_queue(), ^{ + KPKIcon *newIcon = [[KPKIcon alloc] initWithImageData:iconData]; + if(newIcon && newIcon.image) { + self.downloadStatus = MPIconDownloadStatusNone; + [metaData addCustomIcon:newIcon]; + [self _updateCollectionViewContent]; + } + else { + self.downloadStatus = MPIconDownloadStatusError; + } + }); }]; - [task resume]; } - (void)deleteIcon:(id)sender {