yattee/Shared/Navigation/AppSidebarNavigation.swift

103 lines
3.5 KiB
Swift
Raw Normal View History

2021-07-11 20:52:49 +00:00
import SwiftUI
#if os(iOS)
import Introspect
#endif
typealias TabSelection = AppSidebarNavigation.TabSelection
2021-07-11 20:52:49 +00:00
struct AppSidebarNavigation: View {
enum TabSelection: String {
case subscriptions, popular, trending, playlists, channel, search
}
2021-07-11 20:52:49 +00:00
@EnvironmentObject<NavigationState> private var navigationState
@State private var didApplyPrimaryViewWorkAround = false
var body: some View {
#if os(iOS)
content.introspectViewController { viewController in
// workaround for an empty supplementary view on launch
// the supplementary view is determined by the default selection inside the
// primary view, but the primary view is not loaded so its selection is not read
// We work around that by briefly showing the primary view.
if !didApplyPrimaryViewWorkAround, let splitVC = viewController.children.first as? UISplitViewController {
UIView.performWithoutAnimation {
splitVC.show(.primary)
splitVC.hide(.primary)
}
didApplyPrimaryViewWorkAround = true
}
}
#else
content
#endif
}
var content: some View {
NavigationView {
sidebar
.frame(minWidth: 180)
2021-07-11 20:52:49 +00:00
Text("Select section")
}
}
var sidebar: some View {
List {
NavigationLink(tag: TabSelection.subscriptions, selection: navigationState.tabSelectionOptionalBinding) {
SubscriptionsView()
}
label: {
2021-08-25 22:12:59 +00:00
Label("Subscriptions", systemImage: "star.circle.fill")
2021-07-27 22:40:04 +00:00
.accessibility(label: Text("Subscriptions"))
2021-07-11 20:52:49 +00:00
}
NavigationLink(tag: TabSelection.popular, selection: navigationState.tabSelectionOptionalBinding) {
2021-07-27 22:40:04 +00:00
PopularView()
2021-07-11 20:52:49 +00:00
}
label: {
Label("Popular", systemImage: "chart.bar")
2021-07-27 22:40:04 +00:00
.accessibility(label: Text("Popular"))
}
NavigationLink(tag: TabSelection.trending, selection: navigationState.tabSelectionOptionalBinding) {
TrendingView()
}
label: {
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
.accessibility(label: Text("Trending"))
}
NavigationLink(tag: TabSelection.playlists, selection: navigationState.tabSelectionOptionalBinding) {
PlaylistsView()
}
label: {
Label("Playlists", systemImage: "list.and.film")
.accessibility(label: Text("Playlists"))
}
NavigationLink(tag: TabSelection.search, selection: navigationState.tabSelectionOptionalBinding) {
SearchView()
}
label: {
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
2021-07-11 20:52:49 +00:00
}
}
2021-07-27 22:40:04 +00:00
#if os(macOS)
.toolbar {
Button(action: toggleSidebar) {
Image(systemName: "sidebar.left").help("Toggle Sidebar")
}
}
#endif
2021-07-11 20:52:49 +00:00
}
2021-07-27 22:40:04 +00:00
#if os(macOS)
private func toggleSidebar() {
NSApp.keyWindow?.contentViewController?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
}
#endif
2021-07-11 20:52:49 +00:00
}