Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Answer a click on a notification in the Peel that sent it

Țugui Dragoșfc9372849f79
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index f2106a99..a10fcccb 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -38,8 +38,8 @@ The package, `Packages/PeelCore`, has four libraries:
- **PeelPrivileged** is the part the helper shares: which paths may be touched, what is protected, how a tool
is run, and how a path is split into names. The helper links nothing else, so it stays small.
- **PeelCommandLine** holds the `peel` commands. `PeelCLI` is only their entry point.
-- **PeelLink** says what a `peel://open?path=` link means, for the Finder extension that sends it, the
- notifications that send it too, and the app that reads it. The extension links nothing else.
+- **PeelLink** says what a `peel://open?path=` link means, for the Finder extension that sends it and the app that
+ reads it. The extension links nothing else.
Keeping the logic in the package is what makes it testable: `swift test --package-path Packages/PeelCore` runs
more than 2,200 tests without building the app.
diff --git a/Peel/Model/PeelNotifications.swift b/Peel/Model/PeelNotifications.swift
index 6dfd95a7..e716c7a0 100644
--- a/Peel/Model/PeelNotifications.swift
+++ b/Peel/Model/PeelNotifications.swift
@@ -1,13 +1,12 @@
import AppKit
import Foundation
import PeelCore
-import PeelLink
import SwiftUI
import UserNotifications
final class PeelNotifications: NSObject, UNUserNotificationCenterDelegate {
- private nonisolated static let pathKey = "path"
- private nonisolated static let toolKey = "tool"
+ nonisolated static let pathKey = "path"
+ nonisolated static let toolKey = "tool"
func activate() {
UNUserNotificationCenter.current().delegate = self
@@ -62,14 +61,30 @@ final class PeelNotifications: NSObject, UNUserNotificationCenterDelegate {
/// One notification for the whole round of checks, never one per app.
func notify(updatesAvailable count: Int, firstName: String, firstApp: URL) {
guard count > 0 else { return }
+ let content = Self.updatesContent(count: count, firstName: firstName, firstApp: firstApp)
+ let request = UNNotificationRequest(identifier: "updates", content: content, trigger: nil)
+ UNUserNotificationCenter.current().add(request)
+ }
+
+ static func updatesContent(count: Int, firstName: String, firstApp: URL) -> UNNotificationContent {
let content = UNMutableNotificationContent()
content.title = count == 1
? String(localized: "\(firstName) has an update")
: String(inflecting: "^[\(count) app](inflect: true) have new updates")
content.body = String(localized: "Peel doesn’t install updates on its own. Click to see what’s waiting, under Applications.")
content.userInfo = [Self.pathKey: firstApp.path(percentEncoded: false)]
- let request = UNNotificationRequest(identifier: "updates", content: content, trigger: nil)
- UNUserNotificationCenter.current().add(request)
+ return content
+ }
+
+ /// Answers a click in this copy of Peel, as a Shortcut does: a `peel://` link goes to whichever copy Launch
+ /// Services prefers.
+ func open(tool: String?, path: String?) {
+ if let path {
+ Navigator.shared.requestedApp = URL(filePath: path, directoryHint: .isDirectory)
+ Navigator.shared.show(.applications)
+ } else if let tool = tool.flatMap(Tool.init(rawValue:)) {
+ Navigator.shared.show(tool)
+ }
}
func notify(diskNearlyFull storage: DeviceInfo.Storage) {
@@ -92,21 +107,9 @@ final class PeelNotifications: NSObject, UNUserNotificationCenterDelegate {
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse
) async {
- if let tool = response.notification.request.content.userInfo[Self.toolKey] as? String {
- await MainActor.run {
- if let tool = Tool(rawValue: tool) {
- Navigator.shared.show(tool)
- }
- }
- return
- }
- guard
- let path = response.notification.request.content.userInfo[Self.pathKey] as? String,
- let url = OpenRequest.link(toApplicationAt: path)
- else { return }
- await MainActor.run {
- _ = NSWorkspace.shared.open(url)
- }
+ let userInfo = response.notification.request.content.userInfo
+ let (tool, path) = (userInfo[Self.toolKey] as? String, userInfo[Self.pathKey] as? String)
+ await MainActor.run { open(tool: tool, path: path) }
}
}
diff --git a/PeelTests/NotificationClickTests.swift b/PeelTests/NotificationClickTests.swift
new file mode 100644
index 00000000..bb35fea7
--- /dev/null
+++ b/PeelTests/NotificationClickTests.swift
@@ -0,0 +1,27 @@
+import Foundation
+@testable import Peel
+import PeelCore
+import Testing
+import UserNotifications
+
+@Suite(.serialized) @MainActor struct NotificationClickTests {
+ private func click(_ content: UNNotificationContent, on notifications: PeelNotifications) {
+ notifications.open(
+ tool: content.userInfo[PeelNotifications.toolKey] as? String,
+ path: content.userInfo[PeelNotifications.pathKey] as? String
+ )
+ }
+
+ @Test func oneUpdateOpensItsAppInThisCopyOfPeel() {
+ defer {
+ Navigator.shared.requestedApp = nil
+ Navigator.shared.requestedTool = nil
+ }
+ let app = URL(filePath: "/Applications/Example.app", directoryHint: .isDirectory)
+
+ click(PeelNotifications.updatesContent(count: 1, firstName: "Example", firstApp: app), on: PeelNotifications())
+
+ #expect(Navigator.shared.requestedApp == app)
+ #expect(Navigator.shared.requestedTool == .applications)
+ }
+}