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.

Show a failed change of the helper where it was asked for, and only there

Țugui Dragoș61c2884c8a18
diff --git a/Peel/Model/HelperModel.swift b/Peel/Model/HelperModel.swift
index af2c5139..b926caa5 100644
--- a/Peel/Model/HelperModel.swift
+++ b/Peel/Model/HelperModel.swift
@@ -9,7 +9,6 @@ final class HelperModel {
private(set) var status = PrivilegedHelper.status
private(set) var changing: Action?
private(set) var isResponding: Bool?
- var failure: Failure?
struct Failure {
let action: Action
@@ -63,32 +62,29 @@ final class HelperModel {
/// Registers the helper, and opens Login Items settings when it needs the user's approval. Per
/// `SMAppService.h`, registering a service that is already registered, or not approved by the user,
/// throws an error. Neither is reported as a failure, since the status says what happened.
- func install() async {
- guard !isChanging else { return }
+ func install() async -> Failure? {
+ guard !isChanging else { return nil }
let error = await change(.install) { try await PrivilegedHelper.register() }
- if let error, status != .requiresApproval, status != .enabled {
- failure = Failure(action: .install, reason: error.localizedDescription)
- }
if status == .requiresApproval {
PrivilegedHelper.openLoginItemsSettings()
}
+ guard let error, status != .requiresApproval, status != .enabled else { return nil }
+ return Failure(action: .install, reason: error.localizedDescription)
}
- func repair() async {
- guard !isChanging else { return }
- if let error = await change(.repair, { try await PrivilegedHelper.repair() }) {
- failure = Failure(action: .repair, reason: error.localizedDescription)
- }
+ func repair() async -> Failure? {
+ guard !isChanging else { return nil }
+ let error = await change(.repair) { try await PrivilegedHelper.repair() }
if status == .requiresApproval {
PrivilegedHelper.openLoginItemsSettings()
}
+ return error.map { Failure(action: .repair, reason: $0.localizedDescription) }
}
- func uninstall() async {
- guard !isChanging else { return }
- if let error = await change(.uninstall, { try await PrivilegedHelper.unregister() }) {
- failure = Failure(action: .uninstall, reason: error.localizedDescription)
- }
+ func uninstall() async -> Failure? {
+ guard !isChanging else { return nil }
+ let error = await change(.uninstall) { try await PrivilegedHelper.unregister() }
+ return error.map { Failure(action: .uninstall, reason: $0.localizedDescription) }
}
/// Runs a change of the helper's registration and answers the error it threw, if any. Nothing about the helper
diff --git a/Peel/Model/SelfUninstall.swift b/Peel/Model/SelfUninstall.swift
index 85a92ac6..87c2b1da 100644
--- a/Peel/Model/SelfUninstall.swift
+++ b/Peel/Model/SelfUninstall.swift
@@ -72,14 +72,14 @@ final class SelfUninstall {
links = await TrashService(exclusions: exclusions).trash(own, usingHelperFor: Set(own))
}
ledger = await PrivilegedHelper.moveLedgerToTrash()
- await helper.uninstall()
+ let helperFailure = await helper.uninstall()
// A helper still registered would point into the Trash, so Peel moves only once the helper is gone.
guard !isRegistered() else {
work.resume()
if !links.trashed.isEmpty {
await record(TrashResult(trashed: links.trashed))
}
- let reason = helper.failure?.reason ?? String(localized: "The helper couldn’t be removed, so Peel stayed where it was.")
+ let reason = helperFailure?.reason ?? String(localized: "The helper couldn’t be removed, so Peel stayed where it was.")
failure = ledger == .moved ? "\(reason)\n\n\(Self.ledgerInTheTrash)" : reason
return
}
diff --git a/Peel/Views/HelperFailureAlert.swift b/Peel/Views/HelperFailureAlert.swift
new file mode 100644
index 00000000..f2359fe2
--- /dev/null
+++ b/Peel/Views/HelperFailureAlert.swift
@@ -0,0 +1,34 @@
+import SwiftUI
+
+/// The alert that says why a change of the helper failed, shown only by the place that asked for the change.
+private struct HelperFailureAlert: ViewModifier {
+ @Binding var failure: HelperModel.Failure?
+
+ func body(content: Content) -> some View {
+ content.alert(failure?.title ?? Text(verbatim: ""), isPresented: isShowingFailure) {
+ Button("OK", role: .cancel) {}
+ } message: {
+ Text(verbatim: failure?.reason ?? "")
+ }
+ }
+
+ private var isShowingFailure: Binding<Bool> {
+ Binding(get: { failure != nil }, set: { if !$0 { failure = nil } })
+ }
+}
+
+extension HelperModel.Failure {
+ fileprivate var title: Text {
+ switch action {
+ case .install: Text("The helper couldn’t be installed.")
+ case .repair: Text("The helper couldn’t be repaired.")
+ case .uninstall: Text("The helper couldn’t be uninstalled.")
+ }
+ }
+}
+
+extension View {
+ func helperFailureAlert(_ failure: Binding<HelperModel.Failure?>) -> some View {
+ modifier(HelperFailureAlert(failure: failure))
+ }
+}
diff --git a/Peel/Views/Home/HomePermissionsView.swift b/Peel/Views/Home/HomePermissionsView.swift
index c054a8fb..a650ab9f 100644
--- a/Peel/Views/Home/HomePermissionsView.swift
+++ b/Peel/Views/Home/HomePermissionsView.swift
@@ -8,6 +8,7 @@ struct HomePermissionsContent: View {
@Environment(ExclusionsStore.self) private var exclusions
@Environment(\.openSettings) private var openSettings
@State private var pointingAt: HomeModel.Permission?
+ @State private var helperFailure: HelperModel.Failure?
static let markSize: CGFloat = 36
/// A whole number of points, so the mark's body (30) and edge (3 on each side) fall on whole pixels at
@@ -41,6 +42,7 @@ struct HomePermissionsContent: View {
.frame(maxWidth: .infinity, alignment: .leading)
.motion(value: home.states)
.motion(value: helper.changing)
+ .helperFailureAlert($helperFailure)
}
/// A card for one section, laid out as a grid so the states line up in one column and the actions in
@@ -304,9 +306,9 @@ struct HomePermissionsContent: View {
if state == .pending {
home.openLoginItemsSettings()
} else if helper.standing == .notAnswering {
- Task { await helper.repair() }
+ Task { helperFailure = await helper.repair() }
} else {
- Task { await helper.install() }
+ Task { helperFailure = await helper.install() }
}
case .notifications: Task { await home.requestNotifications() }
case .fullDiskAccess, .appManagement, .finderExtension, .openAtLogin: reveal(permission)
diff --git a/Peel/Views/Settings/SettingsView.swift b/Peel/Views/Settings/SettingsView.swift
index 354c9907..cf8b2e3e 100644
--- a/Peel/Views/Settings/SettingsView.swift
+++ b/Peel/Views/Settings/SettingsView.swift
@@ -622,6 +622,7 @@ extension GeneralSettingsView {
private struct HelperSettingsView: View {
@Environment(HelperModel.self) private var helper
+ @State private var failure: HelperModel.Failure?
/// The helper's buttons have no label beside them, so each is centered in its row.
var body: some View {
@@ -645,7 +646,7 @@ private struct HelperSettingsView: View {
switch helper.standing {
case .notInstalled:
Button("Install Helper", systemImage: "lock.shield") {
- Task { await helper.install() }
+ Task { failure = await helper.install() }
}
.disabled(helper.isChanging)
.centeredInRow()
@@ -670,7 +671,7 @@ private struct HelperSettingsView: View {
Text("Peel’s helper isn’t answering. Repairing it installs the one this version of Peel works with.")
.font(.callout)
Button("Repair Helper", systemImage: "arrow.clockwise") {
- Task { await helper.repair() }
+ Task { failure = await helper.repair() }
}
.disabled(helper.isChanging)
.centeredInRow()
@@ -685,16 +686,12 @@ private struct HelperSettingsView: View {
.task {
await helper.checkConnection()
}
- .alert(helperFailureTitle, isPresented: isShowingFailure) {
- Button("OK", role: .cancel) {}
- } message: {
- Text(verbatim: helper.failure?.reason ?? "")
- }
+ .helperFailureAlert($failure)
}
private var uninstallButton: some View {
Button("Uninstall Helper", systemImage: "trash", role: .destructive) {
- Task { await helper.uninstall() }
+ Task { failure = await helper.uninstall() }
}
.disabled(helper.isChanging)
.centeredInRow()
@@ -721,20 +718,4 @@ private struct HelperSettingsView: View {
}
}
}
-
- private var helperFailureTitle: Text {
- switch helper.failure?.action {
- case .install: Text("The helper couldn’t be installed.")
- case .repair: Text("The helper couldn’t be repaired.")
- case .uninstall: Text("The helper couldn’t be uninstalled.")
- case nil: Text(verbatim: "")
- }
- }
-
- private var isShowingFailure: Binding<Bool> {
- Binding(
- get: { helper.failure != nil },
- set: { if !$0 { helper.failure = nil } }
- )
- }
}