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.

Read History again when Peel comes forward

Țugui Dragoș52df75804f19
diff --git a/Peel/Model/RemovalHistoryStore.swift b/Peel/Model/RemovalHistoryStore.swift
index ee39cf38..00552708 100644
--- a/Peel/Model/RemovalHistoryStore.swift
+++ b/Peel/Model/RemovalHistoryStore.swift
@@ -167,8 +167,9 @@ final class RemovalHistoryStore {
/// sits in many batches.
var selectedIDs: Set<RemovalRecord.ID> = []
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 the times History was read again, by Reload or as Peel came forward, so a removal's page then looks in
+ /// the Trash again with the records as they are now.
+ private(set) var rereads = 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.
@@ -215,8 +216,20 @@ final class RemovalHistoryStore {
/// the Trash looked at again.
func reload() async {
failures = [:]
- reloads += 1
+ await readAgain()
+ }
+
+ /// Reads History again as Peel comes forward, since `peel` may have moved or put back something meanwhile, once
+ /// History is in use: its page was opened, or Undo can put back a removal of this session.
+ func readAgainIfInUse() async {
+ guard hasLoaded || undoable != nil else { return }
+ await readAgain()
+ }
+
+ /// Reads History before a removal's page looks in the Trash, so the page never looks with records `peel` changed.
+ private func readAgain() async {
await load()
+ rereads += 1
}
/// The problem History's page shows: that History cannot be read, as soon as that is known, otherwise what the
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index cc403769..2b5d1d55 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -192,6 +192,7 @@ struct PeelApp: App {
// History may have become readable again meanwhile, or stopped being, and `peel` may have moved something.
history.checkReadability()
stats.reload()
+ Task { await history.readAgainIfInUse() }
homebrew.checkInstalled()
Task { await followHomebrewsChecks() }
// Home's checks run again whatever page is showing: permissions change in System Settings, and the
diff --git a/Peel/Views/History/HistoryDetailView.swift b/Peel/Views/History/HistoryDetailView.swift
index bd20067a..3cd7d2d9 100644
--- a/Peel/Views/History/HistoryDetailView.swift
+++ b/Peel/Views/History/HistoryDetailView.swift
@@ -5,7 +5,6 @@ import SwiftUI
struct HistoryDetailView: View {
@Environment(RemovalHistoryStore.self) private var history
@Environment(HelperModel.self) private var helper
- @Environment(\.controlActiveState) private var controlActiveState
@Environment(\.openSettings) private var openSettings
/// Nil until the disk has answered. An empty value would show every record as gone from the Trash for a
/// moment, and offer to forget records that are still there.
@@ -17,12 +16,11 @@ struct HistoryDetailView: View {
@State private var failures: [(record: RemovalRecord, failure: RestoreFailure)] = []
let batch: RemovalBatch
- /// The id of the page's task: the batch, whether the window is key, and the reloads asked for. When any changes,
- /// the page reads the disk again, since the Trash may have been emptied in the meantime.
+ /// The id of the page's task: the batch, and each time History was read again. When either changes, the page
+ /// reads the disk again, since the Trash may have been emptied in the meantime.
private struct Look: Hashable {
let batch: RemovalBatch
- let isActive: Bool
- let reloads: Int
+ let rereads: Int
}
/// The size of what can still be put back. Until the disk has answered, it is the batch's own total.
@@ -56,8 +54,11 @@ struct HistoryDetailView: View {
}
.navigationTitle(Text(verbatim: batch.title))
.toolbar(removing: .title)
- .task(id: Look(batch: batch, isActive: controlActiveState == .key, reloads: history.reloads)) {
- standing = await RemovalStanding.of(batch.records)
+ .task(id: Look(batch: batch, rereads: history.rereads)) {
+ let standing = await RemovalStanding.of(batch.records)
+ // A look that a newer one replaced leaves its answer.
+ guard !Task.isCancelled else { return }
+ self.standing = standing
}
.task(id: batch.id) {
// Failures belong to the batch they happened in, so another batch's page starts without them. Keyed to
diff --git a/PeelTests/RemovalHistoryStoreTests.swift b/PeelTests/RemovalHistoryStoreTests.swift
index 20770f06..8d9abff8 100644
--- a/PeelTests/RemovalHistoryStoreTests.swift
+++ b/PeelTests/RemovalHistoryStoreTests.swift
@@ -1,10 +1,48 @@
import Foundation
+import Observation
@testable import Peel
import PeelCore
+import Synchronization
import Testing
@MainActor
struct RemovalHistoryStoreTests {
+ private func moved(_ name: String) throws -> TrashResult {
+ let json = #"{"originalURL": "file:///Users/me/\#(name)", "trashedURL": "file:///Users/me/.Trash/\#(name)", "date": 0}"#
+ return TrashResult(trashed: [try JSONDecoder().decode(TrashedItem.self, from: Data(json.utf8))])
+ }
+
+ @Test func readsWhatPeelChangedBeforeTheRemovalsPageLooksInTheTrashAgain() async throws {
+ let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: folder) }
+ let url = folder.appending(path: "removals.json")
+ let history = RemovalHistoryStore(
+ log: RemovalLog(url: url),
+ refusals: RefusalLog(url: folder.appending(path: "refusals.json")),
+ knowsTheExclusions: { true }
+ )
+ let part = RemovalPart(source: "Example", sourceKey: nil, tool: Tool.developer.rawValue)
+ var removal = RemovalInProgress()
+ await history.record(try moved("One"), part: part, sizes: [:], in: &removal)
+ await history.record(try moved("Two"), part: part, sizes: [:], in: &removal)
+ history.finish(removal)
+ await history.load()
+ let records = try #require(history.batches.first?.records)
+ _ = await RemovalLog(url: url).remove([records[0].id])
+
+ let seen = Mutex<Int?>(nil)
+ withObservationTracking({ _ = history.rereads }, onChange: {
+ MainActor.assumeIsolated { seen.withLock { $0 = history.batches.first?.records.count } }
+ })
+ await history.readAgainIfInUse()
+
+ #expect(seen.withLock { $0 } == 1, "the page looked in the Trash before History was read again")
+ _ = await RemovalLog(url: url).remove([records[1].id])
+ await history.readAgainIfInUse()
+ #expect(history.batches.isEmpty)
+ #expect(history.undoTitle == nil, "Undo still offered what peel put back")
+ }
+
@Test func aReadThatFindsNothingNewChangesNothing() async throws {
let folder = FileManager.default.temporaryDirectory.appending(path: "PeelTests-\(UUID().uuidString)")
defer { try? FileManager.default.removeItem(at: folder) }