Skip to content

Commit

Permalink
Merge pull request #164 from newmarcel/v1.6.1/user-notifications
Browse files Browse the repository at this point in the history
Added Support for User Notifications
  • Loading branch information
newmarcel committed Apr 10, 2021
2 parents dcb6895 + a9a8f08 commit b663d23
Show file tree
Hide file tree
Showing 78 changed files with 1,497 additions and 750 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### v1.6.1 ###

- added support for notifications, use `System Preferences` to manage notification settings ([#164](https://github.com/newmarcel/KeepingYouAwake/pull/164))
- please note, this feature is only available on macOS 11 or newer; the previous experimental notifications support has been removed

### v1.6.0 ###

- raised minimum deployment target to macOS Sierra ([#142](https://github.com/newmarcel/KeepingYouAwake/pull/142))
Expand Down
6 changes: 4 additions & 2 deletions KYAKit/Extensions/NSDate+RemainingTime.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ - (NSDateComponentsFormatter *)dateComponentsFormatter

- (NSString *)kya_localizedRemainingTime
{
return [[self dateComponentsFormatter] stringFromDate:[NSDate date] toDate:self];
Auto startDate = [NSDate dateWithTimeIntervalSinceNow:-1];
return [[self dateComponentsFormatter] stringFromDate:startDate toDate:self];
}

- (NSString *)kya_localizedRemainingTimeWithoutPhrase
{
Auto formatter = [self dateComponentsFormatter];
formatter.includesTimeRemainingPhrase = NO;
Auto remainingTimeString = [formatter stringFromDate:[NSDate date] toDate:self];
Auto startDate = [NSDate dateWithTimeIntervalSinceNow:-1];
Auto remainingTimeString = [formatter stringFromDate:startDate toDate:self];
formatter.includesTimeRemainingPhrase = YES;

return remainingTimeString;
Expand Down
6 changes: 0 additions & 6 deletions KYAKit/Extensions/NSUserDefaults+Keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ NS_ASSUME_NONNULL_BEGIN

// User Default Keys
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyActivateOnLaunch;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyNotificationsEnabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyDefaultTimeInterval;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyAllowDisplaySleep;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisabled;
Expand All @@ -28,11 +27,6 @@ FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled;
*/
@property (nonatomic, getter = kya_isActivatedOnLaunch) BOOL kya_activateOnLaunch;

/**
Returns YES if user notifications should be displayed.
*/
@property (nonatomic, getter = kya_areNotificationsEnabled) BOOL kya_notificationsEnabled;

/**
Returns the default time interval for the sleep wake timer.
*/
Expand Down
15 changes: 1 addition & 14 deletions KYAKit/Extensions/NSUserDefaults+Keys.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

// User Default Keys
NSString * const KYAUserDefaultsKeyActivateOnLaunch = @"info.marcel-dierkes.KeepingYouAwake.ActivateOnLaunch";
NSString * const KYAUserDefaultsKeyNotificationsEnabled = @"info.marcel-dierkes.KeepingYouAwake.NotificationsEnabled";
NSString * const KYAUserDefaultsKeyDefaultTimeInterval = @"info.marcel-dierkes.KeepingYouAwake.TimeInterval";
NSString * const KYAUserDefaultsKeyAllowDisplaySleep = @"info.marcel-dierkes.KeepingYouAwake.AllowDisplaySleep";
NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisabled = @"info.marcel-dierkes.KeepingYouAwake.MenuBarIconHighlightDisabled";
Expand All @@ -22,7 +21,7 @@
NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled = @"info.marcel-dierkes.KeepingYouAwake.PreReleaseUpdatesEnabled";

@implementation NSUserDefaults (Keys)
@dynamic kya_activateOnLaunch, kya_defaultTimeInterval, kya_notificationsEnabled;
@dynamic kya_activateOnLaunch, kya_defaultTimeInterval;
@dynamic kya_allowDisplaySleep;
@dynamic kya_menuBarIconHighlightDisabled;
@dynamic kya_batteryCapacityThresholdEnabled, kya_batteryCapacityThreshold;
Expand All @@ -41,18 +40,6 @@ - (void)setKya_activateOnLaunch:(BOOL)activateOnLaunch
[self setBool:activateOnLaunch forKey:KYAUserDefaultsKeyActivateOnLaunch];
}

#pragma mark - Notifications Enabled

- (BOOL)kya_areNotificationsEnabled
{
return [self boolForKey:KYAUserDefaultsKeyNotificationsEnabled];
}

- (void)setKya_notificationsEnabled:(BOOL)notificationsEnabled
{
[self setBool:notificationsEnabled forKey:KYAUserDefaultsKeyNotificationsEnabled];
}

#pragma mark - Default Time Interval

- (NSTimeInterval)kya_defaultTimeInterval
Expand Down
3 changes: 3 additions & 0 deletions KYAKit/KYAKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ FOUNDATION_EXPORT const unsigned char KYAKitVersionString[];
#import <KYAKit/KYAEvent.h>
#import <KYAKit/KYAEventHandler.h>
#import <KYAKit/KYASleepWakeTimer.h>
#import <KYAKit/KYAUserNotificationCenter.h>

#import <KYAKit/NSApplication+KYALauncher.h>
#import <KYAKit/NSBundle+KYAUpdateFeed.h>
#import <KYAKit/NSBundle+KYAVersion.h>
#import <KYAKit/NSDate+RemainingTime.h>
#import <KYAKit/NSUserDefaults+Keys.h>
#import <KYAKit/NSURL+KYAUserNotificationCenter.h>
#import <KYAKit/NSWorkspace+KYAUserNotificationCenter.h>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NSURL+KYAUserNotificationCenter.h
// KYAKit
//
// Created by Marcel Dierkes on 20.02.21.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSURL (KYAUserNotificationCenter)
@property (class, nonatomic, readonly) NSURL *kya_notificationPreferencesURL;
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// NSURL+KYAUserNotificationCenter.m
// KYAKit
//
// Created by Marcel Dierkes on 20.02.21.
//

#import "NSURL+KYAUserNotificationCenter.h"
#import "KYADefines.h"

@implementation NSURL (KYAUserNotificationCenter)

+ (NSURL *)kya_notificationPreferencesURL
{
return [self URLWithString:@"x-apple.systempreferences:com.apple.preference.notifications"];
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NSWorkspace+KYAUserNotificationCenter.h
// KYAKit
//
// Created by Marcel Dierkes on 20.02.21.
//

#import <Cocoa/Cocoa.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^KYAOpenNotificationsCompletionHandler)(NSRunningApplication *_Nullable,
NSError *_Nullable);

@interface NSWorkspace (KYAUserNotificationCenter)

/// Opens the notifications pane in System Preferences.
/// @param completionHandler An optional completion handler, called on a private queue
- (void)kya_openNotificationPreferencesWithCompletionHandler:(nullable KYAOpenNotificationsCompletionHandler)completionHandler API_AVAILABLE(macos(11.0));

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// NSWorkspace+KYAUserNotificationCenter.m
// KYAKit
//
// Created by Marcel Dierkes on 20.02.21.
//

#import "NSWorkspace+KYAUserNotificationCenter.h"
#import "KYADefines.h"
#import "NSURL+KYAUserNotificationCenter.h"

@implementation NSWorkspace (KYAUserNotificationCenter)

- (void)kya_openNotificationPreferencesWithCompletionHandler:(KYAOpenNotificationsCompletionHandler)completionHandler
{
Auto prefURL = NSURL.kya_notificationPreferencesURL;
Auto config = [NSWorkspaceOpenConfiguration configuration];
config.addsToRecentItems = NO;

[self openApplicationAtURL:prefURL
configuration:config
completionHandler:^(NSRunningApplication *app, NSError *error) {
if(completionHandler != nil)
{
completionHandler(app, error);
}
}];
}

@end
49 changes: 49 additions & 0 deletions KYAKit/KYAUserNotificationCenter/KYAUserNotificationCenter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// KYAUserNotificationCenter.h
// KYAKit
//
// Created by Marcel Dierkes on 13.02.21.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@class KYAUserNotification;

typedef NS_ENUM(NSUInteger, KYAUserNotificationAuthorizationStatus) {
KYAUserNotificationAuthorizationStatusUndetermined = 0,
KYAUserNotificationAuthorizationStatusGranted,
KYAUserNotificationAuthorizationStatusDenied,
};

typedef void(^KYAUserNotificationsAuthorizationCompletion)(KYAUserNotificationAuthorizationStatus, NSError *_Nullable);

API_AVAILABLE(macos(11.0))
@interface KYAUserNotificationCenter : NSObject
@property (class, nonatomic, readonly) KYAUserNotificationCenter *sharedCenter;

- (void)getAuthorizationStatusWithCompletion:(void(NS_NOESCAPE ^)(KYAUserNotificationAuthorizationStatus))completion;
- (void)requestAuthorizationWithCompletion:(nullable NS_NOESCAPE KYAUserNotificationsAuthorizationCompletion)completion;
- (void)requestAuthorizationIfUndetermined;

- (void)postNotification:(__kindof KYAUserNotification *)notification;
- (void)clearAllDeliveredNotifications;

@end

API_AVAILABLE(macos(11.0))
@interface KYAUserNotification : NSObject
@property (copy, nonatomic) NSString *identifier;
@property (copy, nonatomic, nullable) NSString *title; // use the localized setter instead
@property (copy, nonatomic, nullable) NSString *subtitle; // use the localized setter instead
@property (copy, nonatomic, nullable) NSString *bodyText; // use the localized setter instead
@property (copy, nonatomic, nullable) NSDictionary *userInfo;

- (void)setLocalizedTitleWithKey:(NSString *)key arguments:(nullable NSArray *)arguments;
- (void)setLocalizedSubtitleWithKey:(NSString *)key arguments:(nullable NSArray *)arguments;
- (void)setLocalizedBodyTextWithKey:(NSString *)key arguments:(nullable NSArray *)arguments;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit b663d23

Please sign in to comment.