SPB Git

Repository Public MIT

DataDescriptorSupport.swift 5.8 KB 152 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Apps/GlassStudio/Kit/Descriptors/DataDescriptorSupport.swift8// Module           : GlassStudio9// 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 SPGlassKit4344// Stateful preview hosts of the data descriptors (see DataDescriptors.swift).4546/// A metric card whose retry button, shown after a failure, really reloads.47struct MetricCardPreview: View {48    let title: String49    @State private var state: GlassLoadState<GlassMetric>50    private let hasRetry: Bool5152    init(title: String, state: GlassLoadState<GlassMetric>) {53        self.title = title54        _state = State(initialValue: state)55        if case .failed = state { hasRetry = true } else { hasRetry = false }56    }5758    private var onRetry: (@MainActor () -> Void)? {59        guard hasRetry else { return nil }60        return { state = .loading }61    }6263    var body: some View {64        GlassMetricCard(LocalizedStringKey(title), state: state, onRetry: onRetry)65    }66}6768/// A table with real sort and selection state. Rows are fictitious.69struct TablePreview: View {70    let rows: [GlassChartPoint]71    let selectionMode: String72    let emptyTitle: String73    @State private var sort: GlassTableSort?74    @State private var single: String?75    @State private var multiple: Set<String>7677    init(rows: [GlassChartPoint], sort: GlassTableSort?, selectionMode: String, selected: String?, emptyTitle: String) {78        self.rows = rows79        self.selectionMode = selectionMode80        self.emptyTitle = emptyTitle81        _sort = State(initialValue: sort)82        _single = State(initialValue: selected)83        _multiple = State(initialValue: selected.map { [$0] } ?? [])84    }8586    private var columns: [GlassTableColumn<GlassChartPoint>] {87        [88            GlassTableColumn(LocalizedStringResource(String.LocalizationValue(L("Station"))), id: "name") { $0.x.label() },89            GlassTableColumn(LocalizedStringResource(String.LocalizationValue(L("Line"))), id: "line") { $0.series ?? "" },90            GlassTableColumn(LocalizedStringResource(String.LocalizationValue(L("Daily riders"))), id: "riders", sortingBy: { $0.value }) { $0.value.formatted(.number) },91        ]92    }9394    private var selection: GlassTableSelection<String> {95        switch selectionMode {96        case "single": .single($single)97        case "multiple": .multiple($multiple)98        default: .none99        }100    }101102    var body: some View {103        VStack(alignment: .leading, spacing: 8) {104            GlassTable(rows, columns: columns, sort: $sort, selection: selection, emptyTitle: emptyTitle.isEmpty ? nil : LocalizedStringKey(emptyTitle))105                .frame(minHeight: 320)106            Text(verbatim: L("Fictitious sample data")).font(.caption).foregroundStyle(.secondary)107        }108    }109}110111/// A grid of generated sample cells with real selection state.112struct DataGridPreview: View {113    let count: Int114    let layout: GlassGridLayout115    let spacing: GlassSpacingStep116    let selectable: Bool117    @State private var selection: Set<String> = []118119    private var samples: [GlassChartPoint] {120        (0..<count).map { GlassChartPoint(label: "\(L("Sample")) \($0 + 1)", value: Double($0 * 37 % 100)) }121    }122123    var body: some View {124        Group {125            if selectable {126                GlassDataGrid(samples, layout: layout, spacing: spacing, selection: $selection, emptyTitle: LocalizedStringKey(L("No sample"))) { sample, isSelected in127                    cell(sample, isSelected: isSelected)128                }129            } else {130                GlassDataGrid(samples, layout: layout, spacing: spacing, emptyTitle: LocalizedStringKey(L("No sample"))) { sample in131                    cell(sample, isSelected: nil)132                }133            }134        }135        .frame(minHeight: 320)136    }137138    private func cell(_ sample: GlassChartPoint, isSelected: Bool?) -> some View {139        VStack(alignment: .leading, spacing: 4) {140            Text(verbatim: sample.x.label()).font(.headline)141            Text(verbatim: "\(Int(sample.value)) %").font(.callout.monospacedDigit()).foregroundStyle(.secondary)142            if let isSelected {143                Image(systemName: isSelected ? "checkmark.circle.fill" : "circle")144                    .accessibilityHidden(true)145            }146        }147        .frame(maxWidth: .infinity, alignment: .leading)148        .padding(12)149        .background(.quaternary, in: RoundedRectangle(cornerRadius: 12, style: .continuous))150    }151}152
Files main
Menu

Theme