From 353a5635e1884f58ec1f592473f01edacdf9244d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Sat, 25 Apr 2015 21:08:35 +0100 Subject: [PATCH] Removed appcast delegate --- Sparkle/SUAppcast.h | 12 ++-------- Sparkle/SUAppcast.m | 32 +++++++++++++-------------- Sparkle/SUAutomaticUpdateAlert.h | 2 -- Sparkle/SUBasicUpdateDriver.h | 2 +- Sparkle/SUBasicUpdateDriver.m | 14 ++++++------ Sparkle/SUUIBasedUpdateDriver.h | 1 + Sparkle/SUUserInitiatedUpdateDriver.m | 10 --------- 7 files changed, 27 insertions(+), 46 deletions(-) diff --git a/Sparkle/SUAppcast.h b/Sparkle/SUAppcast.h index 9b4c677b..9e9acd5d 100644 --- a/Sparkle/SUAppcast.h +++ b/Sparkle/SUAppcast.h @@ -12,23 +12,15 @@ #import #import "SUExport.h" -@protocol SUAppcastDelegate; - @class SUAppcastItem; -SU_EXPORT @interface SUAppcast : NSObject +SU_EXPORT @interface SUAppcast : NSObject -@property (weak) id delegate; @property (copy) NSString *userAgentString; @property (copy) NSDictionary *httpHeaders; -- (void)fetchAppcastFromURL:(NSURL *)url; +- (void)fetchAppcastFromURL:(NSURL *)url completionBlock:(void (^)(NSError *))err; @property (readonly, copy) NSArray *items; @end -@protocol SUAppcastDelegate -- (void)appcastDidFinishLoading:(SUAppcast *)appcast; -- (void)appcast:(SUAppcast *)appcast failedToLoadWithError:(NSError *)error; -@end - #endif diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index a075200e..98d2e899 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -33,6 +33,7 @@ @end @interface SUAppcast () +@property (strong) void (^completionBlock)(NSError *); @property (copy) NSString *downloadFilename; @property (strong) NSURLDownload *download; @property (copy) NSArray *items; @@ -43,14 +44,16 @@ @implementation SUAppcast @synthesize downloadFilename; -@synthesize delegate; +@synthesize completionBlock; @synthesize userAgentString; @synthesize httpHeaders; @synthesize download; @synthesize items; -- (void)fetchAppcastFromURL:(NSURL *)url +- (void)fetchAppcastFromURL:(NSURL *)url completionBlock:(void (^)(NSError *))block { + self.completionBlock = block; + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; if (self.userAgentString) { [request setValue:self.userAgentString forHTTPHeaderField:@"User-Agent"]; @@ -210,27 +213,23 @@ } } - if ([appcastItems count]) - { + if ([appcastItems count]) { NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; [appcastItems sortUsingDescriptors:@[sort]]; self.items = appcastItems; } - if (failed) - { + if (failed) { [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while parsing the update feed.", nil) }]]; - } - else if ([self.delegate respondsToSelector:@selector(appcastDidFinishLoading:)]) - { - [self.delegate appcastDidFinishLoading:self]; + } else { + self.completionBlock(nil); + self.completionBlock = nil; } } - (void)download:(NSURLDownload *)__unused aDownload didFailWithError:(NSError *)error { - if (self.downloadFilename) - { + if (self.downloadFilename) { [[NSFileManager defaultManager] removeItemAtPath:self.downloadFilename error:nil]; } self.downloadFilename = nil; @@ -245,10 +244,11 @@ - (void)reportError:(NSError *)error { - if ([self.delegate respondsToSelector:@selector(appcast:failedToLoadWithError:)]) - { - [self.delegate appcast:self failedToLoadWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastError userInfo:@{NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil), NSLocalizedFailureReasonErrorKey: [error localizedDescription]}]]; - } + self.completionBlock([NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastError userInfo:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil), + NSLocalizedFailureReasonErrorKey: [error localizedDescription] + }]); + self.completionBlock = nil; } - (NSXMLNode *)bestNodeInNodes:(NSArray *)nodes diff --git a/Sparkle/SUAutomaticUpdateAlert.h b/Sparkle/SUAutomaticUpdateAlert.h index be0f7f6d..1fcfcfb7 100644 --- a/Sparkle/SUAutomaticUpdateAlert.h +++ b/Sparkle/SUAutomaticUpdateAlert.h @@ -11,8 +11,6 @@ #import "SUWindowController.h" -@protocol SUAutomaticUpdateAlertDelegate; - typedef NS_ENUM(NSInteger, SUAutomaticInstallationChoice) { SUInstallNowChoice, SUInstallLaterChoice, diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 32e320bc..fee63ede 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -15,7 +15,7 @@ #import "SUAppcast.h" @class SUAppcastItem, SUHost; -@interface SUBasicUpdateDriver : SUUpdateDriver +@interface SUBasicUpdateDriver : SUUpdateDriver @property (strong, readonly) SUAppcastItem *updateItem; @property (strong, readonly) NSURLDownload *download; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index dc34f12f..9ae856d6 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -54,10 +54,15 @@ SUAppcast *appcast = [[SUAppcast alloc] init]; - [appcast setDelegate:self]; [appcast setUserAgentString:[self.updater userAgentString]]; [appcast setHttpHeaders:[self.updater httpHeaders]]; - [appcast fetchAppcastFromURL:URL]; + [appcast fetchAppcastFromURL:URL completionBlock:^(NSError *error) { + if (error) { + [self abortUpdateWithError:error]; + } else { + [self appcastDidFinishLoading:appcast]; + } + }]; } - (id)versionComparator @@ -153,11 +158,6 @@ } } -- (void)appcast:(SUAppcast *)__unused ac failedToLoadWithError:(NSError *)error -{ - [self abortUpdateWithError:error]; -} - - (void)didFindValidUpdate { assert(self.updateItem); diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 65f37443..990446d7 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -20,6 +20,7 @@ - (void)showModalAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; - (void)installAndRestart:(id)sender; +- (void)appcastDidFinishLoading:(SUAppcast *)ac; @end diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index 5dd66f29..d1e0c9a6 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -78,16 +78,6 @@ [super abortUpdate]; } -- (void)appcast:(SUAppcast *)ac failedToLoadWithError:(NSError *)error -{ - if (self.isCanceled) - { - [self abortUpdate]; - return; - } - [super appcast:ac failedToLoadWithError:error]; -} - - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { // We don't check to see if this update's been skipped, because the user explicitly *asked* if he had the latest version.