SPB Git

Repository Public License

ActionsView.swift 6.6 KB 122 lines swift

1// ORIX Clean — App/Sources/Features/Actions/ActionsView.swift2// Description: ORIX Clean Actions: run, create and save cleanup workflows. Removal always passes through the preview.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ORIXCleanCore5import SwiftUI67struct ActionsView: View {8    @Environment(AppModel.self) private var model9    @State private var editing: Workflow?1011    var body: some View {12        ScrollView {13            VStack(alignment: .leading, spacing: ORIX.Space.l) {14                HStack(alignment: .top) {15                    SectionTitle(title: L("ORIX Clean Actions"), subtitle: L("Reusable sequences of real operations. A step that removes files can only follow a review step, and the review always shows you the exact list."))16                    Spacer()17                    Button(L("New Action…")) { editing = Workflow(name: L("My action"), steps: [.collectCaches, .review, .moveToTrash, .report]) }18                        .accessibilityIdentifier("actions.new")19                }20                LazyVGrid(columns: [GridItem(.adaptive(minimum: 330), spacing: ORIX.Space.l)], alignment: .leading, spacing: ORIX.Space.l) {21                    ForEach(model.workflows) { w in card(w) }22                }23                if !model.workflowEvents.isEmpty {24                    Card {25                        VStack(alignment: .leading, spacing: 6) {26                            SectionTitle(title: L("Last run"), subtitle: nil)27                            ForEach(model.workflowEvents) { e in28                                HStack(alignment: .firstTextBaseline) {29                                    Text("\(e.step + 1)").font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textTertiary).frame(width: 18, alignment: .trailing)30                                    Text(e.message).font(.callout)31                                    Spacer()32                                    Text(e.date.formatted(date: .omitted, time: .standard)).font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textTertiary)33                                }34                            }35                        }36                    }37                    .accessibilityIdentifier("actions.log")38                }39            }40            .padding(ORIX.Space.xl)41            .frame(maxWidth: 1100).frame(maxWidth: .infinity)42        }43        .sheet(item: $editing) { w in WorkflowEditor(workflow: w) { saved in if let saved { model.save(saved) }; editing = nil } }44    }4546    private func card(_ w: Workflow) -> some View {47        Card {48            VStack(alignment: .leading, spacing: ORIX.Space.s) {49                HStack {50                    Image(systemName: "bolt.fill").foregroundStyle(ORIX.Palette.accent).accessibilityHidden(true)51                    Text(w.name).font(.headline)52                    Spacer()53                    if !w.builtIn {54                        Menu { Button(L("Edit…")) { editing = w }; Button(L("Delete"), role: .destructive) { model.delete(w) } } label: { Image(systemName: "ellipsis.circle") }55                            .menuStyle(.borderlessButton).fixedSize().accessibilityLabel(L("More"))56                    }57                }58                ForEach(Array(w.steps.enumerated()), id: \.offset) { i, step in59                    HStack(alignment: .firstTextBaseline, spacing: 6) {60                        Text("\(i + 1).").font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textTertiary)61                        Text(step.title).font(.callout).foregroundStyle(step == .review ? ORIX.Palette.accentSecondary : .primary)62                    }63                }64                HStack {65                    Spacer()66                    if model.runningWorkflow == w.id { ProgressView().controlSize(.small) }67                    Button(L("Run")) { model.run(w) }.buttonStyle(.borderedProminent).disabled(model.runningWorkflow != nil)68                        .accessibilityIdentifier("actions.run." + w.id)69                }70            }71        }72    }73}7475struct WorkflowEditor: View {76    @State var workflow: Workflow77    var done: (Workflow?) -> Void78    @State private var error: String?7980    static let palette: [(String, WorkflowStep)] = [81        ("Scan Downloads", .scanFolder(path: NSHomeDirectory() + "/Downloads")), ("Scan Home Folder", .scanFolder(path: NSHomeDirectory())),82        ("Find duplicates (1 MB+)", .findDuplicates(minimumSizeMB: 1)), ("Analyze caches and logs", .collectCaches),83        ("Analyze developer data", .collectDeveloperData), ("Collect downloads older than 90 days", .collectDownloads(olderThanDays: 90)),84        ("Review and confirm", .review), ("Move to Trash", .moveToTrash), ("Create report", .report),85    ]8687    var body: some View {88        VStack(alignment: .leading, spacing: ORIX.Space.m) {89            Text(L("Edit action")).font(.title2.weight(.semibold))90            TextField(L("Name"), text: $workflow.name).textFieldStyle(.roundedBorder)91            List {92                ForEach(Array(workflow.steps.enumerated()), id: \.offset) { i, step in93                    HStack {94                        Text("\(i + 1).").foregroundStyle(ORIX.Palette.textTertiary)95                        Text(step.title)96                        Spacer()97                        Button { workflow.steps.remove(at: i) } label: { Image(systemName: "minus.circle") }.buttonStyle(.borderless).accessibilityLabel(L("Remove step"))98                    }99                }100                .onMove { workflow.steps.move(fromOffsets: $0, toOffset: $1) }101            }102            .frame(height: 220)103            Menu(L("Add Step")) { ForEach(Self.palette, id: \.0) { p in Button(L(p.0)) { workflow.steps.append(p.1) } } }.fixedSize()104            if let error { Label(error, systemImage: "exclamationmark.circle").font(.callout).foregroundStyle(ORIX.Palette.high) }105            HStack {106                Spacer()107                Button(L("Cancel")) { done(nil) }.keyboardShortcut(.cancelAction)108                Button(L("Save")) {109                    do { try workflow.validate(); done(workflow) } catch WorkflowError.removalWithoutReview {110                        error = L("“Move to Trash” must come right after “Review and confirm”. An action can never remove files you have not seen.")111                    } catch WorkflowError.duplicatesWithoutScan {112                        error = L("“Find duplicates” needs a scan step before it.")113                    } catch { self.error = L("The action has no step.") }114                }115                .buttonStyle(.borderedProminent).keyboardShortcut(.defaultAction).disabled(workflow.name.trimmingCharacters(in: .whitespaces).isEmpty)116            }117        }118        .padding(ORIX.Space.xl)119        .frame(width: 520)120    }121}122
Files main
Menu

Theme