SPB Git

Repository Public License

CandidateViews.swift 11.9 KB 216 lines swift

1// ORIX Clean — App/Sources/Features/Cleanup/CandidateViews.swift2// Description: Candidate lists for caches, developer data and downloads. Every row states reason, risk and consequence.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ORIXCleanCore5import SwiftUI67/// One candidate with its explanation. Used by every cleanup list.8struct CandidateRow: View {9    @Environment(AppModel.self) private var model10    var item: CleanupItem11    @State private var expanded = false1213    var body: some View {14        let selected = model.isSelected(item.path)15        VStack(alignment: .leading, spacing: 4) {16            HStack(spacing: ORIX.Space.s) {17                Checkbox(isOn: selected, label: L("Select %@ for cleanup", item.name)) { model.toggle(item) }18                FileIcon(path: item.path, size: 22)19                VStack(alignment: .leading, spacing: 1) {20                    HStack(spacing: 6) {21                        Text(item.owner.map { $0 == item.name ? $0 : "\($0) — \(item.name)" } ?? item.name).lineLimit(1).truncationMode(.middle)22                        RiskBadge(risk: item.risk)23                        if !item.closeFirst.isEmpty, !model.runningBundleIDs.isDisjoint(with: item.closeFirst) {24                            Label(L("running"), systemImage: "play.circle").font(.caption).foregroundStyle(ORIX.Palette.review)25                                .help(L("The application is running. Quit it before cleaning this item."))26                        }27                    }28                    PathText(path: item.path)29                }30                Spacer(minLength: ORIX.Space.s)31                if let d = item.measurement.modified {32                    Text(ByteFormat.date(d)).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).frame(width: 92, alignment: .trailing)33                }34                VStack(alignment: .trailing, spacing: 0) {35                    SizeText(bytes: item.measurement.allocated)36                    if item.measurement.files > 1 { Text(L("%@ files", ByteFormat.count(item.measurement.files))).font(.caption2.monospacedDigit()).foregroundStyle(ORIX.Palette.textTertiary) }37                }38                .frame(width: 100, alignment: .trailing)39                Button { withAnimation(.snappy(duration: 0.15)) { expanded.toggle() } } label: { Image(systemName: expanded ? "chevron.up" : "info.circle") }40                    .buttonStyle(.borderless).help(L("Why it is listed and what happens if you remove it"))41                    .accessibilityLabel(L("Details"))42            }43            if expanded {44                VStack(alignment: .leading, spacing: 3) {45                    detail(L("Why it is listed"), item.reason)46                    detail(L("If you remove it"), item.consequence)47                    if let r = item.regeneration { detail(L("Getting it back"), r) }48                    if let c = item.confidenceNote { detail(L("Certainty"), c) }49                    if item.measurement.incomplete { detail(L("Measurement"), L("Part of this item could not be read; its size is a lower bound.")) }50                    detail(L("Estimated recoverable"), item.measurement.recoverable.text)51                }52                .padding(.leading, 56).padding(.bottom, 4)53            }54        }55        .contextMenu { FileMenu(path: item.path) }56        .accessibilityElement(children: .contain)57    }5859    private func detail(_ k: String, _ v: String) -> some View {60        HStack(alignment: .firstTextBaseline, spacing: 6) {61            Text(k).font(.caption.weight(.semibold)).foregroundStyle(ORIX.Palette.textSecondary).frame(width: 150, alignment: .leading)62            Text(v).font(.caption).fixedSize(horizontal: false, vertical: true).textSelection(.enabled)63        }64    }65}6667struct CandidateHeader: View {68    @Environment(AppModel.self) private var model69    var title: String70    var subtitle: String71    var items: [CleanupItem]72    var phase: TaskPhase73    var refresh: () -> Void7475    var body: some View {76        HStack(alignment: .top, spacing: ORIX.Space.m) {77            SectionTitle(title: title, subtitle: subtitle)78            Spacer()79            if phase == .running { ProgressView().controlSize(.small) }80            VStack(alignment: .trailing, spacing: 2) {81                Text(ByteFormat.string(items.reduce(0) { $0 + $1.measurement.allocated })).font(.title3.weight(.semibold).monospacedDigit())82                Text(L("%@ items found", ByteFormat.count(items.count))).font(.caption).foregroundStyle(ORIX.Palette.textSecondary)83            }84            Menu(L("Select")) {85                Button(L("All Low-Risk Items")) { model.select(items.filter { $0.risk == .low }) }86                Button(L("None")) { model.deselect(items.map(\.path)) }87            }.fixedSize()88            Button { refresh() } label: { Image(systemName: "arrow.clockwise") }.help(L("Measure again")).accessibilityLabel(L("Measure again")).disabled(phase == .running)89        }90        .padding(ORIX.Space.l)91    }92}9394struct CachesView: View {95    @Environment(AppModel.self) private var model96    var body: some View {97        VStack(spacing: 0) {98            CandidateHeader(title: L("Caches & Logs"),99                            subtitle: L("Recreatable data in documented locations. Apple system caches and caches that hold application state are never listed. Removing caches does not make a Mac faster; it returns space that applications may use again."),100                            items: model.caches, phase: model.cachesPhase) { model.loadCaches(force: true) }101            Divider()102            if model.caches.isEmpty, model.cachesPhase == .done {103                EmptyState(symbol: "internaldrive", title: L("Nothing to clean"), message: L("No cache or log location above 64 KB was found in the locations ORIX Clean knows."))104            } else {105                List {106                    ForEach([CleanupCategory.appCache, .browserCache, .logs, .temporary], id: \.self) { cat in107                        let list = model.caches.filter { $0.category == cat }108                        if !list.isEmpty {109                            Section("\(cat.title) · \(ByteFormat.string(list.reduce(0) { $0 + $1.measurement.allocated }))") {110                                ForEach(list) { CandidateRow(item: $0) }111                            }112                        }113                    }114                }115                .listStyle(.inset)116                .accessibilityIdentifier("caches.list")117            }118        }119        .onAppear { model.loadCaches() }120    }121}122123struct DeveloperView: View {124    @Environment(AppModel.self) private var model125    var body: some View {126        VStack(spacing: 0) {127            CandidateHeader(title: L("Developer Cleanup Lab"),128                            subtitle: L("Build products, package caches and device support files, each with what it costs to regenerate. Simulators, runtimes and toolchains are never removed here."),129                            items: model.developer + model.buildArtifacts, phase: model.developerPhase) { model.loadDeveloper(force: true) }130            Divider()131            if model.developer.isEmpty, model.buildArtifacts.isEmpty, model.developerHints.isEmpty, model.developerPhase == .done {132                EmptyState(symbol: "hammer", title: L("No developer data found"), message: L("None of the known developer tool locations holds data on this Mac."))133            } else {134                List {135                    let owners = Dictionary(grouping: model.developer, by: { $0.owner ?? "" })136                    ForEach(owners.keys.sorted { (owners[$0]!.reduce(0) { $0 + $1.measurement.allocated }) > (owners[$1]!.reduce(0) { $0 + $1.measurement.allocated }) }, id: \.self) { owner in137                        Section("\(owner) · \(ByteFormat.string(owners[owner]!.reduce(0) { $0 + $1.measurement.allocated }))") {138                            ForEach(owners[owner]!) { CandidateRow(item: $0) }139                        }140                    }141                    if !model.buildArtifacts.isEmpty {142                        Section(L("Build folders in the scanned location · %@", ByteFormat.string(model.buildArtifacts.reduce(0) { $0 + $1.measurement.allocated }))) {143                            ForEach(model.buildArtifacts) { CandidateRow(item: $0) }144                        }145                    }146                    if !model.developerHints.isEmpty {147                        Section(L("Reported only — clean with the tool that owns it")) {148                            ForEach(model.developerHints) { h in149                                HStack(alignment: .top, spacing: ORIX.Space.s) {150                                    Image(systemName: "hand.raised").foregroundStyle(ORIX.Palette.textSecondary).frame(width: 22).accessibilityHidden(true)151                                    VStack(alignment: .leading, spacing: 2) {152                                        Text(h.owner)153                                        PathText(path: h.path + "/x")154                                        Text(h.guidance).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).textSelection(.enabled)155                                    }156                                    Spacer()157                                    SizeText(bytes: h.measurement.allocated)158                                }159                                .contextMenu { FileMenu(path: h.path) }160                                .accessibilityElement(children: .combine)161                            }162                        }163                    }164                }165                .listStyle(.inset)166                .accessibilityIdentifier("developer.list")167            }168        }169        .onAppear { model.loadDeveloper() }170    }171}172173struct DownloadsView: View {174    @Environment(AppModel.self) private var model175    @State private var olderThan = 0176177    var body: some View {178        let cutoff = Date().addingTimeInterval(-Double(olderThan) * 86400)179        let entries = model.downloads.filter { olderThan == 0 || ($0.added ?? $0.measurement.modified ?? Date()) < cutoff }180        VStack(spacing: 0) {181            CandidateHeader(title: L("Downloads & Archives"),182                            subtitle: L("Installers, disk images, archives and incomplete downloads in your Downloads folder. Everything here is “review required”: only you know what you still need."),183                            items: entries.map { $0.cleanupItem() }, phase: model.downloadsPhase) { model.loadDownloads(force: true) }184            HStack {185                Picker(L("Added"), selection: $olderThan) {186                    Text(L("Any time")).tag(0)187                    Text(L("More than 30 days ago")).tag(30)188                    Text(L("More than 90 days ago")).tag(90)189                    Text(L("More than 1 year ago")).tag(365)190                }.frame(width: 280)191                Spacer()192            }.padding(.horizontal, ORIX.Space.l).padding(.bottom, ORIX.Space.s)193            Divider()194            if case let .failed(message) = model.downloadsPhase {195                EmptyState(symbol: "lock", title: L("Downloads is not readable"), message: message)196            } else if entries.isEmpty, model.downloadsPhase == .done {197                EmptyState(symbol: "arrow.down.circle", title: L("Nothing here"), message: L("The Downloads folder has no item matching this filter."))198            } else {199                List {200                    ForEach(DownloadEntry.Kind.allCases, id: \.self) { kind in201                        let list = entries.filter { $0.kind == kind }202                        if !list.isEmpty {203                            Section("\(kind.title) · \(ByteFormat.string(list.reduce(0) { $0 + $1.measurement.allocated }))") {204                                ForEach(list) { CandidateRow(item: $0.cleanupItem()) }205                            }206                        }207                    }208                }209                .listStyle(.inset)210                .accessibilityIdentifier("downloads.list")211            }212        }213        .onAppear { model.loadDownloads() }214    }215}216
Files main
Menu

Theme