Permalink
Browse files

Make settings persistent

  • Loading branch information...
1 parent 669c876 commit f9779a3f3aa454a0c39e75e908b0cc843a9800ed @anishathalye committed Mar 5, 2017
Showing with 81 additions and 2 deletions.
  1. +6 −0 Lumen.xcodeproj/project.pbxproj
  2. +2 −0 Lumen/Constants.h
  3. +2 −2 Lumen/Info.plist
  4. +41 −0 Lumen/Model.m
  5. +14 −0 Lumen/NSArray+Functional.h
  6. +16 −0 Lumen/NSArray+Functional.m
@@ -12,6 +12,7 @@
F802B9ED1AD81FB700FCFD65 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F802B9EC1AD81FB700FCFD65 /* Images.xcassets */; };
F802B9F01AD81FB700FCFD65 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F802B9EE1AD81FB700FCFD65 /* MainMenu.xib */; };
F802B9FC1AD81FB700FCFD65 /* LumenTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F802B9FB1AD81FB700FCFD65 /* LumenTests.m */; };
+ F80722861E6BD17F000C3ECE /* NSArray+Functional.m in Sources */ = {isa = PBXBuildFile; fileRef = F80722851E6BD17F000C3ECE /* NSArray+Functional.m */; };
F825A7A81E6B984300106611 /* stats.m in Sources */ = {isa = PBXBuildFile; fileRef = F825A7A71E6B984300106611 /* stats.m */; };
F83E30F71AD8252D0098C5CC /* BrightnessController.m in Sources */ = {isa = PBXBuildFile; fileRef = F83E30F61AD8252D0098C5CC /* BrightnessController.m */; };
F83E30FB1AD836F10098C5CC /* util.c in Sources */ = {isa = PBXBuildFile; fileRef = F83E30F91AD836F10098C5CC /* util.c */; };
@@ -40,6 +41,8 @@
F802B9F51AD81FB700FCFD65 /* LumenTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = LumenTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
F802B9FA1AD81FB700FCFD65 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
F802B9FB1AD81FB700FCFD65 /* LumenTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LumenTests.m; sourceTree = "<group>"; };
+ F80722841E6BD17F000C3ECE /* NSArray+Functional.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+Functional.h"; sourceTree = "<group>"; };
+ F80722851E6BD17F000C3ECE /* NSArray+Functional.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+Functional.m"; sourceTree = "<group>"; };
F825A7A71E6B984300106611 /* stats.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = stats.m; sourceTree = "<group>"; };
F825A7A91E6B984C00106611 /* stats.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = stats.h; sourceTree = "<group>"; };
F83E30F41AD823640098C5CC /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = "<group>"; };
@@ -99,6 +102,8 @@
F83E30F61AD8252D0098C5CC /* BrightnessController.m */,
F8833AE21D419803006FD2B0 /* Model.h */,
F8833AE31D419803006FD2B0 /* Model.m */,
+ F80722841E6BD17F000C3ECE /* NSArray+Functional.h */,
+ F80722851E6BD17F000C3ECE /* NSArray+Functional.m */,
F83E30F41AD823640098C5CC /* Constants.h */,
F83E30F91AD836F10098C5CC /* util.c */,
F83E30FA1AD836F10098C5CC /* util.h */,
@@ -240,6 +245,7 @@
F83E30FB1AD836F10098C5CC /* util.c in Sources */,
F83E30F71AD8252D0098C5CC /* BrightnessController.m in Sources */,
F825A7A81E6B984300106611 /* stats.m in Sources */,
+ F80722861E6BD17F000C3ECE /* NSArray+Functional.m in Sources */,
F802B9E91AD81FB700FCFD65 /* AppDelegate.m in Sources */,
F8833AE41D419803006FD2B0 /* Model.m in Sources */,
);
View
@@ -15,6 +15,8 @@
#define TELEMETRY_SALT (@"com.anishathalye.lumen")
#define TELEMETRY_INTERVAL (1 * 24 * 60 * 60) // seconds
+#define DEFAULTS_CALIBRATION_POINTS (@"calibrationPoints")
+
#define TICK_INTERVAL (0.5) // seconds
#define MIN_X_SPACING (10.0) // absolute difference in L* coordinate
#define CHANGE_NOTICE (0.01) // difference in screen brightness level
View
@@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
- <string>1.0.0</string>
+ <string>1.1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
- <string>1.0.0</string>
+ <string>1.1.0</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
View
@@ -4,6 +4,7 @@
#import "Model.h"
#import "Constants.h"
#import "util.h"
+#import "NSArray+Functional.h"
@interface XYPoint : NSObject
@@ -25,6 +26,26 @@ - (id)initWithX:(float)x andY:(float)y {
return self;
}
+- (NSDictionary *)asDictionary {
+ return @{@"x": [NSNumber numberWithFloat:self.x],
+ @"y": [NSNumber numberWithFloat:self.y]};
+}
+
+- (id)initWithDictionary:(NSDictionary *)dictionary {
+ self = [super init];
+ if (self && [dictionary isKindOfClass:[NSDictionary class]]) {
+ NSNumber *x = [dictionary objectForKey:@"x"];
+ if ([x isKindOfClass:[NSNumber class]]) {
+ self.x = [x floatValue];
+ }
+ NSNumber *y = [dictionary objectForKey:@"y"];
+ if ([y isKindOfClass:[NSNumber class]]) {
+ self.y = [y floatValue];
+ }
+ }
+ return self;
+}
+
@end
@interface Model ()
@@ -39,6 +60,7 @@ - (id)init {
self = [super init];
if (self) {
self.points = [NSMutableArray new];
+ [self restoreDefaults];
}
return self;
}
@@ -85,6 +107,7 @@ - (void)observeOutput:(float)output forInput:(float)input {
}
}
[self.points removeObjectsAtIndexes:toDelete];
+ [self synchronizeDefaults];
}
- (float)predictFromInput:(float)input {
@@ -127,4 +150,22 @@ - (float)predictFromInput:(float)input {
*/
}
+- (void)restoreDefaults {
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+ NSArray *points = [[defaults arrayForKey:DEFAULTS_CALIBRATION_POINTS] map:^(NSDictionary *point) {
+ return [[XYPoint alloc] initWithDictionary:point];
+ }];
+ if (points) {
+ self.points = [points mutableCopy];
+ }
+}
+
+- (void)synchronizeDefaults {
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+ NSArray *encoded = [self.points map:^(XYPoint *point) {
+ return [point asDictionary];
+ }];
+ [defaults setObject:encoded forKey:DEFAULTS_CALIBRATION_POINTS];
+}
+
@end
View
@@ -0,0 +1,14 @@
+// Copyright (c) 2015-2017 Anish Athalye (me@anishathalye.com)
+// Released under GPLv3. See the included LICENSE.txt for details
+
+#import <Foundation/Foundation.h>
+
+@interface NSArray (Functional)
+
+NS_ASSUME_NONNULL_BEGIN
+
+- (NSArray *)map:(id (^)(id x))function;
+
+NS_ASSUME_NONNULL_END
+
+@end
View
@@ -0,0 +1,16 @@
+// Copyright (c) 2015-2017 Anish Athalye (me@anishathalye.com)
+// Released under GPLv3. See the included LICENSE.txt for details
+
+#import "NSArray+Functional.h"
+
+@implementation NSArray (Functional)
+
+- (NSArray *)map:(id (^)(id x))function {
+ NSMutableArray *array = [NSMutableArray new];
+ for (id value in self) {
+ [array addObject:function(value)];
+ }
+ return array;
+}
+
+@end

0 comments on commit f9779a3

Please sign in to comment.