SPB Git

Repository Public MIT

GlassTabBarExample.swift 5.7 KB 153 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Examples/Gallery/Navigation/GlassTabBarExample.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 GlassTabBarExample: GlassExample {45    public static let componentID = "glass-tab-bar"4647    enum DemoTab: String, Hashable, Sendable {48        case today, library, inbox, search49    }5051    enum DemoRoute: Hashable, Sendable {52        case album(String)53        case track(String)54    }5556    @State private var state = GlassTabNavigationState<DemoTab, DemoRoute>(selection: .today)57    @State private var unread = 358    @State private var showsInbox = true5960    /// Deep links of the demo: `glasskit-demo://library/<album>/<track>`.61    static let router = GlassTabRouter<DemoTab, DemoRoute>(62        schemes: ["glasskit-demo"],63        tabs: ["today": .today, "library": .library, "inbox": .inbox, "search": .search]64    ) { tab, components, _ in65        switch (tab, components.count) {66        case (_, 0): []67        case (.library, 1): [.album(components[0])]68        case (.library, 2): [.album(components[0]), .track(components[1])]69        default: nil70        }71    }7273    public init() {}7475    private var tabs: [GlassTabItem<DemoTab>] {76        var tabs = [77            GlassTabItem(id: DemoTab.today, title: "Today", systemImage: "sun.max"),78            GlassTabItem(id: DemoTab.library, title: "Library", systemImage: "square.stack"),79        ]80        if showsInbox {81            tabs.append(GlassTabItem(id: .inbox, title: "Inbox", systemImage: "tray", badge: .count(unread)))82        }83        tabs.append(GlassTabItem(id: .search, title: "Search", systemImage: "magnifyingglass", isSearch: true))84        return tabs85    }8687    public var body: some View {88        GlassTabBar(tabs: tabs, state: $state, minimizeBehavior: .onScrollDown) { tab in89            root(tab)90        } destination: { route in91            switch route {92            case .album(let name):93                List(1...12, id: \.self) { number in94                    NavigationLink("Track \(number)", value: DemoRoute.track("\(name) · \(number)"))95                }96                .navigationTitle(name)97            case .track(let name):98                Text(verbatim: name).font(.title2).navigationTitle("Now playing")99            }100        }101        .onOpenURL { url in102            if let destination = Self.router.destination(for: url) { state.open(destination) }103        }104        .onChange(of: showsInbox) {105            state.reconcile(with: tabs.map(\.id), fallback: .today)106        }107    }108109    @ViewBuilder110    private func root(_ tab: DemoTab) -> some View {111        switch tab {112        case .today:113            List {114                Section("Deep links") {115                    Button("Open “Aurora” in Library") {116                        state.open(GlassTabDestination(tab: .library, path: [.album("Aurora")]))117                    }118                    Button("Open track 4 of “Tides”") {119                        if let url = URL(string: "glasskit-demo://library/Tides/4"),120                           let destination = Self.router.destination(for: url) {121                            state.open(destination)122                        }123                    }124                }125                Section("Dynamic tabs") {126                    Toggle("Show the Inbox tab", isOn: $showsInbox)127                    Stepper("Unread: \(unread)", value: $unread, in: 0...99)128                }129            }130            .navigationTitle("Today")131        case .library:132            List(["Aurora", "Tides", "Quiet Hours", "Granite"], id: \.self) { album in133                NavigationLink(album, value: DemoRoute.album(album))134            }135            .navigationTitle("Library")136        case .inbox:137            List(0..<unread, id: \.self) { index in138                Text("Sample message \(index + 1)")139            }140            .overlay {141                if unread == 0 { ContentUnavailableView("No messages", systemImage: "tray") }142            }143            .navigationTitle("Inbox")144        case .search:145            Text("Search results appear here.")146                .foregroundStyle(.secondary)147                .navigationTitle("Search")148        }149    }150}151152#Preview("GlassTabBar") { GlassTabBarExample() }153
Files main
Menu

Theme