Skip to content

Commit b1fb062

Browse files
authored
Merge pull request #27 from dataJAR/3.1
Approved
2 parents 29cdec7 + 44ebcd5 commit b1fb062

15 files changed

Lines changed: 178 additions & 212 deletions

File tree

Notifier/.swiftlint.yml

100644100755
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ opt_in_rules: # some rules are only opt-in
88
- trailing_newline
99
- colon
1010
- comma
11-
included: # paths to include during linting. `--path` is ignored if present.
12-
- auto-update
1311
excluded: # paths to ignore during linting. Takes precedence over `included`.
1412
- Pods
1513
- Scripts/CodeSignUpdate.swift
14+
- Payload
1615

1716
# configurable rules can be customized from this configuration file
1817
# binary rules can set their severity level

Notifier/Notifier - Alerts/Structures.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct RootElements: Codable {
6262
struct UserInfo: Codable {
6363
// Optional - action to perform when the message is clicked
6464
var messageAction: [TaskObject]?
65-
// Optional - alert only - action to perform when the message button is clicked
65+
// Optional - action to perform when the message button is clicked
6666
var messageButtonAction: [TaskObject]?
6767
// Arguments for the task object
6868
struct TaskObject: Codable {

Notifier/Notifier - Alerts/UserNotifications.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@ func processNotificationActions(userInfoKey: String, userInfo: [AnyHashable: Any
259259
""")
260260
// If task failed to run
261261
} else {
262-
// Post error
263-
postToNSLogAndStdOut(logLevel: "ERROR", logMessage:
264-
"""
265-
Running: \(messageActionDict["taskPath"] ?? "")
266-
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
267-
""", functionName: #function.components(separatedBy: "(")[0],
268-
verboseMode: "enabled")
262+
// Post error
263+
postToNSLogAndStdOut(logLevel: "ERROR", logMessage:
264+
"""
265+
Running: \(messageActionDict["taskPath"] ?? "")
266+
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
267+
""", functionName: #function.components(separatedBy: "(")[0],
268+
verboseMode: "enabled")
269269
}
270270
}
271271
}

Notifier/Notifier - Notifications/AppDelegate.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ func processArguments(messageContent: MessageContent, notificationCenter: UNUser
8282
notificationContent.userInfo["messageAction"] = getNotificationBodyAction(messageContent: messageContent,
8383
rootElements: rootElements)
8484
}
85+
// messageButton needs defining, even when not called. So processing it here along with messageButtonAction
86+
let (tempMessageButtonAction, tempCategory) = processMessageButton(notificationCenter: notificationCenter,
87+
messageContent: messageContent, rootElements: rootElements)
88+
// Set the notifications category
89+
notificationCenter.setNotificationCategories([tempCategory])
90+
// If tempMessageButtonAction has a value
91+
if !tempMessageButtonAction.isEmpty {
92+
// Add messageButtonAction to userInfo
93+
notificationContent.userInfo["messageButtonAction"] = tempMessageButtonAction
94+
}
8595
// If we have a value for messageSound passed
8696
if messageContent.messageSound != nil {
8797
// Set the notifications sound

Notifier/Notifier - Notifications/Functions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func gracefulLogout(userInfo: [AnyHashable: Any]) {
8787
// Progress log
8888
NSLog("\(#function.components(separatedBy: "(")[0]) - logout prompting")
8989
}
90-
// Create an NSAppleScript object wiht the logout command
90+
// Create an NSAppleScript object with the logout command
9191
if let scriptObject = NSAppleScript(source: "tell application \"loginwindow\" to «event aevtlogo»") {
9292
// If we receive output from the prior command
9393
if let outputString = scriptObject.executeAndReturnError(&error).stringValue {

Notifier/Notifier - Notifications/Structures.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ struct MessageContent: Codable {
1111
var messageAction: [TaskObject]?
1212
// The notifications message (required)
1313
var messageBody: String?
14+
// Optional - message button label
15+
var messageButton: String?
16+
// Optional - action to perform when the message button is clicked
17+
var messageButtonAction: [TaskObject]?
1418
// Optional - the sound played when the notification has been delivered
1519
var messageSound: String?
1620
// Optional - the notifications subtitle
@@ -25,10 +29,13 @@ struct MessageContent: Codable {
2529
var taskArguments: [String]?
2630
}
2731
// Initialize MessageContent
28-
init(messageAction: [TaskObject]? = nil, messageBody: String? = nil, messageSound: String? = nil,
29-
messageSubtitle: String? = nil, messageTitle: String? = nil) {
32+
init(messageAction: [TaskObject]? = nil, messageBody: String? = nil, messageButton: String? = nil,
33+
messageButtonAction: [TaskObject]? = nil, messageSound: String? = nil, messageSubtitle: String? = nil,
34+
messageTitle: String? = nil) {
3035
self.messageAction = messageAction
3136
self.messageBody = messageBody
37+
self.messageButton = messageButton
38+
self.messageButtonAction = messageButtonAction
3239
self.messageSound = messageSound
3340
self.messageSubtitle = messageSubtitle
3441
self.messageTitle = messageTitle
@@ -55,6 +62,8 @@ struct RootElements: Codable {
5562
struct UserInfo: Codable {
5663
// Optional - action to perform when the message is clicked
5764
var messageAction: [TaskObject]?
65+
// Optional - action to perform when the message button is clicked
66+
var messageButtonAction: [TaskObject]?
5867
// Arguments for the task object
5968
struct TaskObject: Codable {
6069
// The tasks executable
@@ -63,7 +72,8 @@ struct UserInfo: Codable {
6372
var taskArguments: [String]?
6473
}
6574
// Initialize ParsedArguments
66-
init(messageAction: [TaskObject]? = nil) {
75+
init(messageAction: [TaskObject]? = nil, messageButtonAction: [TaskObject]? = nil) {
6776
self.messageAction = messageAction
77+
self.messageButtonAction = messageButtonAction
6878
}
6979
}

Notifier/Notifier - Notifications/UserNotifications.swift

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,60 @@ func handleNotification(forResponse response: UNNotificationResponse) {
134134
exit(0)
135135
}
136136

137+
// Adds messageButton (always needed) and messageButtonAction (when defined)
138+
func processMessageButton(notificationCenter: UNUserNotificationCenter, messageContent: MessageContent,
139+
rootElements: RootElements) ->
140+
([AnyHashable: Any], UNNotificationCategory) {
141+
// Var declaration
142+
var tempCategory = UNNotificationCategory(identifier: "banner", actions: [], intentIdentifiers: [],
143+
options: .customDismissAction)
144+
var messageButtonAction = [AnyHashable: Any]()
145+
// If we have a value for messageButton passed
146+
if messageContent.messageButton != nil {
147+
// Create an action object
148+
let notificationAction = UNNotificationAction(identifier: "messagebutton",
149+
title: messageContent.messageButton ?? "",
150+
options: [])
151+
// Amend tempCategory
152+
tempCategory = UNNotificationCategory(identifier: "banner", actions: [notificationAction],
153+
intentIdentifiers: [],
154+
options: .customDismissAction)
155+
// If verbose mode is enabled
156+
if rootElements.verboseMode != nil {
157+
// Progress log
158+
NSLog("\(#function.components(separatedBy: "(")[0]) - messagebutton processed")
159+
}
160+
// If we have a values for messageButton and messageButtonAction passed
161+
if messageContent.messageButtonAction != nil {
162+
// Add taskPath from messagAction to messageButtonAction
163+
messageButtonAction["taskPath"] = messageContent.messageButtonAction?[0].taskPath
164+
// Add taskArguments from messageButtonAction
165+
messageButtonAction["taskArguments"] =
166+
messageContent.messageButtonAction?[0].taskArguments
167+
// If verbose mode is enabled
168+
if rootElements.verboseMode != nil {
169+
// Progress log
170+
NSLog("""
171+
\(#function.components(separatedBy: "(")[0]) - messageButtonAction - taskPath: \
172+
\(messageButtonAction["taskPath"] ?? ""),
173+
taskArguments: \(messageButtonAction["taskArguments"] ?? [])
174+
""")
175+
}
176+
// Return tempCategory and tempUserInfo
177+
return (messageButtonAction, tempCategory)
178+
}
179+
// If we don't have a value for messageButton
180+
} else {
181+
// If verbose mode is enabled
182+
if rootElements.verboseMode != nil {
183+
// Progress log
184+
NSLog("\(#function.components(separatedBy: "(")[0]) - no messagebutton defined")
185+
}
186+
}
187+
// Return empty userInfo for messageButtonAction and tempCategory
188+
return ([:], tempCategory)
189+
}
190+
137191
// Post the notification
138192
func postNotification(notificationCenter: UNUserNotificationCenter, notificationContent: UNMutableNotificationContent,
139193
messageContent: MessageContent, passedBase64: String, rootElements: RootElements) {
@@ -207,11 +261,11 @@ func processNotificationActions(userInfoKey: String, userInfo: [AnyHashable: Any
207261
} else {
208262
// Post error
209263
postToNSLogAndStdOut(logLevel: "ERROR", logMessage:
210-
"""
211-
Running: \(messageActionDict["taskPath"] ?? "")
212-
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
213-
""", functionName: #function.components(separatedBy: "(")[0],
214-
verboseMode: "enabled")
264+
"""
265+
Running: \(messageActionDict["taskPath"] ?? "")
266+
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
267+
""", functionName: #function.components(separatedBy: "(")[0],
268+
verboseMode: "enabled")
215269
}
216270
}
217271
}

0 commit comments

Comments
 (0)