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.

Opening an app no longer counts as a change to the list of apps

Țugui Dragoș60a81eefff63
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
index 4455c17d..f9524e86 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/AppCatalog.swift
@@ -111,6 +111,14 @@ public enum AppCatalog {
apps.sorted(by: byName)
}
+ /// Whether `reading` lists the same apps as `listed`, each the same build in the same place, whatever each says
+ /// about when it was last opened: that changes every time an app is opened, and says nothing about what is
+ /// installed. Both are in the order `sorted` keeps.
+ public static func listsTheSameApps(_ reading: [InstalledApp], as listed: [InstalledApp]) -> Bool {
+ reading.count == listed.count
+ && zip(reading, listed).allSatisfy { $0.withLastUsedDate(nil) == $1.withLastUsedDate(nil) }
+ }
+
static func appBundles(in directory: URL, maximumDepth: Int = 3) -> [URL] {
guard let enumerator = FileManager.default.enumerator(
at: directory,
diff --git a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
index 4077c032..ceced476 100644
--- a/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Apps/IntelInspector.swift
@@ -19,7 +19,6 @@ public struct IntelFinding: Sendable, Hashable, Identifiable {
public let owner: String?
/// Nil when the size is not known because measuring it timed out or was refused. Unknown is not zero.
public let size: Int64?
- public let lastUsedDate: Date?
public var id: URL { url }
}
@@ -109,7 +108,6 @@ public enum IntelInspector {
name: app.name,
owner: nil,
size: await measure(app.url),
- lastUsedDate: app.lastUsedDate,
))
continue
}
@@ -120,7 +118,6 @@ public enum IntelInspector {
name: url.lastPathComponent,
owner: app.name,
size: await measure(url),
- lastUsedDate: app.lastUsedDate,
))
}
}
@@ -132,7 +129,6 @@ public enum IntelInspector {
name: plugin.name,
owner: nil,
size: plugin.size,
- lastUsedDate: nil,
))
}
@@ -143,7 +139,6 @@ public enum IntelInspector {
name: url.lastPathComponent,
owner: nil,
size: await measure(url),
- lastUsedDate: nil,
))
}
@@ -156,7 +151,6 @@ public enum IntelInspector {
name: item.label,
owner: item.plistURL?.lastPathComponent,
size: await measure(program),
- lastUsedDate: nil,
))
}
@@ -167,7 +161,6 @@ public enum IntelInspector {
name: url.lastPathComponent,
owner: nil,
size: await measure(url),
- lastUsedDate: nil,
))
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
index 84c1c246..5777d53b 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/AppCatalogTests.swift
@@ -3,8 +3,32 @@ import Foundation
import Testing
struct AppCatalogTests {
- private func app(_ name: String) -> InstalledApp {
- InstalledApp(url: URL(filePath: "/Applications/\(name).app", directoryHint: .isDirectory), bundleIdentifier: "org.example.\(name)", name: name)
+ private let yesterday = Date(timeIntervalSince1970: 1_800_000_000)
+ private let today = Date(timeIntervalSince1970: 1_800_086_400)
+
+ private func app(_ name: String, version: String = "1.0", lastUsed: Date? = nil) -> InstalledApp {
+ InstalledApp(
+ url: URL(filePath: "/Applications/\(name).app", directoryHint: .isDirectory),
+ bundleIdentifier: "org.example.\(name)", name: name, version: version, lastUsedDate: lastUsed
+ )
+ }
+
+ /// Opening an app changes when it was last opened and nothing about what is installed, so the pages that scan
+ /// again whenever the apps change are not told they did.
+ @Test func anAppOpenedAgainListsTheSameApps() {
+ let listed = [app("Tunewell", lastUsed: yesterday), app("Zed")]
+ let reading = [app("Tunewell", lastUsed: today), app("Zed", lastUsed: today)]
+
+ #expect(AppCatalog.listsTheSameApps(reading, as: listed))
+ }
+
+ @Test func anotherBuildAnAppAddedOrAnAppGoneIsAnotherList() {
+ let listed = [app("Tunewell"), app("Zed")]
+
+ #expect(!AppCatalog.listsTheSameApps([app("Tunewell", version: "2.0"), app("Zed")], as: listed))
+ #expect(!AppCatalog.listsTheSameApps([app("Arc"), app("Tunewell"), app("Zed")], as: listed))
+ #expect(!AppCatalog.listsTheSameApps([app("Tunewell")], as: listed))
+ #expect(AppCatalog.listsTheSameApps(listed, as: listed))
}
/// A folder's URL ends in a slash, as the catalog reads it, and a plug-in inside the app still belongs to it.
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
index f1830bbf..1649d7d4 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/SearchingTests.swift
@@ -65,7 +65,6 @@ struct SearchingTests {
name: "Widget.plugin",
owner: "Photoshop",
size: 10,
- lastUsedDate: nil,
)
#expect(finding.matches("widget"))
@@ -89,7 +88,7 @@ struct SearchingTests {
@Test func anEmptyQueryKeepsEverything() {
#expect(duplicates("/a/b.txt").matches(""))
#expect(CloudFile(url: URL(filePath: "/a"), name: "a", container: "c", size: 1, modified: nil).matches(""))
- #expect(IntelFinding(url: URL(filePath: "/a"), kind: .app, name: "a", owner: nil, size: 1, lastUsedDate: nil).matches(""))
+ #expect(IntelFinding(url: URL(filePath: "/a"), kind: .app, name: "a", owner: nil, size: 1).matches(""))
}
/// File Search is for finding what the user made. What an app keeps in a Library folder is listed last and
diff --git a/Peel/Model/AppLibrary.swift b/Peel/Model/AppLibrary.swift
index bcc00003..bef0c4eb 100644
--- a/Peel/Model/AppLibrary.swift
+++ b/Peel/Model/AppLibrary.swift
@@ -66,9 +66,13 @@ final class AppLibrary {
static let unusedMonths = 6
private(set) var apps: [InstalledApp] = []
- /// Incremented whenever the list changes, or anything about one of its apps does. Work is keyed to this
- /// rather than to `apps.count`, because an upgrade replaces a bundle without changing the count.
+ /// Incremented whenever the apps listed change: one installed, removed, moved, or replaced by another build.
+ /// Work is keyed to this rather than to `apps.count`, because an upgrade replaces a bundle without changing the
+ /// count. When an app was last opened is not part of it, since opening an app changes nothing that is installed.
private(set) var revision = 0
+ /// Incremented when only the dates the apps were last opened changed, which the list shows, sorts by, and
+ /// filters on.
+ private(set) var lastOpenedRevision = 0
private(set) var isLoading = false
private(set) var hasLoaded = false
private(set) var updateStatuses: [InstalledApp.ID: UpdateStatus] = [:] {
@@ -179,7 +183,13 @@ final class AppLibrary {
func refresh() async -> [InstalledApp] {
guard let found = await scanRun.run({ await AppCatalog.installedApps() }) else { return [] }
lastRead = .now
- guard AppCatalog.sorted(found + stillThere(revealed, beside: found)) != apps else { return [] }
+ let listed = AppCatalog.sorted(found + stillThere(revealed, beside: found))
+ guard listed != apps else { return [] }
+ guard !AppCatalog.listsTheSameApps(listed, as: apps) else {
+ apps = listed
+ lastOpenedRevision += 1
+ return []
+ }
let changed = adopt(found)
// Remembered whenever the list changes, so an app installed and removed between two visits to Orphaned
// Files is still known. Done last, so nothing above is left half done while this waits.
@@ -329,6 +339,7 @@ final class AppLibrary {
private struct VisibleKey: Hashable {
let revision: Int
+ let lastOpened: Int
let updates: Int
let sizes: Int
let sort: AppSort
@@ -346,6 +357,7 @@ final class AppLibrary {
func visibleApps(matching searchText: String) -> VisibleApps {
let key = VisibleKey(
revision: revision,
+ lastOpened: lastOpenedRevision,
updates: updatesRevision,
sizes: sort == .size ? sizesRevision : 0,
sort: sort,
diff --git a/Peel/Views/Intel/IntelDetailView.swift b/Peel/Views/Intel/IntelDetailView.swift
index a4192846..46bad21a 100644
--- a/Peel/Views/Intel/IntelDetailView.swift
+++ b/Peel/Views/Intel/IntelDetailView.swift
@@ -10,6 +10,15 @@ struct IntelDetailView: View {
AppCatalog.app(holding: finding.url, among: library.apps)
}
+ /// When the app was last opened, for an app or a bundle inside one. Read from the list, which follows it, since
+ /// a new date is no reason to scan again.
+ private var lastOpened: Date? {
+ switch finding.kind {
+ case .app, .insideApp: app?.lastUsedDate
+ case .plugin, .driver, .backgroundItem, .commandLineTool: nil
+ }
+ }
+
var body: some View {
Form {
Section {
@@ -62,9 +71,9 @@ struct IntelDetailView: View {
LabeledContent("Size") {
Text(finding.size.byteCount).monospacedDigit()
}
- if let lastUsedDate = finding.lastUsedDate {
+ if let lastOpened {
LabeledContent("Last opened") {
- Text(lastUsedDate, format: .relative(presentation: .named))
+ Text(lastOpened, format: .relative(presentation: .named))
}
}
if let app, let status = library.shownUpdateStatus(of: app) {