-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathConfigurationManager.m
80 lines (65 loc) · 2.96 KB
/
ConfigurationManager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// ConfigurationManager.m
// ShowKeys
//
// Created by John Hobbs on 6/25/15.
// Copyright (c) 2015 John Hobbs. All rights reserved.
//
#import "ConfigurationManager.h"
static NSString *const kPreferenceOpacity = @"opacity";
static NSString *const kPreferenceFadeTimeout = @"fade";
static NSString *const kPreferenceFontSize = @"fontSize";
static NSString *const kPreferenceTextColor = @"textColor";
static NSString *const kPreferenceMaxChars = @"maxChars";
@implementation ConfigurationManager
+ (ConfigurationManager *)instance {
static ConfigurationManager *instance;
if(!instance) {
instance = [[ConfigurationManager alloc] initInstance];
}
return instance;
}
- (instancetype)init { return nil; }
- (instancetype)initInstance {
self = [super init];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{
kPreferenceOpacity: @0.8,
kPreferenceFadeTimeout: @1.0,
kPreferenceFontSize: @32,
kPreferenceMaxChars: @32,
kPreferenceTextColor: [NSArchiver archivedDataWithRootObject:[NSColor whiteColor]]
}];
return self;
}
+ (NSSet *)keyPathsForValuesAffectingAddress {
return [NSSet setWithObjects:@"UpdateConfiguration", nil];
}
- (void)reset {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPreferenceOpacity];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPreferenceFadeTimeout];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPreferenceFontSize];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPreferenceMaxChars];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPreferenceTextColor];
[[NSUserDefaults standardUserDefaults] synchronize];
[self load];
}
- (void)load {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
_opacity = [prefs floatForKey:kPreferenceOpacity];
_fadeTimeout = [prefs floatForKey:kPreferenceFadeTimeout];
_fontSize = [prefs integerForKey:kPreferenceFontSize];
_maxChars = [prefs integerForKey:kPreferenceMaxChars];
NSData *data = [prefs objectForKey:kPreferenceTextColor];
_textColor = [NSUnarchiver unarchiveObjectWithData:data];
}
- (void)store {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:_opacity forKey:kPreferenceOpacity];
[prefs setFloat:_fadeTimeout forKey:kPreferenceFadeTimeout];
[prefs setInteger:_fontSize forKey:kPreferenceFontSize];
NSData *data = [NSArchiver archivedDataWithRootObject:_textColor];
[prefs setInteger:_maxChars forKey:kPreferenceMaxChars];
[prefs setObject:data forKey:kPreferenceTextColor];
[prefs synchronize];
}
@end