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 the saved settings in Settings, and keep them current

Țugui Dragoș57d012462afa
diff --git a/Peel/Model/ResetPlan.swift b/Peel/Model/ResetPlan.swift
index 2173012b..faa27a86 100644
--- a/Peel/Model/ResetPlan.swift
+++ b/Peel/Model/ResetPlan.swift
@@ -143,6 +143,7 @@ final class ResetPlan {
return self.isAppRunning
}
self.outcome = outcome
+ SavedSettings.shared.reload()
return outcome.result
}
@@ -150,7 +151,7 @@ final class ResetPlan {
func putSettingsBack(from backup: URL) async -> PreferenceBackup.Restored {
// A copy is saved under the app's identifier, so an app with none has no copy to put back.
guard let identifier = app.bundleIdentifier else { return .notSaved }
- return await QuitGuard.shared.run {
+ let restored = await QuitGuard.shared.run {
await PreferenceBackup.restore(
from: backup,
of: identifier,
@@ -160,6 +161,9 @@ final class ResetPlan {
return self.isAppRunning
}
}
+ // The settings the app had until now may have become a copy of their own.
+ SavedSettings.shared.reload()
+ return restored
}
/// Quits the app and the helpers it ships, so nothing writes its settings again while they are cleared.
diff --git a/Peel/Model/SavedSettings.swift b/Peel/Model/SavedSettings.swift
new file mode 100644
index 00000000..c9467cb9
--- /dev/null
+++ b/Peel/Model/SavedSettings.swift
@@ -0,0 +1,18 @@
+import Observation
+import PeelCore
+
+/// The copies of settings that app resets kept, newest first. Read when first asked for, so Settings draws them in
+/// its first frame, then again by whatever makes or removes a copy, and by Settings when it appears or Peel comes
+/// forward, since a copy can also be removed in Finder.
+@Observable
+final class SavedSettings {
+ static let shared = SavedSettings()
+
+ private(set) var copies = PreferenceBackup.copies()
+
+ private init() {}
+
+ func reload() {
+ copies = PreferenceBackup.copies()
+ }
+}
diff --git a/Peel/Views/Settings/SavedSettingsSection.swift b/Peel/Views/Settings/SavedSettingsSection.swift
index f717c100..427dcb71 100644
--- a/Peel/Views/Settings/SavedSettingsSection.swift
+++ b/Peel/Views/Settings/SavedSettingsSection.swift
@@ -7,7 +7,6 @@ import SwiftUI
struct SavedSettingsSection: View {
@Environment(AppLibrary.self) private var library
@Environment(RemovalHistoryStore.self) private var history
- @State private var copies: [PreferenceBackup.Copy] = []
@State private var workingOn: PreferenceBackup.Copy.ID?
@State private var failure: Failure?
@State private var copyToTrash: PreferenceBackup.Copy?
@@ -16,6 +15,8 @@ struct SavedSettingsSection: View {
/// launch and quit, because a read of the workspace while drawing gives Observation nothing to follow.
@State private var openApps: Set<String> = []
+ private var copies: [PreferenceBackup.Copy] { SavedSettings.shared.copies }
+
var body: some View {
Group {
if !copies.isEmpty {
@@ -31,7 +32,9 @@ struct SavedSettingsSection: View {
}
}
}
- .task { reload() }
+ .onChange(of: copies, initial: true) {
+ refreshOpenApps()
+ }
// Put Back waits for the app to quit. The workspace reports every change to the apps that run through
// key-value observing, while it posts no notice for a menu bar or background app.
.onReceive(NSWorkspace.shared.publisher(for: \.runningApplications)) { _ in
@@ -87,11 +90,6 @@ struct SavedSettingsSection: View {
}
}
- private func reload() {
- copies = PreferenceBackup.copies()
- refreshOpenApps()
- }
-
/// Whether the app a copy belongs to is running, by the rule a reset uses: the app, its helpers, and another
/// copy of it that writes the same settings.
private func isRunning(_ bundleIdentifier: String) -> Bool {
@@ -125,7 +123,7 @@ struct SavedSettingsSection: View {
}
}
// The settings the app had until now may have become a copy of their own.
- reload()
+ SavedSettings.shared.reload()
switch restored {
case .complete:
break
@@ -161,7 +159,7 @@ struct SavedSettingsSection: View {
sizes: [URL: Int64](measured: [(copy.folder, size)])
)
}
- reload()
+ SavedSettings.shared.reload()
}
private var trashQuestion: Text {
diff --git a/Peel/Views/Settings/SettingsView.swift b/Peel/Views/Settings/SettingsView.swift
index babdccbc..770adf32 100644
--- a/Peel/Views/Settings/SettingsView.swift
+++ b/Peel/Views/Settings/SettingsView.swift
@@ -246,6 +246,7 @@ private struct GeneralSettingsView: View {
let status = SMAppService.mainApp.status
opensAtLogin = status == .enabled || status == .requiresApproval
needsApprovalToOpenAtLogin = status == .requiresApproval
+ SavedSettings.shared.reload()
}
var body: some View {
@@ -469,7 +470,7 @@ private struct GeneralSettingsView: View {
}
.task { readStandings() }
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- // The user may have changed the login item in System Settings, so read it again.
+ // The user may have changed the login item in System Settings, or a saved copy in Finder.
readStandings()
}
.confirmationDialog("Remove Peel from this Mac?", isPresented: $isConfirmingSelfRemoval) {