SPB Git

Repository Public MIT

GlassDataGridExample.swift 3.6 KB 96 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/Data/GlassDataGridExample.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 GlassDataGridExample: GlassExample {45    public static let componentID = "glass-data-grid"4647    struct Sample: Identifiable {48        let id: Int49        var reading: Double { Double((id * 37) % 100) / 100 }50    }5152    /// Thirty thousand fictitious samples, produced on demand: no array of that size is ever built.53    struct Samples: RandomAccessCollection {54        let count: Int55        var startIndex: Int { 0 }56        var endIndex: Int { count }57        subscript(position: Int) -> Sample { Sample(id: position) }58    }5960    @State private var selection: Set<Int> = [2]61    @State private var fixedColumns = false6263    public init() {}6465    public var body: some View {66        VStack(alignment: .leading, spacing: 12) {67            Toggle("Three fixed columns", isOn: $fixedColumns)68            Text("\(selection.count) of \(Samples(count: 30_000).count) samples selected")69                .font(.callout)70                .foregroundStyle(.secondary)71            GlassDataGrid(72                Samples(count: 30_000),73                layout: fixedColumns ? .fixed(3) : .adaptive(minimum: 110),74                selection: $selection75            ) { sample, isSelected in76                VStack(alignment: .leading, spacing: 4) {77                    Text("Sample \(sample.id + 1)")78                        .font(.headline)79                    Text(sample.reading, format: .percent.precision(.fractionLength(0)))80                        .font(.callout.monospacedDigit())81                        .foregroundStyle(.secondary)82                    Text(isSelected ? "Selected" : "Tap to select")83                        .font(.caption)84                        .foregroundStyle(.secondary)85                }86                .frame(maxWidth: .infinity, alignment: .leading)87                .padding(12)88                .background(.quaternary, in: RoundedRectangle(cornerRadius: 12, style: .continuous))89            }90        }91        .padding()92    }93}9495#Preview("GlassDataGrid") { GlassDataGridExample() }96
Files main
Menu

Theme