Moved favIconFetching to MPIconHelper

This commit is contained in:
Michael Starke
2018-08-17 12:19:16 +02:00
parent 8ed936a08d
commit 27d1f512f0
3 changed files with 55 additions and 42 deletions

View File

@@ -144,4 +144,6 @@ typedef NS_ENUM(NSUInteger, MPIconType) {
*/
+ (NSArray *)databaseIconTypes;
+ (void)fetchIconDataForURL:(NSURL *)url completionHandler:(void (^)(NSData *iconData))handler;
@end

View File

@@ -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

View File

@@ -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 {