Repository Public License
1// ORIX Clean — App/Sources/Features/Overview/OverviewView.swift2// Description: Dashboard: volumes, scan targets, measured breakdown by type, evidence-based recommendations.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Charts5import ORIXCleanCore6import SwiftUI78struct OverviewView: View {9 @Environment(AppModel.self) private var model10 @AppStorage("mapPalette") private var paletteName = MapPalette.orix.rawValue11 private var palette: MapPalette { MapPalette(rawValue: paletteName) ?? .orix }12 @AppStorage("onboarded") private var onboarded = false1314 private func loadAll() { model.loadCaches(); model.loadDeveloper(); model.loadDownloads(); model.loadTrash() }1516 var body: some View {17 ScrollView {18 VStack(alignment: .leading, spacing: ORIX.Space.l) {19 if !model.fullDiskAccess {20 InfoBanner(symbol: "lock", text: L("Full Disk Access is off. Folders that macOS protects (Mail, Safari data, the Trash, other apps' containers) are not read and are missing from totals."),21 actionTitle: L("Open Settings")) { NSWorkspace.shared.open(Permissions.fullDiskAccessSettingsURL) }22 }23 volumesCard24 scanCard25 if let scan = model.scan { breakdownCard(scan) }26 recommendationsCard27 }28 .padding(ORIX.Space.xl)29 .frame(maxWidth: 1100)30 .frame(maxWidth: .infinity)31 }32 // Nothing is read before the first-launch sheet is dismissed.33 .onAppear { if onboarded || model.launch.skipOnboarding { loadAll() } }34 .onChange(of: onboarded) { if onboarded { loadAll() } }35 }3637 private var volumesCard: some View {38 Card {39 VStack(alignment: .leading, spacing: ORIX.Space.m) {40 SectionTitle(title: L("Volumes"), subtitle: L("Capacity and free space as reported by macOS right now."))41 ForEach(model.volumes) { v in42 VStack(alignment: .leading, spacing: 5) {43 HStack {44 Image(systemName: v.isStartup ? "internaldrive.fill" : (v.isNetwork ? "network" : "externaldrive.fill")).foregroundStyle(ORIX.Palette.accent).accessibilityHidden(true)45 Text(v.name).font(.headline)46 Text(v.format.uppercased()).font(.caption2.weight(.semibold)).padding(.horizontal, 5).padding(.vertical, 1)47 .background(ORIX.Palette.hairline, in: Capsule())48 if v.isReadOnly { Text(L("Read-only")).font(.caption2).foregroundStyle(ORIX.Palette.textSecondary) }49 Spacer()50 Text(L("%@ free of %@", ByteFormat.string(v.available), ByteFormat.string(v.total))).font(.callout.monospacedDigit())51 Button(L("Scan")) { model.startScan(path: v.path); model.section = .map }52 .controlSize(.small).disabled(model.scanPhase == .running)53 .accessibilityLabel(L("Scan %@", v.name))54 }55 ProportionBar(fraction: 1 - v.freeFraction, color: v.freeFraction < 0.1 ? ORIX.Palette.review : ORIX.Palette.accent, height: 8)56 if v.purgeable > 500_000_000 {57 Text(L("macOS reports %@ more that it can free on demand (purgeable: caches, local snapshots).", ByteFormat.string(v.purgeable)))58 .font(.caption).foregroundStyle(ORIX.Palette.textSecondary)59 }60 }61 .accessibilityElement(children: .contain)62 if v.id != model.volumes.last?.id { Divider() }63 }64 }65 }66 }6768 private var scanCard: some View {69 Card {70 VStack(alignment: .leading, spacing: ORIX.Space.m) {71 SectionTitle(title: L("Analyze storage"), subtitle: L("A scan reads names and sizes only. It never opens, changes or uploads your files."))72 HStack(spacing: ORIX.Space.m) {73 scanButton(L("Home Folder"), "house", model.home, id: "overview.scanHome")74 scanButton(L("Downloads"), "arrow.down.circle", model.home + "/Downloads", id: "overview.scanDownloads")75 scanButton(L("Documents"), "doc", model.home + "/Documents", id: "overview.scanDocuments")76 Button { model.chooseFolderAndScan() } label: { Label(L("Choose…"), systemImage: "folder") }.controlSize(.large)77 }78 if let scan = model.scan {79 let _ = model.treeVersion80 Divider()81 HStack(alignment: .top, spacing: ORIX.Space.l) {82 StatTile(title: L("Size on disk"), value: ByteFormat.string(scan.tree[0].allocatedSize), detail: scan.tree.rootPath.abbreviatingHome)83 StatTile(title: L("Content size"), value: ByteFormat.string(scan.tree[0].logicalSize), detail: L("Sum of file lengths"))84 StatTile(title: L("Files"), value: ByteFormat.count(Int(scan.tree[0].fileCount)), detail: L("%@ folders", ByteFormat.count(scan.directories)))85 StatTile(title: L("Scan duration"), value: String(format: "%.1f s", scan.duration), detail: L("%@ items per second", ByteFormat.count(Int(scan.filesPerSecond))))86 }87 if !scan.issues.isEmpty {88 let denied = scan.issues.filter { $0.kind == .permissionDenied }.count89 InfoBanner(symbol: "eye.slash", text: L("%d folders could not be read (%d for lack of permission). Their content is not in these totals.", scan.issues.count, denied))90 }91 }92 }93 }94 }9596 private func scanButton(_ title: String, _ symbol: String, _ path: String, id: String) -> some View {97 Button { model.startScan(path: path); model.section = .map } label: { Label(title, systemImage: symbol) }98 .controlSize(.large).disabled(model.scanPhase == .running).accessibilityIdentifier(id)99 }100101 private func breakdownCard(_ scan: ScanResult) -> some View {102 let _ = model.treeVersion103 let cats = TreeQuery.categories(in: scan.tree)104 let total = max(1, cats.reduce(0) { $0 + $1.allocated })105 return Card {106 VStack(alignment: .leading, spacing: ORIX.Space.m) {107 SectionTitle(title: L("What takes the space"), subtitle: L("Measured size on disk by file type in the scanned location."))108 Chart(cats) { c in109 BarMark(x: .value(L("Size"), c.allocated))110 .foregroundStyle(palette.swiftUIColor(c.category))111 }112 .chartXAxis(.hidden).chartLegend(.hidden).frame(height: 22)113 .clipShape(RoundedRectangle(cornerRadius: 5))114 .accessibilityHidden(true)115 LazyVGrid(columns: [GridItem(.adaptive(minimum: 220), spacing: ORIX.Space.m)], alignment: .leading, spacing: ORIX.Space.s) {116 ForEach(cats) { c in117 HStack(spacing: 6) {118 Image(systemName: c.category.symbol).foregroundStyle(palette.swiftUIColor(c.category)).frame(width: 18).accessibilityHidden(true)119 Text(c.category.title)120 Spacer()121 Text(ByteFormat.string(c.allocated)).font(.callout.monospacedDigit())122 Text(String(format: "%.0f%%", Double(c.allocated) / Double(total) * 100)).font(.caption.monospacedDigit())123 .foregroundStyle(ORIX.Palette.textSecondary).frame(width: 36, alignment: .trailing)124 }125 .accessibilityElement(children: .combine)126 }127 }128 }129 }130 }131132 private var recommendationsCard: some View {133 Card {134 VStack(alignment: .leading, spacing: ORIX.Space.m) {135 SectionTitle(title: L("Recommendations"), subtitle: L("Each one states what was measured. None is produced without data."))136 if model.recommendations.isEmpty {137 Text(L("Nothing stands out in what has been analyzed so far.")).font(.callout).foregroundStyle(ORIX.Palette.textSecondary)138 }139 ForEach(model.recommendations) { r in140 HStack(alignment: .top, spacing: ORIX.Space.m) {141 VStack(alignment: .leading, spacing: 3) {142 HStack { Text(r.title).font(.headline); RiskBadge(risk: r.risk) }143 Text(r.evidence).font(.callout)144 Text(r.consequence).font(.caption).foregroundStyle(ORIX.Palette.textSecondary)145 }146 Spacer()147 Button(r.suggestedAction) { model.section = AppSection(rawValue: r.destination.rawValue) ?? .overview }148 }149 .accessibilityElement(children: .contain)150 if r.id != model.recommendations.last?.id { Divider() }151 }152 }153 }154 }155}156