Repository Public License
1// ORIX Clean — App/Sources/Features/Cleanup/CleanupPreviewSheet.swift2// Description: Mandatory preview before any removal, and the per-item results afterwards with undo.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ORIXCleanCore5import SwiftUI67struct CleanupPreviewSheet: View {8 @Environment(AppModel.self) private var model9 var request: PreviewRequest10 @State private var excluded: Set<String> = []11 @State private var acknowledged = false12 @State private var typed = ""1314 private var plan: CleanupPlan { CleanupPlan(items: request.plan.items.filter { !excluded.contains($0.path) }) }1516 var body: some View {17 let plan = plan18 let permanent = request.mode == .permanent19 let running = model.runningBundleIDs20 let busy = plan.items.filter { !running.isDisjoint(with: $0.closeFirst) }21 VStack(alignment: .leading, spacing: 0) {22 VStack(alignment: .leading, spacing: ORIX.Space.s) {23 Text(permanent ? L("Delete permanently?") : L("Review before cleaning")).font(.title2.weight(.semibold))24 Text(permanent25 ? L("These items are in the Trash. Deleting them is permanent and cannot be undone by ORIX Clean.")26 : L("These exact items will be moved to the Trash. Space is reclaimed when you empty the Trash. Until then you can put them back."))27 .font(.callout).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true)28 HStack(alignment: .top, spacing: ORIX.Space.l) {29 StatTile(title: L("Items"), value: ByteFormat.count(plan.items.count), detail: L("%@ files", ByteFormat.count(plan.fileCount)))30 StatTile(title: L("Content size"), value: ByteFormat.string(plan.logicalSize), detail: L("Sum of file lengths"))31 StatTile(title: L("Estimated recoverable"), value: plan.recoverable.text,32 detail: plan.recoverable.isExact ? L("Allocated on disk") : L("Range: some files share storage (APFS)"))33 StatTile(title: L("Highest risk"), value: plan.highestRisk.title, detail: nil)34 }35 .padding(.top, 4)36 }37 .padding(ORIX.Space.l)38 Divider()39 List {40 ForEach(plan.byRisk.indices, id: \.self) { i in41 let (risk, items) = plan.byRisk[i]42 Section {43 ForEach(items) { item in row(item, busy: !running.isDisjoint(with: item.closeFirst)) }44 } header: {45 HStack { RiskBadge(risk: risk); Text(L("%@ items · %@", ByteFormat.count(items.count), ByteFormat.string(items.reduce(0) { $0 + $1.measurement.allocated }))) }46 }47 }48 }49 .listStyle(.inset)50 .frame(minHeight: 260)51 .accessibilityIdentifier("preview.list")52 Divider()53 VStack(alignment: .leading, spacing: ORIX.Space.s) {54 if !busy.isEmpty {55 InfoBanner(symbol: "play.circle", text: L("%d items belong to applications that are running. They will be left in place until you quit those applications.", busy.count))56 }57 if plan.highestRisk == .high {58 Toggle(L("I have checked the high-risk items and want to remove them"), isOn: $acknowledged).toggleStyle(.checkbox)59 .accessibilityIdentifier("preview.acknowledge")60 }61 if permanent {62 HStack {63 Text(L("Type DELETE to confirm:")).font(.callout)64 TextField("DELETE", text: $typed).textFieldStyle(.roundedBorder).frame(width: 120).accessibilityIdentifier("preview.type")65 }66 }67 HStack {68 Text(L("Recovery: %@", permanent ? L("none after deletion, except from your own backups") : L("Put Back from the Trash, or Undo in the results")))69 .font(.caption).foregroundStyle(ORIX.Palette.textSecondary)70 Spacer()71 Button(L("Cancel")) { model.cancelPreview(request) }.keyboardShortcut(.cancelAction).accessibilityIdentifier("preview.cancel")72 Button(permanent ? L("Delete Permanently") : L("Move %@ Items to Trash", ByteFormat.count(plan.items.count))) {73 model.confirm(request, plan: plan, highRiskAcknowledged: acknowledged)74 }75 .buttonStyle(.borderedProminent)76 .tint(permanent ? ORIX.Palette.high : ORIX.Palette.accent)77 .disabled(plan.items.isEmpty || (plan.highestRisk == .high && !acknowledged) || (permanent && typed != "DELETE"))78 .accessibilityIdentifier("preview.confirm")79 }80 }81 .padding(ORIX.Space.l)82 }83 .frame(minWidth: 820, idealWidth: 900, minHeight: 600, idealHeight: 680)84 .interactiveDismissDisabled()85 }8687 private func row(_ item: CleanupItem, busy: Bool) -> some View {88 HStack(alignment: .top, spacing: ORIX.Space.s) {89 Checkbox(isOn: true, label: L("Keep %@ in this cleanup", item.name)) { excluded.insert(item.path) }90 FileIcon(path: item.path, size: 20)91 VStack(alignment: .leading, spacing: 2) {92 HStack(spacing: 6) {93 Text(item.path.abbreviatingHome).font(.callout).lineLimit(1).truncationMode(.middle).textSelection(.enabled)94 if busy { Label(L("running"), systemImage: "play.circle").font(.caption).foregroundStyle(ORIX.Palette.review) }95 }96 Text(item.reason).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true)97 Text(item.consequence).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true)98 if let c = item.confidenceNote { Text(c).font(.caption.weight(.medium)).foregroundStyle(ORIX.Palette.review) }99 }100 Spacer()101 VStack(alignment: .trailing, spacing: 1) {102 SizeText(bytes: item.measurement.allocated)103 Text(item.category.title).font(.caption2).foregroundStyle(ORIX.Palette.textTertiary)104 }105 }106 .contextMenu { FileMenu(path: item.path) }107 .accessibilityElement(children: .combine)108 }109}110111struct CleanupResultsSheet: View {112 @Environment(AppModel.self) private var model113 var reportID: UUID114115 var body: some View {116 if let report = model.operations.first(where: { $0.id == reportID }) ?? model.lastReport {117 VStack(alignment: .leading, spacing: 0) {118 VStack(alignment: .leading, spacing: ORIX.Space.s) {119 Text(report.cancelled ? L("Cleanup stopped") : L("Cleanup results")).font(.title2.weight(.semibold))120 HStack(alignment: .top, spacing: ORIX.Space.l) {121 ForEach(DeletionStatus.allCases, id: \.self) { s in122 if report.count(s) > 0 { StatTile(title: s.title, value: ByteFormat.count(report.count(s)), detail: ByteFormat.string(report.allocated(s))) }123 }124 }125 if report.mode == .moveToTrash, report.count(.movedToTrash) > 0 {126 InfoBanner(symbol: "trash", text: L("%@ is now in the Trash and still occupies disk space. It is reclaimed when you empty the Trash.", ByteFormat.string(report.allocated(.movedToTrash))),127 actionTitle: L("Show Trash")) { model.showResults = false; model.section = .trash }128 }129 if let a = report.freeBefore, let b = report.freeAfter {130 Text(L("Free space reported by macOS: %@ before, %@ after.", ByteFormat.string(a), ByteFormat.string(b)))131 .font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary)132 }133 }134 .padding(ORIX.Space.l)135 Divider()136 List(report.records) { r in137 HStack(alignment: .top, spacing: ORIX.Space.s) {138 Image(systemName: symbol(r.status)).foregroundStyle(color(r.status)).frame(width: 18).accessibilityHidden(true)139 VStack(alignment: .leading, spacing: 1) {140 Text(r.path.abbreviatingHome).font(.callout).lineLimit(1).truncationMode(.middle)141 Text("\(r.restored ? L("Put back") : r.status.title) — \(r.detail)").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)142 }143 Spacer()144 SizeText(bytes: r.allocatedSize)145 if r.status == .movedToTrash, !r.restored, r.trashPath != nil {146 Button(L("Put Back")) { model.restore(r, in: report) }.controlSize(.small)147 }148 }149 .accessibilityElement(children: .contain)150 }151 .listStyle(.inset)152 .frame(minHeight: 240)153 .accessibilityIdentifier("results.list")154 Divider()155 HStack {156 Menu(L("Export Report")) {157 ForEach(ReportFormat.allCases, id: \.self) { f in158 Button(f.title) { model.export(Report(title: L("Cleanup results"), scope: L("%d items", report.records.count), sections: ReportBuilder.cleanupResults(report), appVersion: model.appVersion), format: f) }159 }160 }.fixedSize()161 Spacer()162 Button(L("Done")) { model.showResults = false }.keyboardShortcut(.defaultAction).accessibilityIdentifier("results.done")163 }164 .padding(ORIX.Space.l)165 }166 .frame(minWidth: 780, idealWidth: 860, minHeight: 520)167 }168 }169170 private func symbol(_ s: DeletionStatus) -> String {171 switch s {172 case .movedToTrash: return "trash.circle.fill"173 case .permanentlyDeleted: return "xmark.bin.circle.fill"174 case .skipped: return "arrow.uturn.right.circle"175 case .failed: return "exclamationmark.octagon.fill"176 case .requiresManualAction: return "hand.raised.circle.fill"177 }178 }179180 private func color(_ s: DeletionStatus) -> Color {181 switch s {182 case .movedToTrash: return ORIX.Palette.low183 case .permanentlyDeleted: return ORIX.Palette.accentSecondary184 case .skipped: return ORIX.Palette.textSecondary185 case .failed: return ORIX.Palette.high186 case .requiresManualAction: return ORIX.Palette.review187 }188 }189}190