SPB Git

Repository Public MIT

example-glass-agent-panel.txt 3.3 KB 72 lines text

1import SwiftUI2import SPGlassKit34public struct GlassAgentPanelExample: GlassExample {5    public static let componentID = "glass-agent-panel"67    /// Fictitious tasks: nothing is executed, the example only edits these values.8    @State private var tasks = [9        GlassAgentTask(id: "t1", title: "Prepare the sample weekly digest", status: .running, steps: [10            GlassAgentStep(id: "s1", title: "Collect sample notes", status: .succeeded, detail: "12 notes found"),11            GlassAgentStep(id: "s2", title: "Group by topic", status: .running, detail: "Topic 3 of 5"),12            GlassAgentStep(id: "s3", title: "Write the digest", status: .pending),13        ]),14        GlassAgentTask(id: "t2", title: "Rename sample files with a deliberately long task title that wraps over two lines", status: .failed, steps: [15            GlassAgentStep(id: "s1", title: "List files", status: .succeeded),16            GlassAgentStep(id: "s2", title: "Apply new names", status: .failed, detail: "4 of 9 renamed", errorMessage: "Sample failure: a file with this name already exists."),17        ]),18        GlassAgentTask(id: "t3", title: "Translate sample page", status: .succeeded, steps: [19            GlassAgentStep(id: "s1", title: "Detect language", status: .succeeded),20            GlassAgentStep(id: "s2", title: "Translate", status: .succeeded),21        ]),22        GlassAgentTask(id: "t4", title: "Queued sample export", status: .pending),23        GlassAgentTask(id: "t5", title: "Sample clean-up", status: .cancelled, steps: [24            GlassAgentStep(id: "s1", title: "Scan", status: .cancelled),25        ], errorMessage: nil),26    ]2728    public init() {}2930    public var body: some View {31        ScrollView {32            VStack(alignment: .leading, spacing: 12) {33                Text("Sample data · simulated tasks, no agent is running")34                    .font(.caption)35                    .foregroundStyle(.secondary)36                GlassAgentPanel(tasks: tasks) { task in37                    cancel(task.id)38                }39                GlassButton("Advance the running task", systemImage: "forward.frame") { advance() }40                Divider()41                Text("No task")42                    .font(.headline)43                GlassAgentPanel(tasks: [])44            }45            .padding()46        }47    }4849    private func cancel(_ id: String) {50        guard let index = tasks.firstIndex(where: { $0.id == id }) else { return }51        tasks[index].status = .cancelled52        for step in tasks[index].steps.indices where !tasks[index].steps[step].status.isTerminal {53            tasks[index].steps[step].status = .cancelled54        }55    }5657    /// Completes the running step of the first running task and starts the next one.58    private func advance() {59        guard let index = tasks.firstIndex(where: { $0.status == .running }) else { return }60        if let running = tasks[index].steps.firstIndex(where: { $0.status == .running }) {61            tasks[index].steps[running].status = .succeeded62        }63        if let next = tasks[index].steps.firstIndex(where: { $0.status == .pending }) {64            tasks[index].steps[next].status = .running65        } else {66            tasks[index].status = .succeeded67        }68    }69}7071#Preview("GlassAgentPanel") { GlassAgentPanelExample().glassTheme(.obsidian) }72
Files main
Menu

Theme