SPB Git

Repository Public MIT

example-glass-table.txt 2.0 KB 51 lines text

1import SwiftUI2import SPGlassKit34public struct GlassTableExample: GlassExample {5    public static let componentID = "glass-table"67    struct Station: Identifiable {8        let id: String9        let name: String10        let line: String11        let dailyRiders: Int12        let opened: Int13    }1415    // Fictitious data.16    private let stations = [17        Station(id: "s1", name: "Harbour Square", line: "Blue", dailyRiders: 48_200, opened: 1987),18        Station(id: "s2", name: "Élan–Université", line: "Green", dailyRiders: 31_750, opened: 2004),19        Station(id: "s3", name: "North Mills", line: "Blue", dailyRiders: 9_640, opened: 1991),20        Station(id: "s4", name: "Parc des Érables", line: "Orange", dailyRiders: 22_310, opened: 2016),21        Station(id: "s5", name: "Old Foundry", line: "Green", dailyRiders: 9_640, opened: 1979),22    ]2324    private let columns: [GlassTableColumn<Station>] = [25        GlassTableColumn("Station", id: "name") { $0.name },26        GlassTableColumn("Line", id: "line") { $0.line },27        GlassTableColumn("Daily riders", id: "riders", sortingBy: { $0.dailyRiders }) { $0.dailyRiders.formatted(.number) },28        GlassTableColumn("Opened", id: "opened", sortingBy: { $0.opened }) { $0.opened.formatted(.number.grouping(.never)) },29    ]3031    @State private var sort: GlassTableSort? = GlassTableSort(columnID: "riders", order: .reverse)32    @State private var selection: Set<String> = ["s2"]33    @State private var showsEmpty = false3435    public init() {}3637    public var body: some View {38        VStack(alignment: .leading, spacing: 12) {39            Toggle("Show the empty state", isOn: $showsEmpty)40            Text("\(selection.count) selected")41                .font(.callout)42                .foregroundStyle(.secondary)43            GlassTable(showsEmpty ? [] : stations, columns: columns, sort: $sort, selection: .multiple($selection), emptyTitle: "No station")44                .frame(minHeight: 320)45        }46        .padding()47    }48}4950#Preview("GlassTable") { GlassTableExample() }51
Files main
Menu

Theme