1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 06:10:32 +05:30
yattee/Shared/Navigation/AppSidebarNavigation.swift

121 lines
3.9 KiB
Swift
Raw Normal View History

2021-07-12 02:22:49 +05:30
import SwiftUI
#if os(iOS)
import Introspect
#endif
struct AppSidebarNavigation: View {
@EnvironmentObject<NavigationState> private var navigationState
2021-08-30 03:06:18 +05:30
@EnvironmentObject<Playlists> private var playlists
@EnvironmentObject<Subscriptions> private var subscriptions
2021-07-12 02:22:49 +05:30
@State private var didApplyPrimaryViewWorkAround = false
2021-08-30 03:06:18 +05:30
var selection: Binding<TabSelection?> {
navigationState.tabSelectionOptionalBinding
}
2021-07-12 02:22:49 +05:30
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-12 02:22:49 +05:30
Text("Select section")
}
}
var sidebar: some View {
List {
2021-08-30 03:06:18 +05:30
mainNavigationLinks
AppSidebarSubscriptions(selection: selection)
AppSidebarPlaylists(selection: selection)
}
#if os(macOS)
.toolbar {
Button(action: toggleSidebar) {
Image(systemName: "sidebar.left").help("Toggle Sidebar")
}
}
#endif
}
var mainNavigationLinks: some View {
Group {
NavigationLink(tag: TabSelection.subscriptions, selection: selection) {
2021-07-12 02:22:49 +05:30
SubscriptionsView()
}
label: {
2021-08-26 03:42:59 +05:30
Label("Subscriptions", systemImage: "star.circle.fill")
2021-07-28 04:10:04 +05:30
.accessibility(label: Text("Subscriptions"))
2021-07-12 02:22:49 +05:30
}
2021-08-30 03:06:18 +05:30
NavigationLink(tag: TabSelection.popular, selection: selection) {
2021-07-28 04:10:04 +05:30
PopularView()
2021-07-12 02:22:49 +05:30
}
label: {
Label("Popular", systemImage: "chart.bar")
2021-07-28 04:10:04 +05:30
.accessibility(label: Text("Popular"))
}
2021-08-30 03:06:18 +05:30
NavigationLink(tag: TabSelection.trending, selection: selection) {
2021-07-28 04:10:04 +05:30
TrendingView()
}
label: {
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
.accessibility(label: Text("Trending"))
}
2021-08-30 03:06:18 +05:30
NavigationLink(tag: TabSelection.playlists, selection: selection) {
2021-07-28 04:10:04 +05:30
PlaylistsView()
}
label: {
Label("Playlists", systemImage: "list.and.film")
.accessibility(label: Text("Playlists"))
}
2021-08-30 03:06:18 +05:30
NavigationLink(tag: TabSelection.search, selection: selection) {
2021-07-28 04:10:04 +05:30
SearchView()
}
label: {
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
2021-07-12 02:22:49 +05:30
}
}
2021-08-30 03:06:18 +05:30
}
static func symbolSystemImage(_ name: String) -> String {
let firstLetter = name.first?.lowercased()
let regex = #"^[a-z0-9]$"#
let symbolName = firstLetter?.range(of: regex, options: .regularExpression) != nil ? firstLetter! : "questionmark"
return "\(symbolName).square"
2021-07-12 02:22:49 +05:30
}
2021-07-28 04:10:04 +05:30
#if os(macOS)
private func toggleSidebar() {
NSApp.keyWindow?.contentViewController?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
}
#endif
2021-07-12 02:22:49 +05:30
}