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.

Write Found to the preferences only when something changed

Țugui Dragoș0e153c89bff5
diff --git a/Packages/PeelCore/Sources/PeelCore/Home/FoundLastTime.swift b/Packages/PeelCore/Sources/PeelCore/Home/FoundLastTime.swift
index 2168631e..6c23146f 100644
--- a/Packages/PeelCore/Sources/PeelCore/Home/FoundLastTime.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Home/FoundLastTime.swift
@@ -37,9 +37,17 @@ public struct FoundLastTime: Sendable, Equatable {
return (try? JSONEncoder().encode(entries)) ?? Data()
}
- /// Records what `tool` found when it looked at `date`. Nothing found takes the tool off the list.
- public mutating func record(count: Int, size: SizeTotal?, for tool: String, at date: Date = .now) {
- findings[tool] = count > 0 ? Finding(count: count, size: size, date: date) : nil
+ /// Records what `tool` found when it looked at `date`, and answers whether that changed anything: the panel says
+ /// when in minutes, so the same finding within a minute is no change. Nothing found takes the tool off the list.
+ @discardableResult
+ public mutating func record(count: Int, size: SizeTotal?, for tool: String, at date: Date = .now) -> Bool {
+ guard count > 0 else { return findings.removeValue(forKey: tool) != nil }
+ if let last = findings[tool], last.count == count, last.size == size,
+ abs(date.timeIntervalSince(last.date)) < 60 {
+ return false
+ }
+ findings[tool] = Finding(count: count, size: size, date: date)
+ return true
}
private struct Stored: Codable {
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/FoundLastTimeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/FoundLastTimeTests.swift
index 6a78812e..517363ab 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/FoundLastTimeTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/FoundLastTimeTests.swift
@@ -41,6 +41,23 @@ struct FoundLastTimeTests {
#expect(found.findings.isEmpty)
}
+ @Test func aLookThatFindsTheSameWithinAMinuteChangesNothing() {
+ var found = FoundLastTime()
+ func changes(_ count: Int, at date: Date) -> Bool {
+ found.record(count: count, size: nil, for: "orphans", at: date)
+ }
+
+ #expect(changes(7, at: monday))
+ #expect(!changes(7, at: monday.addingTimeInterval(59)))
+ #expect(found.findings["orphans"]?.date == monday)
+
+ #expect(changes(8, at: monday.addingTimeInterval(59)))
+ #expect(changes(8, at: tuesday))
+ #expect(found.findings["orphans"]?.date == tuesday)
+ #expect(changes(0, at: tuesday))
+ #expect(!changes(0, at: tuesday))
+ }
+
/// Any process can write an app's preferences, so an entry that makes no sense is left out, and the rest stay.
@Test func readsNothingThatMakesNoSense() {
let json = #"""
diff --git a/Peel/Model/FoundLastTimeStore.swift b/Peel/Model/FoundLastTimeStore.swift
index 3e80144a..9fbe74bb 100644
--- a/Peel/Model/FoundLastTimeStore.swift
+++ b/Peel/Model/FoundLastTimeStore.swift
@@ -19,8 +19,11 @@ final class FoundLastTimeStore {
Tool.allCases.compactMap { tool in found.findings[tool.rawValue].map { (tool, $0) } }
}
+ /// Each write makes every `AppStorage` in the app read again, and a look often finds what the last one did.
func record(_ looked: Looked, for tool: Tool) {
- found.record(count: looked.count, size: looked.size, for: tool.rawValue)
+ var next = found
+ guard next.record(count: looked.count, size: looked.size, for: tool.rawValue) else { return }
+ found = next
defaults.set(found.data, forKey: Self.key)
}
}
diff --git a/PeelTests/FoundLastTimeStoreTests.swift b/PeelTests/FoundLastTimeStoreTests.swift
new file mode 100644
index 00000000..611c5683
--- /dev/null
+++ b/PeelTests/FoundLastTimeStoreTests.swift
@@ -0,0 +1,20 @@
+import Foundation
+@testable import Peel
+import Testing
+
+@MainActor
+struct FoundLastTimeStoreTests {
+ @Test func aLookThatFindsTheSameWritesNothing() throws {
+ let made = try MadeUpDefaults()
+ defer { made.forget() }
+ let store = FoundLastTimeStore(defaults: made.defaults)
+
+ store.record(Looked(count: 3, size: nil), for: .orphans)
+ let written = try #require(made.defaults.data(forKey: "home.foundLastTime"))
+ store.record(Looked(count: 3, size: nil), for: .orphans)
+ #expect(made.defaults.data(forKey: "home.foundLastTime") == written)
+
+ store.record(Looked(count: 4, size: nil), for: .orphans)
+ #expect(made.defaults.data(forKey: "home.foundLastTime") != written)
+ }
+}