Repository Public License
1// ORIX Scan2// File: App/Sources/Actions/WorkflowEditorView.swift3// Description: Visual workflow builder: input source, ordered steps with editable parameters, reordering, validation.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import ORIXDesign8import ORIXKit9import SwiftData10import SwiftUI1112struct WorkflowEditorView: View {13 @Environment(AppModel.self) private var model14 @Environment(\.orixAccent) private var accent15 @Query private var stored: [WorkflowRecord]16 let workflowID: UUID17 @State private var workflow: Workflow?18 @State private var editMode: EditMode = .inactive1920 private static let symbols = ["wand.and.stars", "doc.viewfinder", "text.viewfinder", "receipt", "book", "tablecells", "tray.full", "envelope", "signature", "building.columns", "graduationcap", "heart.text.square"]2122 init(workflowID: UUID) {23 self.workflowID = workflowID24 _stored = Query(filter: #Predicate<WorkflowRecord> { $0.id == workflowID })25 }2627 var body: some View {28 Group {29 if let binding = Binding($workflow) {30 editor(binding)31 } else {32 ORIXEmptyState(systemName: "wand.and.stars", title: "Action Unavailable", message: "This action no longer exists.")33 }34 }35 .navigationTitle(workflow?.name ?? "")36 .navigationBarTitleDisplayMode(.inline)37 .task(id: stored.first?.payload) { if workflow == nil { workflow = stored.first?.workflow } }38 .onChange(of: workflow) { _, new in39 guard let new, let record = stored.first, record.workflow != new else { return }40 record.workflow = new41 try? model.index.context.save()42 }43 }4445 private func editor(_ workflow: Binding<Workflow>) -> some View {46 List {47 Section("Action") {48 TextField("Name", text: workflow.name).font(.orixHeadline).accessibilityIdentifier("workflow.name")49 Picker("Starts With", selection: workflow.input) {50 ForEach(WorkflowInput.allCases) { input in Label { Text(input.title) } icon: { Image(systemName: input.symbol) }.tag(input) }51 }52 ScrollView(.horizontal, showsIndicators: false) {53 HStack(spacing: ORIX.Space.xs) {54 ForEach(Self.symbols, id: \.self) { symbol in55 Button { workflow.wrappedValue.symbol = symbol } label: {56 ORIXGlyph(symbol, tint: workflow.wrappedValue.symbol == symbol ? accent.primary : ORIX.Palette.textTertiary, size: 40)57 }58 .buttonStyle(.plain)59 .accessibilityLabel("Icon \(symbol)")60 .accessibilityAddTraits(workflow.wrappedValue.symbol == symbol ? .isSelected : [])61 }62 }63 }64 }65 Section {66 ForEach(workflow.steps) { $step in67 StepEditor(step: $step, index: (workflow.wrappedValue.steps.firstIndex { $0.id == step.id } ?? 0) + 1,68 issues: workflow.wrappedValue.validationIssues)69 }70 .onMove { workflow.wrappedValue.steps.move(fromOffsets: $0, toOffset: $1); ORIXHaptics.tick() }71 .onDelete { workflow.wrappedValue.steps.remove(atOffsets: $0) }72 Menu {73 ForEach(ActionKind.allCases) { kind in74 Button { workflow.wrappedValue.steps.append(ActionStep(kind)); ORIXHaptics.tick() } label: { Label { Text(kind.title) } icon: { Image(systemName: kind.symbol) } }75 }76 } label: { Label("Add Step", systemImage: "plus.circle.fill").font(.orixHeadline) }77 .accessibilityIdentifier("workflow.addStep")78 } header: {79 Text("Steps")80 } footer: {81 if workflow.wrappedValue.steps.isEmpty {82 Text("Add at least one step.")83 } else {84 Text("Steps run from top to bottom. Export steps recognize the text first when they need it.")85 }86 }87 Section {88 Button { WorkflowLauncher.start(workflow.wrappedValue, model: model) } label: { Label("Run Action", systemImage: "play.fill") }89 .buttonStyle(.orixPrimary)90 .listRowInsets(EdgeInsets())91 .listRowBackground(Color.clear)92 .disabled(!workflow.wrappedValue.validationIssues.isEmpty)93 .accessibilityIdentifier("workflow.run")94 }95 }96 .environment(\.editMode, $editMode)97 .toolbar { ToolbarItem(placement: .topBarTrailing) { EditButton() } }98 }99}100101/// One step as a card: what it does and the parameters it accepts.102private struct StepEditor: View {103 @Environment(\.orixAccent) private var accent104 @Binding var step: ActionStep105 let index: Int106 let issues: [Workflow.ValidationIssue]107108 var body: some View {109 VStack(alignment: .leading, spacing: ORIX.Space.xs) {110 HStack(spacing: ORIX.Space.sm) {111 Text(index, format: .number).font(.orixCaption).monospacedDigit().foregroundStyle(.white)112 .frame(width: 24, height: 24).background(accent.primary, in: Circle())113 .accessibilityLabel("Step \(index)")114 ORIXGlyph(step.kind.symbol, size: 32)115 VStack(alignment: .leading, spacing: 1) {116 Text(step.kind.title).font(.orixHeadline)117 Text(step.kind.summary).font(.orixCaption).foregroundStyle(ORIX.Palette.textSecondary)118 }119 }120 parameters121 }122 .padding(.vertical, 4)123 }124125 @ViewBuilder private var parameters: some View {126 switch step.kind {127 case .enhance:128 Picker("Filter", selection: $step.filter) { ForEach(FilterMode.allCases) { Text($0.title).tag($0) } }129 case .recognizeText:130 Picker("Mode", selection: $step.recognition) { ForEach(RecognitionMode.allCases) { Text($0.title).tag($0) } }131 case .exportPDF:132 Picker("Quality", selection: $step.preset) { ForEach(ExportPreset.allCases) { Text($0.title).tag($0) } }133 Toggle("Searchable Text Layer", isOn: $step.searchable)134 case .rename:135 TextField("Pattern", text: $step.pattern).textInputAutocapitalization(.never).autocorrectionDisabled()136 Text("Tokens: {title} {date} {time} {pages}").font(.orixMono).foregroundStyle(ORIX.Palette.textSecondary)137 if issues.contains(.emptyPattern(step.id)) { warning("Enter a name pattern.") }138 case .moveToFolder:139 TextField("Folder name", text: $step.folderName)140 if issues.contains(.missingFolder(step.id)) { warning("Enter a folder name. The folder is created if needed.") }141 case .autoCrop, .deskew, .markFavorite, .exportText, .exportTables:142 EmptyView()143 }144 }145146 private func warning(_ text: LocalizedStringKey) -> some View {147 Label(text, systemImage: "exclamationmark.triangle.fill").font(.orixCaption).foregroundStyle(ORIX.Palette.warning)148 }149}150