Skip to content

Commit f9779a3

Browse files
committed
Make settings persistent
1 parent 669c876 commit f9779a3

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

Lumen.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
F802B9ED1AD81FB700FCFD65 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F802B9EC1AD81FB700FCFD65 /* Images.xcassets */; };
1313
F802B9F01AD81FB700FCFD65 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F802B9EE1AD81FB700FCFD65 /* MainMenu.xib */; };
1414
F802B9FC1AD81FB700FCFD65 /* LumenTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F802B9FB1AD81FB700FCFD65 /* LumenTests.m */; };
15+
F80722861E6BD17F000C3ECE /* NSArray+Functional.m in Sources */ = {isa = PBXBuildFile; fileRef = F80722851E6BD17F000C3ECE /* NSArray+Functional.m */; };
1516
F825A7A81E6B984300106611 /* stats.m in Sources */ = {isa = PBXBuildFile; fileRef = F825A7A71E6B984300106611 /* stats.m */; };
1617
F83E30F71AD8252D0098C5CC /* BrightnessController.m in Sources */ = {isa = PBXBuildFile; fileRef = F83E30F61AD8252D0098C5CC /* BrightnessController.m */; };
1718
F83E30FB1AD836F10098C5CC /* util.c in Sources */ = {isa = PBXBuildFile; fileRef = F83E30F91AD836F10098C5CC /* util.c */; };
@@ -40,6 +41,8 @@
4041
F802B9F51AD81FB700FCFD65 /* LumenTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LumenTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4142
F802B9FA1AD81FB700FCFD65 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4243
F802B9FB1AD81FB700FCFD65 /* LumenTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LumenTests.m; sourceTree = "<group>"; };
44+
F80722841E6BD17F000C3ECE /* NSArray+Functional.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Functional.h"; sourceTree = "<group>"; };
45+
F80722851E6BD17F000C3ECE /* NSArray+Functional.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Functional.m"; sourceTree = "<group>"; };
4346
F825A7A71E6B984300106611 /* stats.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = stats.m; sourceTree = "<group>"; };
4447
F825A7A91E6B984C00106611 /* stats.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = stats.h; sourceTree = "<group>"; };
4548
F83E30F41AD823640098C5CC /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = "<group>"; };
@@ -99,6 +102,8 @@
99102
F83E30F61AD8252D0098C5CC /* BrightnessController.m */,
100103
F8833AE21D419803006FD2B0 /* Model.h */,
101104
F8833AE31D419803006FD2B0 /* Model.m */,
105+
F80722841E6BD17F000C3ECE /* NSArray+Functional.h */,
106+
F80722851E6BD17F000C3ECE /* NSArray+Functional.m */,
102107
F83E30F41AD823640098C5CC /* Constants.h */,
103108
F83E30F91AD836F10098C5CC /* util.c */,
104109
F83E30FA1AD836F10098C5CC /* util.h */,
@@ -240,6 +245,7 @@
240245
F83E30FB1AD836F10098C5CC /* util.c in Sources */,
241246
F83E30F71AD8252D0098C5CC /* BrightnessController.m in Sources */,
242247
F825A7A81E6B984300106611 /* stats.m in Sources */,
248+
F80722861E6BD17F000C3ECE /* NSArray+Functional.m in Sources */,
243249
F802B9E91AD81FB700FCFD65 /* AppDelegate.m in Sources */,
244250
F8833AE41D419803006FD2B0 /* Model.m in Sources */,
245251
);

Lumen/Constants.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#define TELEMETRY_SALT (@"com.anishathalye.lumen")
1616
#define TELEMETRY_INTERVAL (1 * 24 * 60 * 60) // seconds
1717

18+
#define DEFAULTS_CALIBRATION_POINTS (@"calibrationPoints")
19+
1820
#define TICK_INTERVAL (0.5) // seconds
1921
#define MIN_X_SPACING (10.0) // absolute difference in L* coordinate
2022
#define CHANGE_NOTICE (0.01) // difference in screen brightness level

Lumen/Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0.0</string>
20+
<string>1.1.0</string>
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleVersion</key>
24-
<string>1.0.0</string>
24+
<string>1.1.0</string>
2525
<key>LSMinimumSystemVersion</key>
2626
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
2727
<key>LSUIElement</key>

Lumen/Model.m

+41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#import "Model.h"
55
#import "Constants.h"
66
#import "util.h"
7+
#import "NSArray+Functional.h"
78

89
@interface XYPoint : NSObject
910

@@ -25,6 +26,26 @@ - (id)initWithX:(float)x andY:(float)y {
2526
return self;
2627
}
2728

29+
- (NSDictionary *)asDictionary {
30+
return @{@"x": [NSNumber numberWithFloat:self.x],
31+
@"y": [NSNumber numberWithFloat:self.y]};
32+
}
33+
34+
- (id)initWithDictionary:(NSDictionary *)dictionary {
35+
self = [super init];
36+
if (self && [dictionary isKindOfClass:[NSDictionary class]]) {
37+
NSNumber *x = [dictionary objectForKey:@"x"];
38+
if ([x isKindOfClass:[NSNumber class]]) {
39+
self.x = [x floatValue];
40+
}
41+
NSNumber *y = [dictionary objectForKey:@"y"];
42+
if ([y isKindOfClass:[NSNumber class]]) {
43+
self.y = [y floatValue];
44+
}
45+
}
46+
return self;
47+
}
48+
2849
@end
2950

3051
@interface Model ()
@@ -39,6 +60,7 @@ - (id)init {
3960
self = [super init];
4061
if (self) {
4162
self.points = [NSMutableArray new];
63+
[self restoreDefaults];
4264
}
4365
return self;
4466
}
@@ -85,6 +107,7 @@ - (void)observeOutput:(float)output forInput:(float)input {
85107
}
86108
}
87109
[self.points removeObjectsAtIndexes:toDelete];
110+
[self synchronizeDefaults];
88111
}
89112

90113
- (float)predictFromInput:(float)input {
@@ -127,4 +150,22 @@ - (float)predictFromInput:(float)input {
127150
*/
128151
}
129152

153+
- (void)restoreDefaults {
154+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
155+
NSArray *points = [[defaults arrayForKey:DEFAULTS_CALIBRATION_POINTS] map:^(NSDictionary *point) {
156+
return [[XYPoint alloc] initWithDictionary:point];
157+
}];
158+
if (points) {
159+
self.points = [points mutableCopy];
160+
}
161+
}
162+
163+
- (void)synchronizeDefaults {
164+
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
165+
NSArray *encoded = [self.points map:^(XYPoint *point) {
166+
return [point asDictionary];
167+
}];
168+
[defaults setObject:encoded forKey:DEFAULTS_CALIBRATION_POINTS];
169+
}
170+
130171
@end

Lumen/NSArray+Functional.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2015-2017 Anish Athalye ([email protected])
2+
// Released under GPLv3. See the included LICENSE.txt for details
3+
4+
#import <Foundation/Foundation.h>
5+
6+
@interface NSArray (Functional)
7+
8+
NS_ASSUME_NONNULL_BEGIN
9+
10+
- (NSArray *)map:(id (^)(id x))function;
11+
12+
NS_ASSUME_NONNULL_END
13+
14+
@end

Lumen/NSArray+Functional.m

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2015-2017 Anish Athalye ([email protected])
2+
// Released under GPLv3. See the included LICENSE.txt for details
3+
4+
#import "NSArray+Functional.h"
5+
6+
@implementation NSArray (Functional)
7+
8+
- (NSArray *)map:(id (^)(id x))function {
9+
NSMutableArray *array = [NSMutableArray new];
10+
for (id value in self) {
11+
[array addObject:function(value)];
12+
}
13+
return array;
14+
}
15+
16+
@end

0 commit comments

Comments
 (0)