From c5671c63a6d0c6a9d4e4a3f3c9489e64f6cc7b5c Mon Sep 17 00:00:00 2001 From: Johnny Sheeley Date: Mon, 21 Jan 2019 22:47:14 -0800 Subject: [PATCH] Taking a swag at #104 --- .../Extensions/NSUserDefaults+Keys.h | 6 +++ .../Extensions/NSUserDefaults+Keys.m | 15 +++++++ KeepingYouAwake/KYAAppController.m | 41 ++++++++++++++++++ .../Base.lproj/Preferences.storyboard | 42 ++++++++++++++----- 4 files changed, 93 insertions(+), 11 deletions(-) diff --git a/KeepingYouAwake/Extensions/NSUserDefaults+Keys.h b/KeepingYouAwake/Extensions/NSUserDefaults+Keys.h index e1fd130e..d2a590da 100644 --- a/KeepingYouAwake/Extensions/NSUserDefaults+Keys.h +++ b/KeepingYouAwake/Extensions/NSUserDefaults+Keys.h @@ -18,6 +18,7 @@ FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisable FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyBatteryCapacityThresholdEnabled; FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyBatteryCapacityThreshold; FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled; +FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyDisableWhenScreenLockedEnabled; @interface NSUserDefaults (Keys) @@ -58,6 +59,11 @@ FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled; */ @property (nonatomic, getter = kya_arePreReleaseUpdatesEnabled) BOOL kya_preReleaseUpdatesEnabled; +/** + Returns YES if KYA should disable itself when the screen locks + */ +@property (nonatomic, getter = kya_isDisableWhenScreenLockedEnabled) BOOL kya_disableWhenScreenLockedEnabled; + @end NS_ASSUME_NONNULL_END diff --git a/KeepingYouAwake/Extensions/NSUserDefaults+Keys.m b/KeepingYouAwake/Extensions/NSUserDefaults+Keys.m index 5e575d5b..da3251f2 100644 --- a/KeepingYouAwake/Extensions/NSUserDefaults+Keys.m +++ b/KeepingYouAwake/Extensions/NSUserDefaults+Keys.m @@ -18,11 +18,14 @@ NSString * const KYAUserDefaultsKeyBatteryCapacityThreshold = @"info.marcel-dierkes.KeepingYouAwake.BatteryCapacityThreshold"; NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled = @"info.marcel-dierkes.KeepingYouAwake.PreReleaseUpdatesEnabled"; +NSString * const KYAUserDefaultsKeyDisableWhenScreenLockedEnabled = @"info.marcel-dierkes.KeepingYouAwake.DisableWhenScreenLockedEnabled"; + @implementation NSUserDefaults (Keys) @dynamic kya_activateOnLaunch, kya_defaultTimeInterval, kya_notificationsEnabled; @dynamic kya_menuBarIconHighlightDisabled; @dynamic kya_batteryCapacityThresholdEnabled, kya_batteryCapacityThreshold; @dynamic kya_preReleaseUpdatesEnabled; +@dynamic kya_disableWhenScreenLockedEnabled; #pragma mark - Activate on Launch @@ -107,4 +110,16 @@ - (void)setKya_preReleaseUpdatesEnabled:(BOOL)preReleaseUpdatesEnabled [self setBool:preReleaseUpdatesEnabled forKey:KYAUserDefaultsKeyPreReleaseUpdatesEnabled]; } +#pragma mark - Disable when screen locks + +- (BOOL)kya_isDisableWhenScreenLockedEnabled +{ + return [self boolForKey:KYAUserDefaultsKeyDisableWhenScreenLockedEnabled]; +} + +- (void)setKya_disableWhenScreenLockedEnabled:(BOOL)disableOnScreenLock +{ + [self setBool:disableOnScreenLock forKey:KYAUserDefaultsKeyDisableWhenScreenLockedEnabled]; +} + @end diff --git a/KeepingYouAwake/KYAAppController.m b/KeepingYouAwake/KYAAppController.m index 9e2aad8f..eda0e0be 100644 --- a/KeepingYouAwake/KYAAppController.m +++ b/KeepingYouAwake/KYAAppController.m @@ -57,6 +57,7 @@ - (instancetype)init [self configureBatteryStatus]; [self configureEventHandler]; + [self configureScreenLockEvents]; } return self; } @@ -66,6 +67,46 @@ - (void)dealloc NSNotificationCenter *center = NSNotificationCenter.defaultCenter; [center removeObserver:self name:NSApplicationDidFinishLaunchingNotification object:nil]; [center removeObserver:self name:kKYABatteryCapacityThresholdDidChangeNotification object:nil]; + + + NSDistributedNotificationCenter *distCenter = [NSDistributedNotificationCenter defaultCenter]; + [distCenter removeObserver:self name:@"com.apple.screenIsLocked" object:nil]; + [distCenter removeObserver:self name:@"com.apple.screenIsUnlocked" object:nil]; +} + +- (void)configureScreenLockEvents +{ + NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter]; + [center addObserver:self + selector:@selector(screenLocked) + name:@"com.apple.screenIsLocked" + object:nil]; + + [center addObserver:self + selector:@selector(screenUnlocked) + name:@"com.apple.screenIsUnlocked" + object:nil]; +} + +- (void) screenLocked +{ + if(![NSUserDefaults.standardUserDefaults kya_disableWhenScreenLockedEnabled]) + { + return + } + // if active, disable & store state + +} + +- (void) screenUnlocked +{ + if(![NSUserDefaults.standardUserDefaults kya_disableWhenScreenLockedEnabled]) + { + return + } + + // if was active, restore state + } - (void)awakeFromNib diff --git a/KeepingYouAwake/Preferences/Base.lproj/Preferences.storyboard b/KeepingYouAwake/Preferences/Base.lproj/Preferences.storyboard index b5f6e6bb..8d5a748b 100644 --- a/KeepingYouAwake/Preferences/Base.lproj/Preferences.storyboard +++ b/KeepingYouAwake/Preferences/Base.lproj/Preferences.storyboard @@ -63,11 +63,11 @@ - + - + + + @@ -137,11 +147,20 @@ + + + + + + + + + @@ -161,6 +180,7 @@ + @@ -172,7 +192,7 @@ - +