Skip to content

Commit fdcb091

Browse files
committed
Factored +calculateUpgradeState out of -init. Magic stuff in -init leads to confusion. Also, now this method can be invoked during mid-app run if desired. I use this now in BookMacster to “Reset and Start Over” to a “New User” state.
1 parent 1d2b8cb commit fdcb091

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

SSYAppInfo.h

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <Cocoa/Cocoa.h>
22
#import "SSYVersionTriplet.h"
33

4+
extern NSString* const constKeyLastVersion ;
45

56
/*!
67
@enum SSYAppInfoUpgradeState
@@ -35,18 +36,7 @@ enum SSYAppInfoUpgradeState {
3536
previous launch. I'm not sure where I'm going with this class right now. Might
3637
add other "features" later.
3738
38-
@details Upon initialization, if found to be running a version that is different
39-
than the previous version in standard user defaults' constKeyLastVersion, an
40-
instance of this class (the shared instance, for instance) will overwrite the
41-
constKeyLastVersion user default with the current version. This should be done
42-
early in the application launch to make sure that this information gets updated.
43-
It should be done at least once during an application launch in order to register
44-
constKeyLastVersion to provide accurate info during the ^next^ launch of this
45-
application.
46-
That being said, this is now a reason for using an instance with instance variables. The
47-
ivars _previousVersionNumbers and consequently _upgradeState can no longer be computed
48-
from data stored on disk (in user defaults) after this overwriting has been done.
49-
39+
@details
5040
Regarding version strings, this class uses what I call "SSYSimpleVersioningSystem" instead
5141
of Apple Generic Versioning. In SSYSimpleVersioningSystem, CFBundleVersion contains two-dot
5242
string of three integers, major.minor.bugfix. So, for example in the About box your
@@ -86,5 +76,24 @@ enum SSYAppInfoUpgradeState {
8676

8777
+ (SSYVersionTriplet*)previousVersionTriplet ;
8878

79+
/*
80+
@brief Sets the current upgrade state in the receiver's shared instance,
81+
and updates the constKeyLastVersion in the user defaults to the current
82+
version found in the app's main bundle.
83+
84+
@details This method must be invoked before you invoke any other methods
85+
in this class, such as early in -applicaitonDidFinishLaunching, or else
86+
other methods will give wrong answers.
87+
88+
The current upgrade state is calculated by comparing the
89+
last-run version found in standard user defaults' constKeyLastVersion
90+
with the current version found in the main bundle, and remembers it.
91+
92+
The reason for setting the current upgrade state (it's an instance variable)
93+
is that, after the user defaults' constKeyLastVersion has been updated to the
94+
current version, it can no longer be calculated.
95+
*/
96+
+ (void)calculateUpgradeState ;
97+
8998
@end
9099

SSYAppInfo.m

+26-26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ @implementation SSYAppInfo
1010
+ (SSYAppInfo*)sharedInfo {
1111
@synchronized(self) {
1212
if (sharedInfo == nil) {
13-
sharedInfo = [[self alloc] init] ;
13+
sharedInfo = [[self alloc] init] ;
14+
[sharedInfo setUpgradeState:SSYNotDefined] ;
1415
}
1516
}
1617
return sharedInfo ;
@@ -76,31 +77,26 @@ + (SSYVersionTriplet*)rawCurrentVersionTriplet {
7677
return cvt ;
7778
}
7879

79-
80-
- (id)init {
81-
self = [super init] ;
82-
if (self != nil) {
83-
SSYVersionTriplet* previousVersionTriplet = [SSYAppInfo rawPreviousVersionTriplet] ;
84-
[self setPreviousVersionTriplet:previousVersionTriplet] ;
85-
[self setCurrentVersionTriplet:[SSYAppInfo rawCurrentVersionTriplet]] ;
86-
enum SSYAppInfoUpgradeState upgradeState = [SSYAppInfo upgradeStateForCurrentVersionTriplet:[self currentVersionTriplet]
87-
previousVersionTriplet:[self previousVersionTriplet]] ;
88-
[self setUpgradeState:upgradeState] ;
89-
if ([self upgradeState] != SSYCurrentRev) {
90-
// Get a nice, clean, filtered versionString of the form "major.minor.bugFix" using SSVersionTriplet methods
91-
SSYVersionTriplet* currentVersionTriplet = [SSYVersionTriplet versionTripletFromBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] ;
92-
NSString* currentVersionString = [currentVersionTriplet string] ;
93-
94-
// Record currently-launched version into prefs
95-
// Note that this must be done AFTER the above reading,
96-
// or else the pref constKeyLastVersion will be written as the previous version!
97-
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults] ;
98-
[userDefaults setObject:currentVersionString
99-
forKey:constKeyLastVersion] ;
100-
[userDefaults synchronize] ;
101-
}
102-
}
103-
return self;
80+
- (void)calculateUpgradeState {
81+
SSYVersionTriplet* previousVersionTriplet = [SSYAppInfo rawPreviousVersionTriplet] ;
82+
[self setPreviousVersionTriplet:previousVersionTriplet] ;
83+
[self setCurrentVersionTriplet:[SSYAppInfo rawCurrentVersionTriplet]] ;
84+
enum SSYAppInfoUpgradeState upgradeState = [SSYAppInfo upgradeStateForCurrentVersionTriplet:[self currentVersionTriplet]
85+
previousVersionTriplet:[self previousVersionTriplet]] ;
86+
[self setUpgradeState:upgradeState] ;
87+
if ([self upgradeState] != SSYCurrentRev) {
88+
// Get a nice, clean, filtered versionString of the form "major.minor.bugFix" using SSVersionTriplet methods
89+
SSYVersionTriplet* currentVersionTriplet = [SSYVersionTriplet versionTripletFromBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]] ;
90+
NSString* currentVersionString = [currentVersionTriplet string] ;
91+
92+
// Record currently-launched version into prefs
93+
// Note that this must be done AFTER the above reading,
94+
// or else the pref constKeyLastVersion will be written as the previous version!
95+
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults] ;
96+
[userDefaults setObject:currentVersionString
97+
forKey:constKeyLastVersion] ;
98+
[userDefaults synchronize] ;
99+
}
104100
}
105101

106102

@@ -122,5 +118,9 @@ + (SSYVersionTriplet*)previousVersionTriplet {
122118
return [[self sharedInfo] previousVersionTriplet] ;
123119
}
124120

121+
+ (void)calculateUpgradeState {
122+
[[self sharedInfo] calculateUpgradeState] ;
123+
}
124+
125125

126126
@end

0 commit comments

Comments
 (0)