SPB Git

Repository Public MIT

GlassSearchFieldExample.swift 4.3 KB 103 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/Forms/GlassSearchFieldExample.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 GlassSearchFieldExample: GlassExample {45    public static let componentID = "glass-search-field"4647    private static let catalogue = ["Café crème", "Cafetière italienne", "Thé vert", "Théière en fonte", "Chocolat chaud", "Citronnade", "Eau pétillante", "Jus d’orange"]48    private static let recent = [49        GlassSearchSuggestion(title: "café", systemImage: "clock"),50        GlassSearchSuggestion(title: "thé", systemImage: "clock"),51        GlassSearchSuggestion(title: "Hot drinks", systemImage: "cup.and.saucer", completion: "chaud"),52    ]5354    @State private var query = ""55    @State private var inlineQuery = ""56    @State private var results = GlassSearchFieldExample.catalogue57    @State private var debouncer = GlassSearchDebouncer(delay: .milliseconds(250))5859    public init() {}6061    /// A stand-in for a remote search: it only filters a local list after a short pause.62    private static func search(_ query: String) async throws -> [String] {63        try await Task.sleep(for: .milliseconds(150))64        let needle = query.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: nil)65        return needle.isEmpty ? catalogue : catalogue.filter { $0.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: nil).contains(needle) }66    }6768    public var body: some View {69        NavigationStack {70            List {71                Section("Inline field") {72                    GlassSearchField(text: $inlineQuery, prompt: "Filter this section")73                        .listRowSeparator(.hidden)74                    ForEach(Self.catalogue.filter { inlineQuery.isEmpty || $0.localizedStandardContains(inlineQuery) }, id: \.self) { item in75                        Text(verbatim: item)76                    }77                }78                Section {79                    ForEach(results, id: \.self) { item in80                        Text(verbatim: item)81                    }82                    if results.isEmpty {83                        Text("No results").foregroundStyle(.secondary)84                    }85                } header: {86                    HStack {87                        Text("Debounced results")88                        if debouncer.isPending { ProgressView().controlSize(.small) }89                    }90                }91            }92            .navigationTitle("Drinks")93            .glassSearchable(text: $query, prompt: "Search drinks", suggestions: query.isEmpty ? Self.recent : [])94            .onChange(of: query) { _, newValue in95                debouncer.submit(newValue, search: Self.search) { results = $0 }96            }97            .onDisappear { debouncer.cancel() }98        }99    }100}101102#Preview("GlassSearchField") { GlassSearchFieldExample() }103
Files main
Menu

Theme