Repository Public License
1// ORIX Clean — App/Sources/Features/History/HistoryView.swift2// Description: Storage history: free space over time, watched folders, cleanup log. Only recorded samples are shown.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Charts5import ORIXCleanCore6import SwiftUI78struct HistoryView: View {9 @Environment(AppModel.self) private var model10 @State private var range = 011 @State private var openReport: DeletionReport?1213 var body: some View {14 ScrollView {15 VStack(alignment: .leading, spacing: ORIX.Space.l) {16 volumeCard17 watchedCard18 operationsCard19 }20 .padding(ORIX.Space.xl)21 .frame(maxWidth: 1100).frame(maxWidth: .infinity)22 }23 .sheet(item: $openReport) { r in24 CleanupResultsSheet(reportID: r.id).environment(model)25 .overlay(alignment: .topTrailing) { Button(L("Close")) { openReport = nil }.padding() }26 }27 .onAppear { model.refreshEnvironment() }28 }2930 private var samples: [VolumeSample] {31 guard range > 0 else { return model.samples }32 let cutoff = Date().addingTimeInterval(-Double(range) * 86400)33 return model.samples.filter { $0.date >= cutoff }34 }3536 private var volumeCard: some View {37 Card {38 VStack(alignment: .leading, spacing: ORIX.Space.m) {39 HStack(alignment: .top) {40 SectionTitle(title: L("Used space on %@", model.startupVolume?.name ?? ""),41 subtitle: model.monitoringStart.map { L("Recorded by ORIX Clean since %@, while the application is open. Nothing exists before that date.", ByteFormat.dateTime($0)) }42 ?? L("No sample recorded yet."))43 Spacer()44 Picker(L("Range"), selection: $range) {45 Text(L("All")).tag(0); Text(L("24 hours")).tag(1); Text(L("7 days")).tag(7); Text(L("30 days")).tag(30)46 }.pickerStyle(.segmented).labelsHidden().frame(width: 300)47 }48 let s = samples49 if s.count < 2 {50 Text(L("A trend needs at least two samples. One is recorded every 15 minutes while ORIX Clean is open, and after each scan and cleanup."))51 .font(.callout).foregroundStyle(ORIX.Palette.textSecondary).frame(height: 120)52 } else {53 let low = Double(s.map(\.used).min() ?? 0), high = Double(s.map(\.used).max() ?? 1)54 let pad = max((high - low) * 0.15, 50_000_000)55 Chart(s) { p in56 AreaMark(x: .value(L("Date"), p.date), yStart: .value("", low - pad), yEnd: .value(L("Used"), Double(p.used)))57 .foregroundStyle(LinearGradient(colors: [ORIX.Palette.accent.opacity(0.35), ORIX.Palette.accent.opacity(0.02)], startPoint: .top, endPoint: .bottom))58 LineMark(x: .value(L("Date"), p.date), y: .value(L("Used"), Double(p.used)))59 .foregroundStyle(ORIX.Palette.accent).lineStyle(StrokeStyle(lineWidth: 2))60 if s.count <= 60 { PointMark(x: .value(L("Date"), p.date), y: .value(L("Used"), Double(p.used))).foregroundStyle(ORIX.Palette.accent).symbolSize(18) }61 }62 .chartYScale(domain: (low - pad) ... (high + pad))63 .chartYAxis { AxisMarks { v in AxisGridLine(); AxisValueLabel { if let d = v.as(Double.self) { Text(ByteFormat.string(Int64(d))) } } } }64 .frame(height: 240)65 .accessibilityLabel(L("Used space over time"))66 .accessibilityValue(L("%d samples from %@ to %@", s.count, ByteFormat.string(Int64(low)), ByteFormat.string(Int64(high))))67 HStack(alignment: .top, spacing: ORIX.Space.l) {68 StatTile(title: L("Samples"), value: ByteFormat.count(s.count), detail: nil)69 StatTile(title: L("Change over the period"), value: signed((s.last?.used ?? 0) - (s.first?.used ?? 0)), detail: nil)70 if let slope = Trend.bytesPerDay(s) {71 StatTile(title: L("Trend per day"), value: signed(Int64(slope)), detail: L("Least-squares fit of the samples"))72 } else {73 StatTile(title: L("Trend per day"), value: "—", detail: L("Needs 3 samples over at least 6 hours"))74 }75 StatTile(title: L("Free now"), value: ByteFormat.string(model.startupVolume?.available ?? 0), detail: nil)76 }77 }78 }79 }80 }8182 private func signed(_ v: Int64) -> String { (v < 0 ? "−" : "+") + ByteFormat.string(abs(v)) }8384 private var watchedCard: some View {85 Card {86 VStack(alignment: .leading, spacing: ORIX.Space.m) {87 HStack(alignment: .top) {88 SectionTitle(title: L("Watched folders"), subtitle: L("Folders measured on demand so you can compare their size over time."))89 Spacer()90 Button(L("Measure Now")) { model.measureWatched() }.disabled(model.watched.isEmpty)91 Button(L("Add Folder…")) {92 let panel = NSOpenPanel()93 panel.canChooseDirectories = true; panel.canChooseFiles = false94 if panel.runModal() == .OK, let u = panel.url { model.watch(u.path) }95 }96 }97 if model.watched.isEmpty { Text(L("No folder is watched.")).font(.callout).foregroundStyle(ORIX.Palette.textSecondary) }98 ForEach(model.watched, id: \.self) { path in99 let list = model.folderSamples[path] ?? []100 HStack(spacing: ORIX.Space.m) {101 Image(systemName: "folder").foregroundStyle(ORIX.Palette.accentSecondary).accessibilityHidden(true)102 VStack(alignment: .leading, spacing: 1) {103 Text(path.abbreviatingHome).lineLimit(1).truncationMode(.middle)104 Text(L("%d measurements", list.count) + (list.first.map { " · " + L("since %@", ByteFormat.date($0.date)) } ?? ""))105 .font(.caption).foregroundStyle(ORIX.Palette.textSecondary)106 }107 Spacer()108 if list.count > 1 {109 Chart(list) { LineMark(x: .value("d", $0.date), y: .value("s", Double($0.allocated))).foregroundStyle(ORIX.Palette.accent) }110 .chartXAxis(.hidden).chartYAxis(.hidden).frame(width: 120, height: 26).accessibilityHidden(true)111 Text(signed((list.last?.allocated ?? 0) - (list.first?.allocated ?? 0))).font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary)112 }113 if let last = list.last { SizeText(bytes: last.allocated) }114 Button { model.unwatch(path) } label: { Image(systemName: "minus.circle") }.buttonStyle(.borderless).accessibilityLabel(L("Stop watching %@", path))115 }116 .accessibilityElement(children: .contain)117 }118 }119 }120 }121122 private var operationsCard: some View {123 Card {124 VStack(alignment: .leading, spacing: ORIX.Space.m) {125 SectionTitle(title: L("Cleanup history"), subtitle: L("Every cleanup run from ORIX Clean on this Mac, with its per-item outcome."))126 if model.operations.isEmpty { Text(L("No cleanup has been run yet.")).font(.callout).foregroundStyle(ORIX.Palette.textSecondary) }127 ForEach(model.operations.prefix(50)) { op in128 HStack(spacing: ORIX.Space.m) {129 Image(systemName: op.mode == .permanent ? "xmark.bin" : "trash").foregroundStyle(ORIX.Palette.textSecondary).frame(width: 20).accessibilityHidden(true)130 VStack(alignment: .leading, spacing: 1) {131 Text(ByteFormat.dateTime(op.started))132 Text(summary(op)).font(.caption).foregroundStyle(ORIX.Palette.textSecondary)133 }134 Spacer()135 SizeText(bytes: op.allocated(.movedToTrash) + op.allocated(.permanentlyDeleted))136 Button(L("Details")) { openReport = op }.controlSize(.small)137 }138 .accessibilityElement(children: .combine)139 }140 }141 }142 }143144 private func summary(_ op: DeletionReport) -> String {145 DeletionStatus.allCases.filter { op.count($0) > 0 }.map { "\(op.count($0)) \($0.title.lowercased())" }.joined(separator: " · ")146 }147}148