Skip to content

Commit

Permalink
Merge pull request #27 from dataJAR/3.1
Browse files Browse the repository at this point in the history
Approved
  • Loading branch information
macmule authored Jun 18, 2024
2 parents 29cdec7 + 44ebcd5 commit b1fb062
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 212 deletions.
3 changes: 1 addition & 2 deletions Notifier/.swiftlint.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ opt_in_rules: # some rules are only opt-in
- trailing_newline
- colon
- comma
included: # paths to include during linting. `--path` is ignored if present.
- auto-update
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Pods
- Scripts/CodeSignUpdate.swift
- Payload

# configurable rules can be customized from this configuration file
# binary rules can set their severity level
Expand Down
2 changes: 1 addition & 1 deletion Notifier/Notifier - Alerts/Structures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct RootElements: Codable {
struct UserInfo: Codable {
// Optional - action to perform when the message is clicked
var messageAction: [TaskObject]?
// Optional - alert only - action to perform when the message button is clicked
// Optional - action to perform when the message button is clicked
var messageButtonAction: [TaskObject]?
// Arguments for the task object
struct TaskObject: Codable {
Expand Down
14 changes: 7 additions & 7 deletions Notifier/Notifier - Alerts/UserNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ func processNotificationActions(userInfoKey: String, userInfo: [AnyHashable: Any
""")
// If task failed to run
} else {
// Post error
postToNSLogAndStdOut(logLevel: "ERROR", logMessage:
"""
Running: \(messageActionDict["taskPath"] ?? "")
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
""", functionName: #function.components(separatedBy: "(")[0],
verboseMode: "enabled")
// Post error
postToNSLogAndStdOut(logLevel: "ERROR", logMessage:
"""
Running: \(messageActionDict["taskPath"] ?? "")
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
""", functionName: #function.components(separatedBy: "(")[0],
verboseMode: "enabled")
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions Notifier/Notifier - Notifications/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func processArguments(messageContent: MessageContent, notificationCenter: UNUser
notificationContent.userInfo["messageAction"] = getNotificationBodyAction(messageContent: messageContent,
rootElements: rootElements)
}
// messageButton needs defining, even when not called. So processing it here along with messageButtonAction
let (tempMessageButtonAction, tempCategory) = processMessageButton(notificationCenter: notificationCenter,
messageContent: messageContent, rootElements: rootElements)
// Set the notifications category
notificationCenter.setNotificationCategories([tempCategory])
// If tempMessageButtonAction has a value
if !tempMessageButtonAction.isEmpty {
// Add messageButtonAction to userInfo
notificationContent.userInfo["messageButtonAction"] = tempMessageButtonAction
}
// If we have a value for messageSound passed
if messageContent.messageSound != nil {
// Set the notifications sound
Expand Down
2 changes: 1 addition & 1 deletion Notifier/Notifier - Notifications/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func gracefulLogout(userInfo: [AnyHashable: Any]) {
// Progress log
NSLog("\(#function.components(separatedBy: "(")[0]) - logout prompting")
}
// Create an NSAppleScript object wiht the logout command
// Create an NSAppleScript object with the logout command
if let scriptObject = NSAppleScript(source: "tell application \"loginwindow\" to «event aevtlogo»") {
// If we receive output from the prior command
if let outputString = scriptObject.executeAndReturnError(&error).stringValue {
Expand Down
16 changes: 13 additions & 3 deletions Notifier/Notifier - Notifications/Structures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ struct MessageContent: Codable {
var messageAction: [TaskObject]?
// The notifications message (required)
var messageBody: String?
// Optional - message button label
var messageButton: String?
// Optional - action to perform when the message button is clicked
var messageButtonAction: [TaskObject]?
// Optional - the sound played when the notification has been delivered
var messageSound: String?
// Optional - the notifications subtitle
Expand All @@ -25,10 +29,13 @@ struct MessageContent: Codable {
var taskArguments: [String]?
}
// Initialize MessageContent
init(messageAction: [TaskObject]? = nil, messageBody: String? = nil, messageSound: String? = nil,
messageSubtitle: String? = nil, messageTitle: String? = nil) {
init(messageAction: [TaskObject]? = nil, messageBody: String? = nil, messageButton: String? = nil,
messageButtonAction: [TaskObject]? = nil, messageSound: String? = nil, messageSubtitle: String? = nil,
messageTitle: String? = nil) {
self.messageAction = messageAction
self.messageBody = messageBody
self.messageButton = messageButton
self.messageButtonAction = messageButtonAction
self.messageSound = messageSound
self.messageSubtitle = messageSubtitle
self.messageTitle = messageTitle
Expand All @@ -55,6 +62,8 @@ struct RootElements: Codable {
struct UserInfo: Codable {
// Optional - action to perform when the message is clicked
var messageAction: [TaskObject]?
// Optional - action to perform when the message button is clicked
var messageButtonAction: [TaskObject]?
// Arguments for the task object
struct TaskObject: Codable {
// The tasks executable
Expand All @@ -63,7 +72,8 @@ struct UserInfo: Codable {
var taskArguments: [String]?
}
// Initialize ParsedArguments
init(messageAction: [TaskObject]? = nil) {
init(messageAction: [TaskObject]? = nil, messageButtonAction: [TaskObject]? = nil) {
self.messageAction = messageAction
self.messageButtonAction = messageButtonAction
}
}
64 changes: 59 additions & 5 deletions Notifier/Notifier - Notifications/UserNotifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,60 @@ func handleNotification(forResponse response: UNNotificationResponse) {
exit(0)
}

// Adds messageButton (always needed) and messageButtonAction (when defined)
func processMessageButton(notificationCenter: UNUserNotificationCenter, messageContent: MessageContent,
rootElements: RootElements) ->
([AnyHashable: Any], UNNotificationCategory) {
// Var declaration
var tempCategory = UNNotificationCategory(identifier: "banner", actions: [], intentIdentifiers: [],
options: .customDismissAction)
var messageButtonAction = [AnyHashable: Any]()
// If we have a value for messageButton passed
if messageContent.messageButton != nil {
// Create an action object
let notificationAction = UNNotificationAction(identifier: "messagebutton",
title: messageContent.messageButton ?? "",
options: [])
// Amend tempCategory
tempCategory = UNNotificationCategory(identifier: "banner", actions: [notificationAction],
intentIdentifiers: [],
options: .customDismissAction)
// If verbose mode is enabled
if rootElements.verboseMode != nil {
// Progress log
NSLog("\(#function.components(separatedBy: "(")[0]) - messagebutton processed")
}
// If we have a values for messageButton and messageButtonAction passed
if messageContent.messageButtonAction != nil {
// Add taskPath from messagAction to messageButtonAction
messageButtonAction["taskPath"] = messageContent.messageButtonAction?[0].taskPath
// Add taskArguments from messageButtonAction
messageButtonAction["taskArguments"] =
messageContent.messageButtonAction?[0].taskArguments
// If verbose mode is enabled
if rootElements.verboseMode != nil {
// Progress log
NSLog("""
\(#function.components(separatedBy: "(")[0]) - messageButtonAction - taskPath: \
\(messageButtonAction["taskPath"] ?? ""),
taskArguments: \(messageButtonAction["taskArguments"] ?? [])
""")
}
// Return tempCategory and tempUserInfo
return (messageButtonAction, tempCategory)
}
// If we don't have a value for messageButton
} else {
// If verbose mode is enabled
if rootElements.verboseMode != nil {
// Progress log
NSLog("\(#function.components(separatedBy: "(")[0]) - no messagebutton defined")
}
}
// Return empty userInfo for messageButtonAction and tempCategory
return ([:], tempCategory)
}

// Post the notification
func postNotification(notificationCenter: UNUserNotificationCenter, notificationContent: UNMutableNotificationContent,
messageContent: MessageContent, passedBase64: String, rootElements: RootElements) {
Expand Down Expand Up @@ -207,11 +261,11 @@ func processNotificationActions(userInfoKey: String, userInfo: [AnyHashable: Any
} else {
// Post error
postToNSLogAndStdOut(logLevel: "ERROR", logMessage:
"""
Running: \(messageActionDict["taskPath"] ?? "")
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
""", functionName: #function.components(separatedBy: "(")[0],
verboseMode: "enabled")
"""
Running: \(messageActionDict["taskPath"] ?? "")
\(messageActionDict["taskArguments"] ?? []) failed with \(taskOutput).
""", functionName: #function.components(separatedBy: "(")[0],
verboseMode: "enabled")
}
}
}
Expand Down
Loading

0 comments on commit b1fb062

Please sign in to comment.