Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 217 additions & 13 deletions GammaTest.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

25 changes: 9 additions & 16 deletions GammaTest/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "AppDelegate.h"
#import "MainViewController.h"
#import "GammaController.h"

#import "NSUserDefaults+Group.h"

typedef NS_ENUM(NSInteger, GammaAction) {
GammaActionNone,
Expand Down Expand Up @@ -59,20 +59,9 @@ - (void)updateShortCutItem {
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval:900]; //Wake up every 15 minutes at minimum

[[NSUserDefaults standardUserDefaults] registerDefaults:@{
@"enabled": @NO,
@"maxOrange": [NSNumber numberWithFloat:0.7],
@"colorChangingEnabled": @YES,
@"lastAutoChangeDate": [NSDate distantPast],
@"autoStartHour": @19,
@"autoStartMinute": @0,
@"autoEndHour": @7,
@"autoEndMinute": @0,
@"updateUI":@YES,
@"colorChangingLocationLatitude": @0,
@"colorChangingLocationLongitude": @0,
@"colorChangingLocationEnabled": @NO
}];
NSString *defaultsPath = [[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"];
NSDictionary *appDefaults = [NSDictionary dictionaryWithContentsOfFile:defaultsPath];
[[NSUserDefaults groupDefaults] registerDefaults:appDefaults];

if ([application respondsToSelector:@selector(shortcutItems)] &&
!application.shortcutItems.count) {
Expand All @@ -84,6 +73,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSUserDefaults groupDefaults] setBool:YES forKey:@"updateUI"];
}

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"App woke with fetch request");

Expand All @@ -108,7 +101,7 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction
}
} else {
//gammathingy://orangeness/switch
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"enabled"]) {
if ([[NSUserDefaults groupDefaults] boolForKey:@"enabled"]) {
[GammaController disableOrangeness];
} else {
[GammaController enableOrangeness];
Expand Down
30 changes: 30 additions & 0 deletions GammaTest/Defaults.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>enabled</key>
<false/>
<key>maxOrange</key>
<real>0.7</real>
<key>colorChangingEnabled</key>
<false/>
<key>lastAutoChangeDate</key>
<date>1-01-01T00:07:33Z</date>
<key>autoStartHour</key>
<integer>19</integer>
<key>autoStartMinute</key>
<integer>0</integer>
<key>autoEndHour</key>
<integer>7</integer>
<key>autoEndMinute</key>
<integer>0</integer>
<key>updateUI</key>
<true/>
<key>colorChangingLocationLatitude</key>
<integer>0</integer>
<key>colorChangingLocationLongitude</key>
<integer>0</integer>
<key>colorChangingLocationEnabled</key>
<false/>
</dict>
</plist>
10 changes: 10 additions & 0 deletions GammaTest/GammaTest.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.me.ahammer.GammaTest</string>
</array>
</dict>
</plist>
10 changes: 6 additions & 4 deletions GammaTest/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required once in order to provide location based screen temperature</string>
<key>AppGroupIdentifier</key>
<string>$(APP_GROUP_IDENTIFIER)</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>Gamma Thingy</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand Down Expand Up @@ -35,6 +37,8 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required once in order to provide location based screen temperature</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
Expand All @@ -49,8 +53,6 @@
<array>
<string>armv7</string>
</array>
<key>CFBundleDisplayName</key>
<string>Gamma Thingy</string>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
Expand Down
76 changes: 46 additions & 30 deletions GammaTest/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "MainViewController.h"
#import "GammaController.h"
#import "NSUserDefaults+Group.h"

@interface MainViewController ()

Expand Down Expand Up @@ -80,7 +81,7 @@ - (void)viewDidLoad {
}

- (void)updateUI {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults* defaults = [NSUserDefaults groupDefaults];

enabledSwitch.on = [defaults boolForKey:@"enabled"];
orangeSlider.value = [defaults floatForKey:@"maxOrange"];
Expand All @@ -95,38 +96,43 @@ - (void)updateUI {

- (IBAction)enabledSwitchChanged:(UISwitch *)sender {
NSLog(@"enabled: %lu",(unsigned long)sender.on);
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"updateUI"];
NSUserDefaults* defaults = [NSUserDefaults groupDefaults];
[defaults setBool:NO forKey:@"updateUI"];

if (sender.on) {
[GammaController setGammaWithTransitionFrom:0 to:[[NSUserDefaults standardUserDefaults] floatForKey:@"maxOrange"]];
[GammaController enableOrangeness];
} else {
[GammaController setGammaWithTransitionFrom:[[NSUserDefaults standardUserDefaults] floatForKey:@"maxOrange"] to:0];
[GammaController disableOrangeness];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"colorChangingLocationEnabled"]) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"colorChangingLocationEnabled"];
if ([defaults boolForKey:@"colorChangingLocationEnabled"]) {
[defaults setBool:NO forKey:@"colorChangingLocationEnabled"];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"colorChangingLocationEnabled"]) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"colorChangingEnabled"];
if ([defaults boolForKey:@"colorChangingLocationEnabled"]) {
[defaults setBool:NO forKey:@"colorChangingEnabled"];
}


[[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:@"enabled"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"updateUI"];
[defaults setBool:YES forKey:@"updateUI"];
[defaults synchronize];
}

- (IBAction)maxOrangeSliderChanged:(UISlider *)sender {
NSLog(@"maxOrange: %f",sender.value);
[[NSUserDefaults standardUserDefaults] setFloat:sender.value forKey:@"maxOrange"];
NSUserDefaults *defaults = [NSUserDefaults groupDefaults];
[defaults setFloat:sender.value forKey:@"maxOrange"];

if (enabledSwitch.on)
if (enabledSwitch.on) {
[GammaController setGammaWithOrangeness:sender.value];
}

[defaults synchronize];
}

- (IBAction)colorChangingEnabledSwitchChanged:(UISwitch *)sender {
NSLog(@"colorChangingEnabled: %lu",(unsigned long)sender.on);
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"updateUI"];
[[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:@"colorChangingEnabled"];
[[NSUserDefaults standardUserDefaults] setObject:[NSDate distantPast] forKey:@"lastAutoChangeDate"];
NSUserDefaults *defaults = [NSUserDefaults groupDefaults];
[defaults setBool:NO forKey:@"updateUI"];
[defaults setBool:sender.on forKey:@"colorChangingEnabled"];
[defaults setObject:[NSDate distantPast] forKey:@"lastAutoChangeDate"];
NSLog(@"color changing switch changed");

if(sender.on) {
Expand All @@ -137,16 +143,20 @@ - (IBAction)colorChangingEnabledSwitchChanged:(UISwitch *)sender {
// Make the time fields full opacity.
for(UITableViewCell *cell in timeBasedInputCells)
[[cell contentView] setAlpha: 1];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"colorChangingLocationEnabled"];
[[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:@"colorChangingEnabled"];
[defaults setBool:NO forKey:@"colorChangingLocationEnabled"];
[defaults setBool:sender.on forKey:@"colorChangingEnabled"];
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"updateUI"];

[defaults setBool:YES forKey:@"updateUI"];
[GammaController autoChangeOrangenessIfNeeded];
[defaults synchronize];
}

- (IBAction)colorChangingLocationSwitchValueChanged:(UISwitch *)sender {
NSUserDefaults *defaults = [NSUserDefaults groupDefaults];

if(sender.on) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"updateUI"];
[defaults setBool:NO forKey:@"updateUI"];
BOOL requestedLocationAuthorization = NO;

if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined) {
Expand All @@ -167,17 +177,17 @@ - (IBAction)colorChangingLocationSwitchValueChanged:(UISwitch *)sender {
CGFloat latitude = self.locationManager.location.coordinate.latitude;
CGFloat longitude = self.locationManager.location.coordinate.longitude;
if (latitude != 0 && longitude != 0) { // make sure the location is available
[[NSUserDefaults standardUserDefaults] setFloat:latitude forKey:@"colorChangingLocationLatitude"];
[[NSUserDefaults standardUserDefaults] setFloat:longitude forKey:@"colorChangingLocationLongitude"];
[defaults setFloat:latitude forKey:@"colorChangingLocationLatitude"];
[defaults setFloat:longitude forKey:@"colorChangingLocationLongitude"];
}

[colorChangingEnabledSwitch setOn:NO animated:YES];

for(UITableViewCell *cell in timeBasedInputCells)
[[cell contentView] setAlpha: .6];

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"colorChangingLocationEnabled"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"colorChangingEnabled"];
[defaults setBool:YES forKey:@"colorChangingLocationEnabled"];
[defaults setBool:NO forKey:@"colorChangingEnabled"];

} else if(!requestedLocationAuthorization) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No access to location"
Expand All @@ -188,17 +198,21 @@ - (IBAction)colorChangingLocationSwitchValueChanged:(UISwitch *)sender {
[alert show];
[sender setOn:NO animated:YES];
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"updateUI"];
[defaults setBool:YES forKey:@"updateUI"];
[GammaController autoChangeOrangenessIfNeeded];
} else {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"colorChangingLocationEnabled"];
[defaults setBool:NO forKey:@"colorChangingLocationEnabled"];
}

[defaults synchronize];
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusDenied) {
[colorChangingLocationBasedSwitch setOn:NO animated:YES];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"colorChangingLocationEnabled"];
NSUserDefaults *defaults = [NSUserDefaults groupDefaults];
[defaults setBool:NO forKey:@"colorChangingLocationEnabled"];
[defaults synchronize];
} else if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// revaluate the UISwitch status
[self colorChangingLocationSwitchValueChanged: colorChangingLocationBasedSwitch];
Expand Down Expand Up @@ -238,16 +252,17 @@ - (void)timePickerValueChanged:(UIDatePicker*)picker {
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:picker.date];
currentField.text = [timeFormatter stringFromDate:picker.date];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [NSUserDefaults groupDefaults];
[defaults setInteger:components.hour forKey:[defaultsKeyPrefix stringByAppendingString:@"Hour"]];
[defaults setInteger:components.minute forKey:[defaultsKeyPrefix stringByAppendingString:@"Minute"]];

[defaults setObject:[NSDate distantPast] forKey:@"lastAutoChangeDate"];
[GammaController autoChangeOrangenessIfNeeded];
[defaults synchronize];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [NSUserDefaults groupDefaults];
NSDate *date = nil;
if (textField == startTimeTextField) {
date = [self dateForHour:[defaults integerForKey:@"autoStartHour"] andMinute:[defaults integerForKey:@"autoStartMinute"]];
Expand All @@ -267,8 +282,9 @@ - (NSDate*)dateForHour:(NSInteger)hour andMinute:(NSInteger)minute{
}

- (void)userDefaultsChanged:(NSNotification *)notification {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"updateUI"])
if ([[NSUserDefaults groupDefaults] boolForKey:@"updateUI"]) {
[self updateUI];
}
}

@end
19 changes: 19 additions & 0 deletions GammaTest/extentions/NSUserDefaults+Group.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NSUserDefaults+Group.h
// GammaTest
//
// Created by Arthur Hammer on 28.10.15.
// Copyright © 2015 Thomas Finch. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSUserDefaults (Group)

// Returns a new NSUserDefaults object for app groups to share defaults in multiple targets.
// The suit name is the bundles' "AppGroupIdentifier" key.
//
// (Note: For now, this returns a new instance on every call.)
+ (NSUserDefaults *)groupDefaults;

@end
18 changes: 18 additions & 0 deletions GammaTest/extentions/NSUserDefaults+Group.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// NSUserDefaults+Group.m
// GammaTest
//
// Created by Arthur Hammer on 28.10.15.
// Copyright © 2015 Thomas Finch. All rights reserved.
//

#import "NSUserDefaults+Group.h"

@implementation NSUserDefaults (Group)

+ (NSUserDefaults *)groupDefaults {
NSString *suitName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AppGroupIdentifier"];
return [[NSUserDefaults alloc] initWithSuiteName:suitName];
}

@end
Loading