SPB Git

Repository Public License

ApplicationsView.swift 8.8 KB 166 lines swift

1// ORIX Clean — App/Sources/Features/Applications/ApplicationsView.swift2// Description: Uninstall assistant (application + associated files with stated certainty) and leftover detection.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ORIXCleanCore5import SwiftUI67struct ApplicationsView: View {8    @Environment(AppModel.self) private var model9    @State private var tab = 010    @State private var search = ""11    @State private var sortBySize = true1213    var body: some View {14        VStack(spacing: 0) {15            HStack {16                Picker("", selection: $tab) {17                    Text(L("Installed Applications")).tag(0)18                    Text(L("Leftover Files")).tag(1)19                }.pickerStyle(.segmented).labelsHidden().frame(width: 360)20                Spacer()21                if tab == 0 {22                    Toggle(L("Largest first"), isOn: $sortBySize).toggleStyle(.checkbox)23                    TextField(L("Search applications"), text: $search).textFieldStyle(.roundedBorder).frame(width: 200)24                }25            }26            .padding(.horizontal, ORIX.Space.m).padding(.vertical, ORIX.Space.s).background(.bar)27            Divider()28            if tab == 0 { installed } else { leftovers }29        }30        .onAppear { model.loadApps() }31    }3233    private var installed: some View {34        @Bindable var model = model35        let apps = model.apps.filter { search.isEmpty || $0.name.localizedCaseInsensitiveContains(search) || $0.bundleID.localizedCaseInsensitiveContains(search) }36            .sorted { sortBySize ? ($0.measurement?.allocated ?? 0) > ($1.measurement?.allocated ?? 0) : $0.name.localizedCaseInsensitiveCompare($1.name) == .orderedAscending }37        return HSplitView {38            List(apps, selection: $model.selectedApp) { app in39                HStack(spacing: ORIX.Space.s) {40                    FileIcon(path: app.path, size: 28)41                    VStack(alignment: .leading, spacing: 1) {42                        Text(app.name).lineLimit(1)43                        Text(app.isSystem ? L("Part of macOS") : app.version).font(.caption).foregroundStyle(ORIX.Palette.textSecondary)44                    }45                    Spacer()46                    if let m = app.measurement { Text(ByteFormat.string(m.allocated)).font(.callout.monospacedDigit()) } else { ProgressView().controlSize(.mini) }47                }48                .tag(app.path)49                .accessibilityElement(children: .combine)50            }51            .listStyle(.inset)52            .frame(minWidth: 280, idealWidth: 340, maxWidth: 440)53            .accessibilityIdentifier("apps.list")54            Group {55                if let app = model.apps.first(where: { $0.path == model.selectedApp }) {56                    AppDetail(app: app)57                } else {58                    EmptyState(symbol: "square.grid.2x2", title: L("Choose an application"),59                               message: L("ORIX Clean shows its size, who signed it, and the files in your Library that carry its identifier. Your documents are never part of an uninstall."))60                }61            }62            .frame(minWidth: 420)63        }64    }6566    private var leftovers: some View {67        VStack(spacing: 0) {68            CandidateHeader(title: L("Leftover Files"),69                            subtitle: L("Library items named after an application identifier for which no installed application was found. This is an inference from names: command-line tools, plug-ins and apps on other volumes can look like leftovers. Review each one."),70                            items: model.leftovers.map(\.cleanupItem), phase: model.leftoversPhase) { model.loadLeftovers() }71            Divider()72            if model.leftoversPhase == .idle {73                EmptyState(symbol: "questionmark.folder", title: L("Look for leftover files"), message: L("Compares identifiers found in your Library with the installed applications."),74                           actionTitle: L("Search")) { model.loadLeftovers() }75            } else if model.leftovers.isEmpty, model.leftoversPhase == .done {76                EmptyState(symbol: "checkmark.seal", title: L("No likely leftovers"), message: L("Every identifier found in your Library matches an installed application or its vendor."))77            } else {78                List { ForEach(model.leftovers) { CandidateRow(item: $0.cleanupItem) } }.listStyle(.inset).accessibilityIdentifier("leftovers.list")79            }80        }81    }82}8384struct AppDetail: View {85    @Environment(AppModel.self) private var model86    var app: InstalledApp8788    var body: some View {89        let files = model.associated[app.path]90        ScrollView {91            VStack(alignment: .leading, spacing: ORIX.Space.l) {92                HStack(spacing: ORIX.Space.m) {93                    FileIcon(path: app.path, size: 64)94                    VStack(alignment: .leading, spacing: 2) {95                        Text(app.name).font(.title2.weight(.semibold))96                        Text(app.bundleID).font(.callout.monospaced()).foregroundStyle(ORIX.Palette.textSecondary).textSelection(.enabled)97                        PathText(path: app.path + "/x")98                    }99                    Spacer()100                    if model.runningBundleIDs.contains(app.bundleID) { Label(L("running"), systemImage: "play.circle").foregroundStyle(ORIX.Palette.review) }101                }102                Card {103                    Grid(alignment: .leading, horizontalSpacing: ORIX.Space.l, verticalSpacing: 6) {104                        fact(L("Version"), "\(app.version) (\(app.build))")105                        fact(L("Size on disk"), app.measurement.map { ByteFormat.string($0.allocated) } ?? "…")106                        fact(L("Signed by"), signatureText)107                        fact(L("Developer team"), app.signature?.teamID ?? "—")108                        fact(L("Last used"), app.lastUsed.map(ByteFormat.date) ?? L("Not recorded by Spotlight"))109                    }110                }111                if app.isSystem {112                    InfoBanner(symbol: "lock.shield", text: L("This application is part of macOS. ORIX Clean does not remove system applications."))113                } else {114                    Card {115                        VStack(alignment: .leading, spacing: ORIX.Space.s) {116                            SectionTitle(title: L("Associated files"), subtitle: L("Found in your Library. The certainty of each match is stated; name-only matches are high risk and never preselected."))117                            if let files {118                                if files.isEmpty { Text(L("No associated file found.")).font(.callout).foregroundStyle(ORIX.Palette.textSecondary) }119                                ForEach(AppInventory.uninstallItems(app: app, files: files).dropFirst()) { CandidateRow(item: $0) }120                            } else {121                                ProgressView().controlSize(.small)122                            }123                        }124                    }125                    HStack {126                        Spacer()127                        Button(L("Select Application Only")) { selectForUninstall(files: []) }128                        Button(L("Select Application and Identifier-Matched Files")) { selectForUninstall(files: (files ?? []).filter { $0.confidence == .identifier }) }129                            .buttonStyle(.borderedProminent).disabled(files == nil)130                            .accessibilityIdentifier("apps.selectUninstall")131                    }132                    Text(L("Selecting adds the items to the cleanup selection. Nothing is removed until you review and confirm."))133                        .font(.caption).foregroundStyle(ORIX.Palette.textSecondary).frame(maxWidth: .infinity, alignment: .trailing)134                }135            }136            .padding(ORIX.Space.l)137        }138        .onAppear { model.loadAssociated(for: app) }139        .onChange(of: app.path) { model.loadAssociated(for: app) }140    }141142    private var signatureText: String {143        guard let s = app.signature else { return "…" }144        let validity = s.valid ? "" : " — " + L("signature does not validate")145        switch s.kind {146        case .apple: return L("Apple (system software)") + validity147        case .appStore: return L("Mac App Store") + validity148        case .developerID: return (s.authority ?? "Developer ID") + validity149        case .adHoc: return L("Ad-hoc signature (no identity)")150        case .unsigned: return L("Not signed")151        case .other: return (s.authority ?? L("Other certificate")) + validity152        }153    }154155    private func fact(_ k: String, _ v: String) -> some View {156        GridRow {157            Text(k).font(.callout).foregroundStyle(ORIX.Palette.textSecondary)158            Text(v).font(.callout).textSelection(.enabled)159        }160    }161162    private func selectForUninstall(files: [AssociatedFile]) {163        model.select(AppInventory.uninstallItems(app: app, files: files))164    }165}166
Files main
Menu

Theme