From 00368011c8f3638600c12aee59e67cba8ca36457 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 6 Mar 2016 23:38:17 -0500 Subject: [PATCH] Add delegate outlets to SUStandardUpdaterController This adds delegate outlets to the updater and to the user driver, so a developer can hook either of them up. Note we now initialize the updater and user driver after being awoken from the nib. We also took this chance to make the delegates to the updater and user driver read only. This means that the delegates have to be available on initialization, which is good because changing delegates or setting delegates too late (and missing messages) is bad. --- Sparkle/SUStandardUpdaterController.h | 39 ++++++++++++++++++++------ Sparkle/SUStandardUpdaterController.m | 34 +++++++++++++++++++--- Sparkle/SUStandardUserDriverProtocol.h | 2 +- Sparkle/SUUpdater.h | 2 +- 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/Sparkle/SUStandardUpdaterController.h b/Sparkle/SUStandardUpdaterController.h index 32a3df8d..204fdc52 100644 --- a/Sparkle/SUStandardUpdaterController.h +++ b/Sparkle/SUStandardUpdaterController.h @@ -11,14 +11,16 @@ NS_ASSUME_NONNULL_BEGIN @class SUUpdater; -@protocol SUStandardUserDriver; +@protocol SUUserDriver, SUStandardUserDriver, SUUpdaterDelegate, SUStandardUserDriverDelegate; /*! A controller class that instantiates a SUUpdater and allows binding UI to it. - This controller's updater targets the application's main bundle, and uses Sparkle's standard user interface. - Thus it is only suitable under these circumstances. Typically, this class is used by sticking it as a custom NSObject subclass in an Interface Builder nib (probably in MainMenu). - The controller creates an SUUpdater instance and allows hooking up the check for updates action and menu item validation. + This class is meant to be instantiated in a nib. When doing so, the controller's updater target's the application's main bundle, + and uses Sparkle's standard user interface. Typically, this class is used by sticking it as a custom NSObject subclass in an Interface Builder nib (probably in MainMenu). + + The controller creates an SUUpdater instance and allows hooking up the check for updates action and menu item validation. It also allows hooking + up the updater's and user driver's delegates. This controller class may not be valuable to you if: * You want to control or defer the instantiation of an SUUpdater, or don't want to be tied into a nib's instantiation, or don't want to use a nib @@ -30,20 +32,39 @@ NS_ASSUME_NONNULL_BEGIN @interface SUStandardUpdaterController : NSObject /*! - Initializes a new updater controller instance. Typically this class is instantiated in a nib, thus this - method is not usually called directly by the developer. + Initializes a new updater controller instance using a provided updater and user driver. + + Typically this class is instantiated in a nib, thus this method is not usually used. */ -- (instancetype)init; +- (instancetype)initWithUpdater:(SUUpdater *)updater userDriver:(id)userDriver; + +/*! + Interface builder outlet for the updater's delegate. + + This property should only be set using Interface Builder by creating a connection using the outlet + */ +@property (nonatomic, weak, nullable) IBOutlet id updaterDelegate; + +/*! + Interface builder outlet for the user driver's delegate. + + This property should only be set using Interface Builder by creating a connection using the outlet + */ +@property (nonatomic, weak, nullable) IBOutlet id userDriverDelegate; /*! Accessible property for the updater. Some properties on the updater can be binded via KVO + + This is nil before being awoken from a nib */ -@property (nonatomic, readonly) SUUpdater *updater; +@property (nonatomic, readonly, nullable) SUUpdater *updater; /*! Accessible property for the updater's user driver. + + This is nil before being awoken from a nib */ -@property (nonatomic, readonly) id userDriver; +@property (nonatomic, readonly, nullable) id userDriver; /*! Explicitly checks for updates and displays a progress dialog while doing so. diff --git a/Sparkle/SUStandardUpdaterController.m b/Sparkle/SUStandardUpdaterController.m index bc4e704b..b19656eb 100644 --- a/Sparkle/SUStandardUpdaterController.m +++ b/Sparkle/SUStandardUpdaterController.m @@ -20,6 +20,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults @property (nonatomic) SUUpdater *updater; @property (nonatomic) id userDriver; +@property (nonatomic) BOOL awokeFromNib; @end @@ -27,20 +28,45 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults @synthesize updater = _updater; @synthesize userDriver = _userDriver; +@synthesize updaterDelegate = _updaterDelegate; +@synthesize userDriverDelegate = _userDriverDelegate; +@synthesize awokeFromNib = _awokeFromNib; +- (instancetype)initWithUpdater:(SUUpdater *)updater userDriver:(id)userDriver +{ + self = [self init]; // note: call our own init here rather than super's + if (self != nil) { + self.updater = updater; + self.userDriver = userDriver; + } + return self; +} + +// Invoked from being instantiated in a nib - (instancetype)init { self = [super init]; if (self != nil) { + // Register as an observer here to match unregistering in -dealloc [self registerAsObserver]; + } + return self; +} + +- (void)awakeFromNib +{ + // awakeFromNib might be called more than once; guard against that + // We have to use awakeFromNib otherwise the delegate outlets may not be connected yet, + // and we aren't a proper window or view controller or anything like that + if (!self.awokeFromNib) { + self.awokeFromNib = YES; NSBundle *hostBundle = [NSBundle mainBundle]; - id userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:nil]; - _updater = [[SUUpdater alloc] initWithHostBundle:hostBundle userDriver:userDriver delegate:nil]; - _userDriver = userDriver; + id userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:self.userDriverDelegate]; + self.updater = [[SUUpdater alloc] initWithHostBundle:hostBundle userDriver:userDriver delegate:self.updaterDelegate]; + self.userDriver = userDriver; } - return self; } - (void)dealloc diff --git a/Sparkle/SUStandardUserDriverProtocol.h b/Sparkle/SUStandardUserDriverProtocol.h index f796dca6..e7e96a80 100644 --- a/Sparkle/SUStandardUserDriverProtocol.h +++ b/Sparkle/SUStandardUserDriverProtocol.h @@ -20,7 +20,7 @@ /*! Readable and writable property for this user driver's delegate */ -@property (nonatomic, weak, nullable) id delegate; +@property (nonatomic, weak, nullable, readonly) id delegate; /*! Indicates whether or not an update is in progress as far as the user's perspective is concerned diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index bff2c12b..f3cf47bc 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -45,7 +45,7 @@ SU_EXPORT @interface SUUpdater : NSObject */ - (instancetype)initWithHostBundle:(NSBundle *)hostBundle userDriver:(id )userDriver delegate:(id )delegate; -@property (weak) id delegate; +@property (weak, readonly) id delegate; @property (nonatomic, readonly) id userDriver; @property (readonly, strong) NSBundle *hostBundle;