Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Duplicates keeps one copy of a group by a rule of its model
Țugui Dragoșfdb10bac8f17
diff --git a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swiftindex 91587295..fb9566b4 100644--- a/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift+++ b/Packages/PeelCore/Sources/PeelCore/Duplicates/Duplicates.swift@@ -147,6 +147,12 @@ public struct DuplicateScan: Sendable {Self.keepingOne(of: folderGroups.map { $0.folders.map(\.url) }, selection)}+ /// Whether the copy at `url` may be selected or deselected, given the `selected` copies of its group: one copy of+ /// every group always stays, so the last one not selected cannot be.+ public static func canChange(_ url: URL, among copies: [URL], selected: Set<URL>) -> Bool {+ selected.contains(url) || copies.count { !selected.contains($0) } > 1+ }+private static func keepingOne(of groups: [[URL]], _ selection: Set<URL>) -> Set<URL> {Set(groups.flatMap { copies inlet chosen = copies.filter(selection.contains)diff --git a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swiftindex 2be02c35..b1792301 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/DuplicateFinderTests.swift@@ -5,6 +5,15 @@ import Synchronizationimport Testingstruct DuplicateFinderTests {+ @Test func oneCopyOfAGroupCanNeverBeSelected() {+ let copies = ["/a/one", "/b/one", "/c/one"].map { URL(filePath: $0) }++ #expect(DuplicateScan.canChange(copies[0], among: copies, selected: []))+ #expect(DuplicateScan.canChange(copies[1], among: copies, selected: [copies[0]]))+ #expect(!DuplicateScan.canChange(copies[2], among: copies, selected: [copies[0], copies[1]]))+ #expect(DuplicateScan.canChange(copies[1], among: copies, selected: [copies[0], copies[1]]), "a selected copy can always go back")+ }+/// Verifying reads a mebibyte at a time, and each report is a transaction on the main actor. So reports are/// let through at most once every 50 ms, and the last one is always let through.@Test func reportsReadingAtARateTheScreenCanTake() {diff --git a/Peel/Model/DuplicateLibrary.swift b/Peel/Model/DuplicateLibrary.swiftindex 1a1e4a93..9b08a395 100644--- a/Peel/Model/DuplicateLibrary.swift+++ b/Peel/Model/DuplicateLibrary.swift@@ -173,7 +173,13 @@ final class DuplicateLibrary {selectedURLs.contains(file.url)}- func setSelected(_ isSelected: Bool, _ file: DuplicateFile) {+ func canChange(_ file: DuplicateFile, in group: DuplicateGroup) -> Bool {+ DuplicateScan.canChange(file.url, among: group.files.map(\.url), selected: selectedURLs)+ }++ /// Changes nothing that would leave the group with no copy.+ func setSelected(_ isSelected: Bool, _ file: DuplicateFile, in group: DuplicateGroup) {+ guard canChange(file, in: group) else { return }if isSelected {selectedURLs.insert(file.url)} else {@@ -181,17 +187,32 @@ final class DuplicateLibrary {}}+ /// What the selection in `group` would free.+ func selectedSize(in group: DuplicateGroup) -> Int64 {+ group.files.filter { selectedURLs.contains($0.url) }.map(\.reclaimableSize).cappedSum+ }+func isSelected(_ folder: DuplicateFolder) -> Bool {selectedFolders.contains(folder.url)}- func setSelected(_ isSelected: Bool, _ folder: DuplicateFolder) {+ func canChange(_ folder: DuplicateFolder, in group: DuplicateFolderGroup) -> Bool {+ DuplicateScan.canChange(folder.url, among: group.folders.map(\.url), selected: selectedFolders)+ }++ /// Changes nothing that would leave the group with no copy.+ func setSelected(_ isSelected: Bool, _ folder: DuplicateFolder, in group: DuplicateFolderGroup) {+ guard canChange(folder, in: group) else { return }if isSelected {selectedFolders.insert(folder.url)} else {selectedFolders.remove(folder.url)}}++ func selectedSize(in group: DuplicateFolderGroup) -> Int64 {+ group.folders.filter { selectedFolders.contains($0.url) }.map(\.reclaimableSize).cappedSum+ }}private extension DuplicateScan {diff --git a/Peel/Views/Duplicates/DuplicateFolderGroupView.swift b/Peel/Views/Duplicates/DuplicateFolderGroupView.swiftindex 33024f03..53ffb5ba 100644--- a/Peel/Views/Duplicates/DuplicateFolderGroupView.swift+++ b/Peel/Views/Duplicates/DuplicateFolderGroupView.swift@@ -5,10 +5,6 @@ struct DuplicateFolderGroupView: View {@Environment(DuplicateLibrary.self) private var duplicateslet group: DuplicateFolderGroup- private var selectedSize: Int64 {- group.folders.filter { duplicates.selectedFolders.contains($0.url) }.map(\.reclaimableSize).cappedSum- }-var body: some View {List {header@@ -20,8 +16,9 @@ struct DuplicateFolderGroupView: View {DuplicateFolderRow(duplicates: duplicates,folder: folder,+ group: group,isSelected: duplicates.isSelected(folder),- canChangeSelection: duplicates.selectedFolders.contains(folder.url) || keptCount > 1,+ canChangeSelection: duplicates.canChange(folder, in: group),isFirst: index == 0)}@@ -54,8 +51,7 @@ struct DuplicateFolderGroupView: View {}}Spacer(minLength: 8)- TotalLabel(bytes: selectedSize, caption: Text("to free"))- .accessibilityLabel(Text("\(selectedSize.byteCount) can be freed"))+ DuplicateSelectedTotal(bytes: duplicates.selectedSize(in: group))}.padding(.vertical, 8)}@@ -72,22 +68,19 @@ struct DuplicateFolderGroupView: View {.frame(width: 92, height: 80).accessibilityHidden(true)}-- private var keptCount: Int {- group.folders.count { !duplicates.selectedFolders.contains($0.url) }- }}private struct DuplicateFolderRow: View {let duplicates: DuplicateLibrarylet folder: DuplicateFolder+ let group: DuplicateFolderGrouplet isSelected: Boollet canChangeSelection: Boolvar isFirst = falsevar body: some View {HStack(spacing: 0) {- Toggle(isOn: Binding(get: { isSelected }, set: { duplicates.setSelected($0, folder) })) {+ Toggle(isOn: Binding(get: { isSelected }, set: { duplicates.setSelected($0, folder, in: group) })) {HStack(alignment: .firstTextBaseline, spacing: 10) {Image(systemName: "folder.fill").foregroundStyle(Color.accentColor)diff --git a/Peel/Views/Duplicates/DuplicateGroupView.swift b/Peel/Views/Duplicates/DuplicateGroupView.swiftindex 1a4b2e11..782d9a52 100644--- a/Peel/Views/Duplicates/DuplicateGroupView.swift+++ b/Peel/Views/Duplicates/DuplicateGroupView.swift@@ -7,11 +7,6 @@ struct DuplicateGroupView: View {@State private var previewURL: URL?let group: DuplicateGroup- /// What the selection in this group would free.- private var selectedSize: Int64 {- group.files.filter { duplicates.selectedURLs.contains($0.url) }.map(\.reclaimableSize).cappedSum- }-var body: some View {List {header@@ -23,8 +18,9 @@ struct DuplicateGroupView: View {DuplicateFileRow(duplicates: duplicates,file: file,+ group: group,isSelected: duplicates.isSelected(file),- canChangeSelection: duplicates.selectedURLs.contains(file.url) || keptCount > 1,+ canChangeSelection: duplicates.canChange(file, in: group),isFirst: index == 0,onPreview: { previewURL = file.url })@@ -67,8 +63,7 @@ struct DuplicateGroupView: View {}Spacer(minLength: 8)// The selection's total, not the group's, so the figure changes as the user selects or deselects copies.- TotalLabel(bytes: selectedSize, caption: Text("to free"))- .accessibilityLabel(Text("\(selectedSize.byteCount) can be freed"))+ DuplicateSelectedTotal(bytes: duplicates.selectedSize(in: group))}.padding(.vertical, 8)}@@ -93,10 +88,6 @@ struct DuplicateGroupView: View {.accessibilityHidden(true)}- private var keptCount: Int {- group.files.count { !duplicates.selectedURLs.contains($0.url) }- }-/// True when a copy other than the first, which Peel suggests keeping, shares its blocks with something else./// This checks blocks, not sizes: a compressed or sparse file is smaller on disk without sharing anything.private var sharesStorage: Bool {@@ -107,6 +98,7 @@ struct DuplicateGroupView: View {private struct DuplicateFileRow: View {let duplicates: DuplicateLibrarylet file: DuplicateFile+ let group: DuplicateGrouplet isSelected: Boollet canChangeSelection: Boolvar isFirst = false@@ -114,7 +106,7 @@ private struct DuplicateFileRow: View {var body: some View {HStack(spacing: 10) {- Toggle(isOn: Binding(get: { isSelected }, set: { duplicates.setSelected($0, file) })) {+ Toggle(isOn: Binding(get: { isSelected }, set: { duplicates.setSelected($0, file, in: group) })) {HStack(alignment: .firstTextBaseline, spacing: 10) {FileThumbnail(url: file.url).frame(width: 20, height: 20)@@ -157,3 +149,13 @@ private struct DuplicateFileRow: View {}}}++/// A group's total to free, which follows the copies selected in it.+struct DuplicateSelectedTotal: View {+ let bytes: Int64++ var body: some View {+ TotalLabel(bytes: bytes, caption: Text("to free"))+ .accessibilityLabel(Text("\(bytes.byteCount) can be freed"))+ }+}