/
HBPreferences.m
62 lines (45 loc) · 1.45 KB
/
HBPreferences.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#import "HBPreferences.h"
#import <notify.h>
#define PREFS_PATH [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/com.eswick.harbor.plist"]
/* Dictionary tools */
/* ======== */
static NSDictionary* defaults() {
return @{
#define PREF(TYPE, NAME, DEFAULT, LABEL, CELL_TYPE, ...) @#NAME : DEFAULT,
#include "Preferences.def"
};
}
@implementation HBPreferences
#define PREF(TYPE, NAME, DEFAULT, LABEL, CELL_TYPE, ...) \
- (TYPE)get##NAME { \
return self.dictionary[@#NAME] ? self.dictionary[@#NAME] : defaults()[@#NAME]; \
} \
\
- (void)set##NAME:(TYPE)newValue { \
self.dictionary[@#NAME] = newValue; \
[self.dictionary writeToFile:PREFS_PATH atomically:true]; \
notify_post("com.eswick.harbor.preferences_changed"); \
}
#include "Preferences.def"
- (NSMutableDictionary*)dictionary {
if (!_dictionary)
self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:PREFS_PATH];
return _dictionary;
}
+ (id)sharedInstance {
static HBPreferences *_prefs;
if (_prefs == nil) {
_prefs = [[self alloc] init];
if (![[NSFileManager defaultManager] fileExistsAtPath:PREFS_PATH]) {
/* Write defaults */
[defaults() writeToFile:PREFS_PATH atomically:true];
}
int token, status;
status = notify_register_dispatch("com.eswick.harbor.preferences_changed", &token,
dispatch_get_main_queue(), ^(int t) {
[_prefs setDictionary:nil];
});
}
return _prefs;
}
@end