From 8103f5fd4339a6295a6dbec7e77b1f712ef58254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 21 Dec 2016 00:36:04 +0100 Subject: [PATCH] Replaced progress delegate with a block --- Sparkle/SUAutomaticUpdateDriver.h | 2 +- Sparkle/SUBasicUpdateDriver.h | 2 +- Sparkle/SUBasicUpdateDriver.m | 3 ++- Sparkle/SUBinaryDeltaUnarchiver.m | 4 ++-- Sparkle/SUDiskImageUnarchiver.m | 4 ++-- Sparkle/SUPipedUnarchiver.m | 6 +++--- Sparkle/SUUIBasedUpdateDriver.h | 2 +- Sparkle/SUUnarchiver.h | 9 +-------- Sparkle/SUUnarchiver.m | 14 ++++++++------ Sparkle/SUUnarchiver_Private.h | 2 +- Tests/SUUnarchiverTest.swift | 11 +++-------- 11 files changed, 25 insertions(+), 34 deletions(-) diff --git a/Sparkle/SUAutomaticUpdateDriver.h b/Sparkle/SUAutomaticUpdateDriver.h index 4dd90e8f..4af54a11 100644 --- a/Sparkle/SUAutomaticUpdateDriver.h +++ b/Sparkle/SUAutomaticUpdateDriver.h @@ -13,7 +13,7 @@ #import "SUBasicUpdateDriver.h" #import "SUAutomaticUpdateAlert.h" -@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver +@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver @end diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 002d82e2..1dd8f367 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 76ea355c..6ee0aadd 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -335,7 +335,6 @@ if (!success) { [self unarchiverDidFail:nil]; } else { - unarchiver.delegate = self; [unarchiver unarchiveWithCompletionBlock:^(NSError *err){ if (err) { [self unarchiverDidFail:err]; @@ -344,6 +343,8 @@ assert(self.updateItem); [self installWithToolAndRelaunch:YES]; + } progressBlock:^(__unused double progress){ + }]; } } diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index f45cd26d..415c9033 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -79,9 +79,9 @@ } } -- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { - [super unarchiveWithCompletionBlock:block]; + [super unarchiveWithCompletionBlock:block progressBlock:progress]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self applyBinaryDelta]; }); diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index e731e9f3..ae1987ef 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -159,9 +159,9 @@ } } -- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { - [super unarchiveWithCompletionBlock:block]; + [super unarchiveWithCompletionBlock:block progressBlock:progress]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self extractDMG]; }); diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index 320d150d..40739497 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -46,9 +46,9 @@ return NO; } -- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { - [super unarchiveWithCompletionBlock:block]; + [super unarchiveWithCompletionBlock:block progressBlock:progress]; [NSThread detachNewThreadSelector:[[self class] selectorConformingToTypeOfPath:self.archivePath] toTarget:self withObject:nil]; } @@ -94,7 +94,7 @@ bytesRead += len; [archiveOutput writeData:data]; dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfProgress:(double)bytesRead / (double)expectedLength]; + [self notifyProgress:(double)bytesRead / (double)expectedLength]; }); } while(bytesRead < expectedLength); diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 8148912b..a511aae9 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -15,7 +15,7 @@ @class SUStatusController; -@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver +@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver - (void)showAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; diff --git a/Sparkle/SUUnarchiver.h b/Sparkle/SUUnarchiver.h index 486a3604..6e38a728 100644 --- a/Sparkle/SUUnarchiver.h +++ b/Sparkle/SUUnarchiver.h @@ -14,27 +14,20 @@ NS_ASSUME_NONNULL_BEGIN @class SUHost; -@protocol SUUnarchiverDelegate; @interface SUUnarchiver : NSObject @property (copy, readonly) NSString *archivePath; @property (copy, readonly) NSString *_Nullable updateHostBundlePath; @property (copy, readonly) NSString *_Nullable decryptionPassword; -@property (weak) _Nullable id delegate; + (nullable SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)host withPassword:(nullable NSString *)decryptionPassword; + (BOOL)unsafeIfArchiveIsNotValidated; -- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError *_Nullable))block; +- (void)unarchiveWithCompletionBlock:(void (^)(NSError *_Nullable))completion progressBlock:(void (^_Nullable)(double progress))progress; @end -@protocol SUUnarchiverDelegate -@optional -- (void)unarchiver:(SUUnarchiver *)unarchiver extractedProgress:(double)progress; -@end - NS_ASSUME_NONNULL_END #endif diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index d719a8bd..1f146c3f 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -17,15 +17,16 @@ @interface SUUnarchiver () @property (strong) void (^completionBlock)(NSError * _Nullable); +@property (strong) void (^progressBlock)(double progress); + @end @implementation SUUnarchiver @synthesize archivePath; @synthesize updateHostBundlePath; -@synthesize delegate; @synthesize decryptionPassword; -@synthesize completionBlock; +@synthesize completionBlock, progressBlock; + (SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(NSString *)hostPath withPassword:(NSString *)decryptionPassword { @@ -39,8 +40,9 @@ - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], self.archivePath]; } -- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))block { +- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))block progressBlock:(void (^_Nullable)(double progress))progress { self.completionBlock = block; + self.progressBlock = progress; } - (instancetype)initWithPath:(NSString *)path hostBundlePath:(NSString *)hostPath password:(NSString *)password @@ -64,10 +66,10 @@ return NO; } -- (void)notifyDelegateOfProgress:(double)progress +- (void)notifyProgress:(double)progress { - if ([self.delegate respondsToSelector:@selector(unarchiver:extractedProgress:)]) { - [self.delegate unarchiver:self extractedProgress:progress]; + if (self.progressBlock) { + self.progressBlock(progress); } } diff --git a/Sparkle/SUUnarchiver_Private.h b/Sparkle/SUUnarchiver_Private.h index c7718e9f..b48792b9 100644 --- a/Sparkle/SUUnarchiver_Private.h +++ b/Sparkle/SUUnarchiver_Private.h @@ -18,7 +18,7 @@ + (BOOL)canUnarchivePath:(NSString *)path; - (instancetype)initWithPath:(NSString *)archive hostBundlePath:(NSString *)host; -- (void)notifyDelegateOfProgress:(double)progress; +- (void)notifyProgress:(double)progress; - (void)unarchiverDidFinish; - (void)unarchiverDidFailWithError:(NSError *)err; @end diff --git a/Tests/SUUnarchiverTest.swift b/Tests/SUUnarchiverTest.swift index c2261077..3e1d55ad 100644 --- a/Tests/SUUnarchiverTest.swift +++ b/Tests/SUUnarchiverTest.swift @@ -8,16 +8,12 @@ import XCTest -class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate +class SUUnarchiverTest: XCTestCase { var password: String? = nil var unarchivedExpectation: XCTestExpectation? = nil var unarchivedResult: Bool = false - func unarchiver(_ unarchiver: SUUnarchiver, extractedProgress progress: Double) - { - } - func unarchiveTestAppWithExtension(_ archiveExtension: String) { let appName = "SparkleTestCodeSignApp" @@ -39,11 +35,10 @@ class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate let unarchiver = SUUnarchiver(forPath: tempArchiveURL.path, updatingHostBundlePath: nil, withPassword: self.password)! - unarchiver.delegate = self - unarchiver.unarchive {(error: Error?) -> Void in + unarchiver.unarchive(completionBlock: {(error: Error?) -> Void in self.unarchivedResult = nil == error; self.unarchivedExpectation!.fulfill() - } + }, progressBlock:{(progress: Double) -> Void in }); super.waitForExpectations(timeout: 7.0, handler: nil)