Repository Public License
1// ORIX Clean — App/Sources/Features/Duplicates/DuplicatesView.swift2// Description: Exact duplicate groups with per-group choice of the copy to keep.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ORIXCleanCore5import SwiftUI67struct DuplicatesView: View {8 @Environment(AppModel.self) private var model9 @State private var expanded: Set<String> = []1011 var body: some View {12 @Bindable var model = model13 if model.tree == nil {14 EmptyState(symbol: "doc.on.doc", title: L("No scan yet"), message: L("Scan a folder first. Duplicates are searched inside the scanned location."),15 actionTitle: L("Scan Home Folder")) { model.startScan(path: model.home) }16 } else {17 VStack(spacing: 0) {18 HStack(spacing: ORIX.Space.m) {19 Picker(L("Minimum size"), selection: $model.duplicateMinimumMB) {20 Text(L("Any size")).tag(0)21 ForEach([1, 10, 100, 1000], id: \.self) { Text(ByteFormat.string(Int64($0) * 1_000_000)).tag($0) }22 }.frame(width: 240)23 Button(model.duplicates == nil ? L("Find Duplicates") : L("Search Again")) { model.findDuplicates() }24 .buttonStyle(.borderedProminent).disabled(model.duplicatePhase == .running).accessibilityIdentifier("duplicates.find")25 Spacer()26 if let d = model.duplicates {27 Text(L("%@ groups · %@ in extra copies", ByteFormat.count(d.groups.count), ByteFormat.string(d.redundantLogical)))28 .font(.callout.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary)29 }30 }31 .padding(.horizontal, ORIX.Space.m).padding(.vertical, ORIX.Space.s).background(.bar)32 Divider()33 if model.duplicatePhase == .running {34 let p = model.duplicateProgress35 ProgressLine(title: p.stage == .hashing ? L("Comparing content (SHA-256)…") : L("Narrowing candidates…"),36 detail: L("%@ of %@ files · %@ read", ByteFormat.count(p.filesDone), ByteFormat.count(p.filesTotal), ByteFormat.string(p.bytesHashed)),37 fraction: p.filesTotal > 0 ? Double(p.filesDone) / Double(p.filesTotal) : nil) { model.cancelDuplicates() }38 .padding(ORIX.Space.m)39 }40 if let d = model.duplicates {41 if d.groups.isEmpty {42 EmptyState(symbol: "checkmark.seal", title: L("No duplicates found"),43 message: L("%@ files of the same size were compared; none has identical content.", ByteFormat.count(d.candidates)))44 } else {45 list(d)46 }47 } else if model.duplicatePhase != .running {48 EmptyState(symbol: "doc.on.doc", title: L("Find identical files"),49 message: L("Files are grouped by size, then by a sampled hash, then by a full SHA-256 of their content. Before a copy is removed it is compared byte for byte with the copy you keep."),50 actionTitle: L("Find Duplicates")) { model.findDuplicates() }51 }52 }53 }54 }5556 private func list(_ d: DuplicateResult) -> some View {57 List {58 if !d.unreadable.isEmpty {59 InfoBanner(symbol: "eye.slash", text: L("%d files could not be read or changed during the comparison; they are not in any group.", d.unreadable.count))60 }61 ForEach(d.groups.prefix(400)) { g in62 Section {63 ForEach(g.files) { f in fileRow(f, g) }64 } header: {65 HStack {66 Text(L("%d copies · %@ each", g.files.count, ByteFormat.string(g.logicalSize))).font(.callout.weight(.semibold))67 if g.sharesStorage {68 Label(L("shares storage"), systemImage: "square.on.square.dashed").font(.caption)69 .help(L("At least two of these files are APFS clones: they point to the same blocks. Removing one frees little or nothing."))70 }71 Spacer()72 Text("SHA-256 " + g.digest.prefix(12) + "…").font(.caption.monospaced()).foregroundStyle(ORIX.Palette.textTertiary)73 Menu(L("Keep…")) {74 ForEach(g.files) { f in Button(f.path.abbreviatingHome) { model.keepOnly(f, in: g) } }75 Divider()76 Button(L("Keep All (clear selection)")) { model.clearGroup(g) }77 }78 .menuStyle(.borderlessButton).fixedSize()79 .help(L("Choose the copy that stays; the others are added to the cleanup selection"))80 }81 }82 }83 if d.groups.count > 400 { Text(L("Showing the 400 largest groups of %@.", ByteFormat.count(d.groups.count))).font(.caption).foregroundStyle(ORIX.Palette.textSecondary) }84 }85 .listStyle(.inset)86 .accessibilityIdentifier("duplicates.list")87 }8889 private func fileRow(_ f: DuplicateFile, _ g: DuplicateGroup) -> some View {90 let selected = model.isSelected(f.path)91 return HStack(spacing: ORIX.Space.s) {92 Checkbox(isOn: selected, label: L("Select %@ for cleanup", (f.path as NSString).lastPathComponent)) {93 if selected { model.basket[f.path] = nil; return }94 let remaining = g.files.filter { $0.path != f.path && !model.isSelected($0.path) }95 guard let keep = remaining.first else { model.errorMessage = L("At least one copy of each group stays. Deselect another copy first."); return }96 let before = Set(g.files.filter { model.isSelected($0.path) }.map(\.path))97 model.keepOnly(keep, in: g)98 for other in g.files where other.path != f.path && !before.contains(other.path) { model.basket[other.path] = nil }99 }100 .disabled(!f.hardLinkPaths.isEmpty)101 Thumbnail(path: f.path, side: 30)102 VStack(alignment: .leading, spacing: 1) {103 Text((f.path as NSString).lastPathComponent).lineLimit(1).truncationMode(.middle)104 PathText(path: f.path)105 }106 Spacer()107 if !f.hardLinkPaths.isEmpty {108 Label(L("%d hard links", f.hardLinkPaths.count + 1), systemImage: "link").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)109 .help(L("This file has several names for the same data. Removing one name frees nothing, so it is not offered."))110 } else if f.mayShareBlocks {111 Label(L("may share blocks"), systemImage: "square.on.square.dashed").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)112 }113 Text(ByteFormat.date(f.modified)).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).frame(width: 96, alignment: .trailing)114 if selected { Text(L("to Trash")).font(.caption.weight(.medium)).foregroundStyle(ORIX.Palette.review) }115 else { Text(L("stays")).font(.caption).foregroundStyle(ORIX.Palette.textTertiary) }116 }117 .contextMenu {118 FileMenu(path: f.path)119 Button(L("Keep Only This Copy")) { model.keepOnly(f, in: g) }120 }121 .accessibilityElement(children: .combine)122 }123}124