SPB Git

Repository Public MIT

GlassPromptBarExample.swift 3.9 KB 96 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/AI/GlassPromptBarExample.swift8// Module           : Examples9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassKit4344public struct GlassPromptBarExample: GlassExample {45    public static let componentID = "glass-prompt-bar"46    @State private var draft = ""47    @State private var isResponding = false48    @State private var sent: [String] = []49    @State private var attachments = [50        GlassPromptAttachment(id: "a1", name: "sample-report.pdf", systemImage: "doc.richtext"),51        GlassPromptAttachment(id: "a2", name: "a-sample-image-with-a-rather-long-file-name.png", systemImage: "photo"),52    ]53    @State private var nextAttachment = 35455    public init() {}5657    public var body: some View {58        VStack(alignment: .leading, spacing: 12) {59            Text("Sample data · nothing is sent anywhere; prompts are only listed below.")60                .font(.caption)61                .foregroundStyle(.secondary)62            ScrollView {63                VStack(alignment: .leading, spacing: 8) {64                    ForEach(Array(sent.enumerated()), id: \.offset) { _, prompt in65                        Text(prompt)66                            .frame(maxWidth: .infinity, alignment: .leading)67                    }68                    if isResponding {69                        GlassThinkingIndicator(.writing, label: "Simulating a response")70                    }71                }72            }73            GlassPromptBar(text: $draft, placeholder: "Ask the sample assistant", isResponding: isResponding, attachments: attachments) { prompt in74                sent.append(prompt)75                isResponding = true76            } onStop: {77                isResponding = false78            } onAttach: {79                // A real host presents its own file importer or photo picker here.80                attachments.append(GlassPromptAttachment(id: "a\(nextAttachment)", name: "sample-\(nextAttachment).txt"))81                nextAttachment += 182            } onRemoveAttachment: { attachment in83                attachments.removeAll { $0.id == attachment.id }84            }85        }86        .padding()87        .task(id: isResponding) {88            guard isResponding else { return }89            try? await Task.sleep(for: .seconds(3))90            if !Task.isCancelled { isResponding = false }91        }92    }93}9495#Preview("GlassPromptBar") { GlassPromptBarExample().glassTheme(.aurora) }96
Files main
Menu

Theme