From 28065e7240f96f87553ea5d36a82b9775814541b Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 26 Jun 2021 11:34:39 -0700 Subject: [PATCH] Refine SPUStandardUpdaterController API * Make updater/userDriver properties non-null * Expose starting the updater and allowing it to be done later instead of at instantiation (so clients in some cases will not need to make their controller declared as an optional) * Make sender in checkForUpdates: nullable --- Sparkle/SPUStandardUpdaterController.h | 29 +++++++------ Sparkle/SPUStandardUpdaterController.m | 59 +++++++++----------------- Sparkle/SUUpdater.h | 2 +- 3 files changed, 37 insertions(+), 53 deletions(-) diff --git a/Sparkle/SPUStandardUpdaterController.h b/Sparkle/SPUStandardUpdaterController.h index ee43d7d0..2974973e 100644 --- a/Sparkle/SPUStandardUpdaterController.h +++ b/Sparkle/SPUStandardUpdaterController.h @@ -41,35 +41,35 @@ SU_EXPORT @interface SPUStandardUpdaterController : NSObject /*! Interface builder outlet for the updater's delegate. - This property should only be set using Interface Builder by creating a connection using the outlet + 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 + 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 loaded from the nib. - You may access this property after your application has finished launching, or after your window controller has finished loading. + When instantiated from a nib, don't perform update checks before the application has finished launching in a MainMenu nib (i.e applicationDidFinishLaunching:) or before the corresponding window/view controller has been loaded (i.e, windowDidLoad or viewDidLoad). The updater is not guaranteed to be started yet before these points. */ -@property (nonatomic, readonly, nullable) SPUUpdater *updater; +@property (nonatomic, readonly) SPUUpdater *updater; /*! Accessible property for the updater's user driver. - - This is nil before being loaded from the nib. - You may access this property after your application has finished launching, or after your window controller has finished loading. */ -@property (nonatomic, readonly, nullable) SPUStandardUserDriver *userDriver; +@property (nonatomic, readonly) SPUStandardUserDriver *userDriver; /*! - Use initWithUpdaterDelegate:userDriverDelegate: or initWithStartingUpdater:updaterDelegate:userDriverDelegate: instead. + Create a new SPUStandardUpdaterController from a nib. + + You cannot call this initializer directly. You must instantiate a SPUStandardUpdaterController inside of a nib (typically the MainMenu nib) to use it. + + To create a SPUStandardUpdaterController programatically, use initWithUpdaterDelegate:userDriverDelegate: or initWithStartingUpdater:updaterDelegate:userDriverDelegate: instead. */ - (instancetype)init NS_UNAVAILABLE; @@ -81,7 +81,7 @@ SU_EXPORT @interface SPUStandardUpdaterController : NSObject - (instancetype)initWithUpdaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate; /*! - Create a new SPUStandardUpdaterController programmatically. + Create a new SPUStandardUpdaterController programmatically allowing you to specify whether or not to start the updater immediately. You can specify whether or not you want to start the updater immediately. If you do not start the updater, you must invoke -[SPUStandardUpdaterController startUpdater] at a later time to start it. @@ -91,9 +91,10 @@ SU_EXPORT @interface SPUStandardUpdaterController : NSObject /*! Starts the updater if it has not already been started. - You should only call this method yourself if you chose not to start the updater on initialization. + You should only call this method yourself if you opted out of starting the updater on initialization. + Hence, do not call this yourself if you are instantiating this controller from a nib. - This invokes -[SPUUpdater startUpdater:]. If an error is returned, the error is logged and an alert is shown to the user (after a few seconds) to contact the developer. + This invokes -[SPUUpdater startUpdater:]. If the application is misconfigured with Sparkle, an error is logged and an alert is shown to the user (after a few seconds) to contact the developer. If you want more control over this behavior, you can create your own SPUUpdater instead. */ - (void)startUpdater; @@ -108,7 +109,7 @@ SU_EXPORT @interface SPUStandardUpdaterController : NSObject This action checks updates by invoking -[SPUUpdater checkForUpdates] */ -- (IBAction)checkForUpdates:(id)sender; +- (IBAction)checkForUpdates:(nullable id)sender; /*! Validates if the menu item for checkForUpdates: can be invoked or not diff --git a/Sparkle/SPUStandardUpdaterController.m b/Sparkle/SPUStandardUpdaterController.m index 6537b599..7d137e32 100644 --- a/Sparkle/SPUStandardUpdaterController.m +++ b/Sparkle/SPUStandardUpdaterController.m @@ -17,30 +17,32 @@ #import "SULocalizations.h" #import -static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaultsObservationContext"; - -@interface SPUStandardUpdaterController () - -@property (nonatomic) SPUUpdater *updater; -@property (nonatomic) SPUStandardUserDriver *userDriver; -@property (nonatomic) BOOL initializedUpdater; - -@end - @implementation SPUStandardUpdaterController @synthesize updater = _updater; @synthesize userDriver = _userDriver; @synthesize updaterDelegate = _updaterDelegate; @synthesize userDriverDelegate = _userDriverDelegate; -@synthesize initializedUpdater = _initializedUpdater; + +- (instancetype)init +{ + // Updater will be later started in -awakeFromNib + // Our updater delegate and user driver delegate outlets will be connected by then too. + // For now we can make sure the updater/userDriver are accessible + return [self initWithStartingUpdater:NO updaterDelegate:nil userDriverDelegate:nil]; +} - (void)awakeFromNib { - // awakeFromNib might be called more than once; guard against that + // Note: awakeFromNib might be called more than once but -startUpdate handles 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, so we don't have a proper "did load" point - [self initializeUpdaterAndStartUpdater:YES]; + [self startUpdater]; +} + +- (instancetype)initWithUpdaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate +{ + return [self initWithStartingUpdater:YES updaterDelegate:updaterDelegate userDriverDelegate:userDriverDelegate]; } - (instancetype)initWithStartingUpdater:(BOOL)startUpdater updaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate @@ -49,36 +51,17 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults _updaterDelegate = updaterDelegate; _userDriverDelegate = userDriverDelegate; - [self initializeUpdaterAndStartUpdater:startUpdater]; - } - return self; -} - -- (instancetype)initWithUpdaterDelegate:(nullable id)updaterDelegate userDriverDelegate:(nullable id)userDriverDelegate -{ - if ((self = [super init])) { - _updaterDelegate = updaterDelegate; - _userDriverDelegate = userDriverDelegate; - - [self initializeUpdaterAndStartUpdater:YES]; - } - return self; -} - -- (void)initializeUpdaterAndStartUpdater:(BOOL)startUpdater -{ - if (!self.initializedUpdater) { - self.initializedUpdater = YES; - NSBundle *hostBundle = [NSBundle mainBundle]; - SPUStandardUserDriver *userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:self.userDriverDelegate]; - self.updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:hostBundle userDriver:userDriver delegate:self.updaterDelegate]; - self.userDriver = userDriver; + SPUStandardUserDriver *userDriver = [[SPUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:userDriverDelegate]; + + _updater = [[SPUUpdater alloc] initWithHostBundle:hostBundle applicationBundle:hostBundle userDriver:userDriver delegate:updaterDelegate]; + _userDriver = userDriver; if (startUpdater) { [self startUpdater]; } } + return self; } - (void)startUpdater @@ -97,7 +80,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults } } -- (IBAction)checkForUpdates:(id)__unused sender +- (IBAction)checkForUpdates:(nullable id)__unused sender { [self.updater checkForUpdates]; } diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index f173c4b6..9116e862 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -34,7 +34,7 @@ Note: This class is now deprecated and acts as a thin wrapper around SPUUpdater and SPUStandardUserDriver */ -__deprecated_msg("Deprecated in Sparkle 2. Use SPUStandardUpdaterController or SPUUpdater instead") +__deprecated_msg("Deprecated in Sparkle 2. Use SPUStandardUpdaterController instead, or SPUUpdater if you need more control.") SU_EXPORT @interface SUUpdater : NSObject @property (unsafe_unretained, nonatomic) IBOutlet id delegate;