Skip to content

Commit 8317398

Browse files
committed
DRY up the magic strings in preferences.
1 parent cc763ac commit 8317398

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

Switch/NNPreferencesService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#import <Foundation/Foundation.h>
1616

17+
1718
@interface NNPreferencesService : NNService
1819

1920
- (void)showPreferencesWindow:(id)sender;

Switch/NNPreferencesService.m

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#import "NNPreferencesWindowController.h"
2020

2121

22+
static NSString *kNNFirstLaunchKey = @"firstLaunch";
23+
24+
2225
@interface NNPreferencesService ()
2326

2427
@property (nonatomic, strong) NNPreferencesWindowController *preferencesWindowController;
@@ -28,6 +31,8 @@ @interface NNPreferencesService ()
2831

2932
@implementation NNPreferencesService
3033

34+
#pragma mark - NNService
35+
3136
- (NNServiceType)serviceType;
3237
{
3338
return NNServiceTypePersistent;
@@ -36,20 +41,21 @@ - (NNServiceType)serviceType;
3641
- (void)startService;
3742
{
3843
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
39-
#pragma message "Keys (and default values) should be DRYed up a bit more."
40-
NSDictionary *userDefaultsValues = @{ @"firstLaunch" : @YES };
41-
[defaults registerDefaults:userDefaultsValues];
44+
[defaults registerDefaults:self._defaultValues];
4245

4346
# if DEBUG
4447
{
4548
BOOL resetDefaults = NO;
4649

4750
if (resetDefaults) {
48-
[defaults removeObjectForKey:@"firstLaunch"];
51+
for (NSString *key in self._defaultValues.allKeys) {
52+
[defaults removeObjectForKey:key];
53+
}
4954
}
5055
}
5156
# endif
5257

58+
// TODO: When these become customizable, they will become fields in the app's preferences.
5359
NNEventManager *keyManager = [NNEventManager sharedManager];
5460
[keyManager registerHotKey:[NNHotKey hotKeyWithKeycode:kVK_Tab modifiers:NNHotKeyModifierOption] forEvent:NNEventManagerEventTypeInvoke];
5561
[keyManager registerHotKey:[NNHotKey hotKeyWithKeycode:kVK_Tab modifiers:(NNHotKeyModifierOption | NNHotKeyModifierShift)] forEvent:NNEventManagerEventTypeDecrement];
@@ -58,19 +64,37 @@ - (void)startService;
5864
[keyManager registerHotKey:[NNHotKey hotKeyWithKeycode:kVK_ANSI_Comma modifiers:NNHotKeyModifierOption] forEvent:NNEventManagerEventTypeShowPreferences];
5965

6066
self.preferencesWindowController = [[NNPreferencesWindowController alloc] initWithWindowNibName:@"NNPreferencesWindowController"];
61-
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
62-
[self.preferencesWindowController showWindow:self];
63-
[defaults setBool:NO forKey:@"firstLaunch"];
67+
if ([[NSUserDefaults standardUserDefaults] boolForKey:kNNFirstLaunchKey]) {
68+
[defaults setBool:NO forKey:kNNFirstLaunchKey];
69+
dispatch_async(dispatch_get_main_queue(), ^{
70+
[self showPreferencesWindow:self];
71+
});
6472
}
6573

6674
[defaults synchronize];
6775
}
6876

77+
#pragma mark - NNPreferencesService
78+
6979
- (void)showPreferencesWindow:(id)sender;
7080
{
7181
[self.preferencesWindowController showWindow:sender];
7282
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
7383
[self.preferencesWindowController.window makeKeyAndOrderFront:sender];
7484
}
7585

86+
#pragma mark Private
87+
88+
- (NSDictionary *)_defaultValues;
89+
{
90+
static NSDictionary *_defaultValues = nil;
91+
static dispatch_once_t onceToken;
92+
dispatch_once(&onceToken, ^{
93+
_defaultValues = @{
94+
kNNFirstLaunchKey : @YES,
95+
};
96+
});
97+
return _defaultValues;
98+
}
99+
76100
@end

0 commit comments

Comments
 (0)