Repository Public MIT
1import SwiftUI2import SPGlassKit34public struct GlassTabBarExample: GlassExample {5 public static let componentID = "glass-tab-bar"67 enum DemoTab: String, Hashable, Sendable {8 case today, library, inbox, search9 }1011 enum DemoRoute: Hashable, Sendable {12 case album(String)13 case track(String)14 }1516 @State private var state = GlassTabNavigationState<DemoTab, DemoRoute>(selection: .today)17 @State private var unread = 318 @State private var showsInbox = true1920 /// Deep links of the demo: `glasskit-demo://library/<album>/<track>`.21 static let router = GlassTabRouter<DemoTab, DemoRoute>(22 schemes: ["glasskit-demo"],23 tabs: ["today": .today, "library": .library, "inbox": .inbox, "search": .search]24 ) { tab, components, _ in25 switch (tab, components.count) {26 case (_, 0): []27 case (.library, 1): [.album(components[0])]28 case (.library, 2): [.album(components[0]), .track(components[1])]29 default: nil30 }31 }3233 public init() {}3435 private var tabs: [GlassTabItem<DemoTab>] {36 var tabs = [37 GlassTabItem(id: DemoTab.today, title: "Today", systemImage: "sun.max"),38 GlassTabItem(id: DemoTab.library, title: "Library", systemImage: "square.stack"),39 ]40 if showsInbox {41 tabs.append(GlassTabItem(id: .inbox, title: "Inbox", systemImage: "tray", badge: .count(unread)))42 }43 tabs.append(GlassTabItem(id: .search, title: "Search", systemImage: "magnifyingglass", isSearch: true))44 return tabs45 }4647 public var body: some View {48 GlassTabBar(tabs: tabs, state: $state, minimizeBehavior: .onScrollDown) { tab in49 root(tab)50 } destination: { route in51 switch route {52 case .album(let name):53 List(1...12, id: \.self) { number in54 NavigationLink("Track \(number)", value: DemoRoute.track("\(name) · \(number)"))55 }56 .navigationTitle(name)57 case .track(let name):58 Text(verbatim: name).font(.title2).navigationTitle("Now playing")59 }60 }61 .onOpenURL { url in62 if let destination = Self.router.destination(for: url) { state.open(destination) }63 }64 .onChange(of: showsInbox) {65 state.reconcile(with: tabs.map(\.id), fallback: .today)66 }67 }6869 @ViewBuilder70 private func root(_ tab: DemoTab) -> some View {71 switch tab {72 case .today:73 List {74 Section("Deep links") {75 Button("Open “Aurora” in Library") {76 state.open(GlassTabDestination(tab: .library, path: [.album("Aurora")]))77 }78 Button("Open track 4 of “Tides”") {79 if let url = URL(string: "glasskit-demo://library/Tides/4"),80 let destination = Self.router.destination(for: url) {81 state.open(destination)82 }83 }84 }85 Section("Dynamic tabs") {86 Toggle("Show the Inbox tab", isOn: $showsInbox)87 Stepper("Unread: \(unread)", value: $unread, in: 0...99)88 }89 }90 .navigationTitle("Today")91 case .library:92 List(["Aurora", "Tides", "Quiet Hours", "Granite"], id: \.self) { album in93 NavigationLink(album, value: DemoRoute.album(album))94 }95 .navigationTitle("Library")96 case .inbox:97 List(0..<unread, id: \.self) { index in98 Text("Sample message \(index + 1)")99 }100 .overlay {101 if unread == 0 { ContentUnavailableView("No messages", systemImage: "tray") }102 }103 .navigationTitle("Inbox")104 case .search:105 Text("Search results appear here.")106 .foregroundStyle(.secondary)107 .navigationTitle("Search")108 }109 }110}111112#Preview("GlassTabBar") { GlassTabBarExample() }113