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.

List again what a Put Back brought back, on the page that moved it

Țugui Dragoș50d30ff70c56
diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swift
index 4a14e701..a00d6e09 100644
--- a/Peel/Model/RemovalHistoryStore.swift
+++ b/Peel/Model/RemovalHistoryStore.swift
@@ -158,6 +158,10 @@ final class RemovalHistoryStore {
var failures: [RemovalRecord.ID: RestoreFailure] = [:]
/// Counts the reloads asked for, so a removal's page looks in the Trash again with each.
private(set) var reloads = 0
+ /// Counts, for each tool, the Put Backs that brought back what it had moved, so its page lists them again.
+ private(set) var putBack: [Tool: Int] = [:]
+ /// The count each page last listed under, kept here so a page off screen at a Put Back lists it when it returns.
+ @ObservationIgnored private var listedAfterPutBack = ScannedRevisions()
/// Counts each change of what History lists. The lists are prepared off the main actor, and one prepared for an
/// older change must never replace a newer one.
private var listedChange = 0
@@ -333,6 +337,11 @@ final class RemovalHistoryStore {
if !again.isEmpty { self.undoable = (again, undoable.title) }
}
+ /// Whether a Put Back brought back what `tool` moved since its page last asked. False the first time it asks.
+ func needsRescanAfterPutBack(_ tool: Tool) -> Bool {
+ listedAfterPutBack.needsRescan(tool.rawValue, at: putBack[tool, default: 0])
+ }
+
/// Takes the records `ids` names out of what Undo would put back, once they are back or gone from History.
private func keepUndoable(without ids: Set<RemovalRecord.ID>) {
guard let undoable, !ids.isEmpty else { return }
@@ -415,6 +424,9 @@ final class RemovalHistoryStore {
selectedIDs.subtract(restored)
if !restored.isEmpty {
keepUndoable(without: restored)
+ for tool in Set(ordered.filter { restored.contains($0.id) }.compactMap { Tool(rawValue: $0.tool) }) {
+ putBack[tool, default: 0] += 1
+ }
await apply(await log.remove(restored))
// A move is announced, and so is its undoing: the rows only vanish from the list.
AccessibilityNotification.Announcement(
diff --git a/Peel/Views/BackgroundItems/BackgroundItemList.swift b/Peel/Views/BackgroundItems/BackgroundItemList.swift
index ec078cbb..089cb8f5 100644
--- a/Peel/Views/BackgroundItems/BackgroundItemList.swift
+++ b/Peel/Views/BackgroundItems/BackgroundItemList.swift
@@ -67,6 +67,7 @@ struct BackgroundItemList: View {
await backgroundItems.refresh(installedApps: library.apps)
}
.rescanOnExclusionChange("BackgroundItemList") { await backgroundItems.refresh(installedApps: library.apps) }
+ .rescanAfterPutBack(.backgroundItems) { await backgroundItems.refresh(installedApps: library.apps) }
// Shown from the list, which stays on screen. An alert on the item's page would vanish with the page if
// the rescan after the action removed the item, and then show up on the next item the user opens.
.alert(failureTitle, isPresented: isShowingFailure) {
diff --git a/Peel/Views/Developer/DeveloperList.swift b/Peel/Views/Developer/DeveloperList.swift
index 479b7a52..22939a6c 100644
--- a/Peel/Views/Developer/DeveloperList.swift
+++ b/Peel/Views/Developer/DeveloperList.swift
@@ -57,6 +57,7 @@ struct DeveloperList: View {
await developer.refresh()
}
.rescanOnExclusionChange("DeveloperList") { await developer.refresh() }
+ .rescanAfterPutBack(.developer) { await developer.refresh() }
}
private var phase: ScanPhase {
diff --git a/Peel/Views/ExclusionRescan.swift b/Peel/Views/ExclusionRescan.swift
index 92cd79dc..71eeda76 100644
--- a/Peel/Views/ExclusionRescan.swift
+++ b/Peel/Views/ExclusionRescan.swift
@@ -17,6 +17,24 @@ extension View {
func rescanOnExclusionChange(scannedUnder revision: Int?, _ rescan: @escaping () async -> Void) -> some View {
modifier(PageExclusionRescan(scannedUnder: revision, rescan: rescan))
}
+
+ /// Scans again once a Put Back, from History or Undo, brought back what `tool` moved, so it is listed again.
+ func rescanAfterPutBack(_ tool: Tool, _ rescan: @escaping () async -> Void) -> some View {
+ modifier(PutBackRescan(tool: tool, rescan: rescan))
+ }
+}
+
+private struct PutBackRescan: ViewModifier {
+ @Environment(RemovalHistoryStore.self) private var history
+ let tool: Tool
+ let rescan: () async -> Void
+
+ func body(content: Content) -> some View {
+ content.task(id: history.putBack[tool, default: 0]) {
+ guard history.needsRescanAfterPutBack(tool) else { return }
+ await rescan()
+ }
+ }
}
private struct PageExclusionRescan: ViewModifier {
diff --git a/Peel/Views/Installers/InstallerList.swift b/Peel/Views/Installers/InstallerList.swift
index 2dd78b40..2a5660ee 100644
--- a/Peel/Views/Installers/InstallerList.swift
+++ b/Peel/Views/Installers/InstallerList.swift
@@ -92,6 +92,7 @@ struct InstallerList: View {
await installers.refresh(installedApps: library.apps)
}
.rescanOnExclusionChange("InstallerList") { await installers.refresh(installedApps: library.apps) }
+ .rescanAfterPutBack(.installers) { await installers.refresh(installedApps: library.apps) }
}
private func phase(_ filtered: [InstallerItem.Kind]) -> ScanPhase {
diff --git a/Peel/Views/Orphans/OrphanList.swift b/Peel/Views/Orphans/OrphanList.swift
index be60af37..fd6a0eb4 100644
--- a/Peel/Views/Orphans/OrphanList.swift
+++ b/Peel/Views/Orphans/OrphanList.swift
@@ -138,6 +138,7 @@ struct OrphanList: View {
await orphans.refresh(from: library)
}
.rescanOnExclusionChange("OrphanList") { await orphans.refresh(from: library) }
+ .rescanAfterPutBack(.orphans) { await orphans.refresh(from: library) }
}
private func phase(_ filtered: [OrphanGroup], _ forAnAdministrator: [AdministratorItem]) -> ScanPhase {
diff --git a/Peel/Views/Packages/PackageList.swift b/Peel/Views/Packages/PackageList.swift
index 6e4bf65e..90e79e35 100644
--- a/Peel/Views/Packages/PackageList.swift
+++ b/Peel/Views/Packages/PackageList.swift
@@ -76,6 +76,7 @@ struct PackageList: View {
await packages.refresh()
}
.rescanOnExclusionChange("PackageList") { await packages.refresh() }
+ .rescanAfterPutBack(.packages) { await packages.refresh() }
}
private func phase(_ filtered: [PackageReceipt]) -> ScanPhase {
diff --git a/Peel/Views/Plugins/PluginList.swift b/Peel/Views/Plugins/PluginList.swift
index 0c4eebc8..025ab10f 100644
--- a/Peel/Views/Plugins/PluginList.swift
+++ b/Peel/Views/Plugins/PluginList.swift
@@ -101,6 +101,7 @@ struct PluginList: View {
await plugins.refresh()
}
.rescanOnExclusionChange("PluginList") { await plugins.refresh() }
+ .rescanAfterPutBack(.plugins) { await plugins.refresh() }
}
private func phase(_ filtered: [Plugin]) -> ScanPhase {
diff --git a/Peel/Views/Projects/ProjectList.swift b/Peel/Views/Projects/ProjectList.swift
index b046e1ee..8ad99d3b 100644
--- a/Peel/Views/Projects/ProjectList.swift
+++ b/Peel/Views/Projects/ProjectList.swift
@@ -172,6 +172,7 @@ struct ProjectList: View {
await projects.refresh()
}
.rescanOnExclusionChange("ProjectList") { await projects.refresh() }
+ .rescanAfterPutBack(.projects) { await projects.refresh() }
}
@ViewBuilder private var unsearchableReasons: some View {
diff --git a/Peel/Views/Space/SpaceList.swift b/Peel/Views/Space/SpaceList.swift
index 0492d760..e88e009a 100644
--- a/Peel/Views/Space/SpaceList.swift
+++ b/Peel/Views/Space/SpaceList.swift
@@ -99,6 +99,7 @@ struct SpaceList: View {
await space.refresh()
}
.rescanOnExclusionChange("SpaceList") { await space.refresh() }
+ .rescanAfterPutBack(.space) { await space.refresh() }
}
private func storage(_ report: SpaceReport) -> some View {
diff --git a/PeelTests/RemovalOutcomeTests.swift b/PeelTests/RemovalOutcomeTests.swift
index e65978eb..00336994 100644
--- a/PeelTests/RemovalOutcomeTests.swift
+++ b/PeelTests/RemovalOutcomeTests.swift
@@ -89,4 +89,18 @@ struct RemovalOutcomeTests {
#expect(history.undoTitle == nil)
}
+
+ @Test func aPutBackTellsThePageOfTheToolThatMovedWhatCameBack() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let history = history(in: folder, outcome: RemovalOutcome())
+ await history.record(try moved(["Cache"]), tool: .plugins, source: "Plug-ins", sizes: [:])
+ #expect(!history.needsRescanAfterPutBack(.plugins) && !history.needsRescanAfterPutBack(.space))
+
+ await history.undoLastRemoval()
+
+ #expect(history.needsRescanAfterPutBack(.plugins))
+ #expect(!history.needsRescanAfterPutBack(.plugins), "once it has listed again")
+ #expect(!history.needsRescanAfterPutBack(.space))
+ }
}