yattee/Shared/Navigation/AppSidebarNavigation.swift

183 lines
6.2 KiB
Swift
Raw Normal View History

2021-07-11 20:52:49 +00:00
import SwiftUI
#if os(iOS)
import Introspect
#endif
struct AppSidebarNavigation: View {
2021-09-13 20:41:16 +00:00
enum SidebarGroup: String, Identifiable {
case main
var id: RawValue {
rawValue
}
}
2021-07-11 20:52:49 +00:00
@EnvironmentObject<NavigationState> private var navigationState
2021-08-29 21:36:18 +00:00
@EnvironmentObject<Playlists> private var playlists
2021-09-19 11:06:54 +00:00
@EnvironmentObject<Recents> private var recents
2021-09-13 20:41:16 +00:00
@EnvironmentObject<SearchState> private var searchState
2021-08-29 21:36:18 +00:00
@EnvironmentObject<Subscriptions> private var subscriptions
2021-07-11 20:52:49 +00:00
@State private var didApplyPrimaryViewWorkAround = false
2021-08-29 21:36:18 +00:00
var selection: Binding<TabSelection?> {
navigationState.tabSelectionOptionalBinding
}
2021-07-11 20:52:49 +00:00
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")
}
2021-09-19 12:42:47 +00:00
.environment(\.navigationStyle, .sidebar)
.searchable(text: $searchState.queryText, placement: .sidebar) {
2021-09-13 20:41:16 +00:00
ForEach(searchState.querySuggestions.collection, id: \.self) { suggestion in
Text(suggestion)
.searchCompletion(suggestion)
}
}
2021-09-19 12:42:47 +00:00
.onChange(of: searchState.queryText) { query in
2021-09-13 20:41:16 +00:00
searchState.loadQuerySuggestions(query)
}
.onSubmit(of: .search) {
searchState.changeQuery { query in
2021-09-19 12:42:47 +00:00
query.query = searchState.queryText
2021-09-13 20:41:16 +00:00
}
2021-09-19 12:42:47 +00:00
recents.open(RecentItem(from: searchState.queryText))
2021-09-19 11:06:54 +00:00
2021-09-13 20:41:16 +00:00
navigationState.tabSelection = .search
}
2021-07-11 20:52:49 +00:00
}
var sidebar: some View {
ScrollViewReader { scrollView in
List {
2021-09-13 20:41:16 +00:00
ForEach(sidebarGroups) { group in
sidebarGroupContent(group)
.id(group)
}
2021-09-13 20:41:16 +00:00
.onChange(of: navigationState.sidebarSectionChanged) { _ in
scrollScrollViewToItem(scrollView: scrollView, for: navigationState.tabSelection)
}
}
2021-09-13 20:41:16 +00:00
.background {
NavigationLink(destination: SearchView(), tag: TabSelection.search, selection: selection) {
Color.clear
}
.hidden()
}
.listStyle(.sidebar)
2021-08-29 21:36:18 +00:00
}
2021-09-13 20:41:16 +00:00
.toolbar {
#if os(macOS)
ToolbarItemGroup {
Button(action: toggleSidebar) {
Image(systemName: "sidebar.left").help("Toggle Sidebar")
}
2021-08-29 21:36:18 +00:00
}
2021-09-13 20:41:16 +00:00
#endif
}
}
var sidebarGroups: [SidebarGroup] {
[.main]
}
func sidebarGroupContent(_ group: SidebarGroup) -> some View {
switch group {
case .main:
return Group {
mainNavigationLinks
2021-09-19 11:06:54 +00:00
AppSidebarRecents(selection: selection)
2021-09-13 20:41:16 +00:00
.id("recentlyOpened")
AppSidebarSubscriptions(selection: selection)
AppSidebarPlaylists(selection: selection)
2021-08-29 21:36:18 +00:00
}
2021-09-13 20:41:16 +00:00
}
2021-08-29 21:36:18 +00:00
}
var mainNavigationLinks: some View {
2021-09-13 20:41:16 +00:00
Section("Videos") {
2021-09-18 20:36:42 +00:00
NavigationLink(tag: TabSelection.watchNow, selection: selection) {
WatchNowView()
}
label: {
Label("Watch Now", systemImage: "play.circle")
.accessibility(label: Text("Watch Now"))
}
2021-09-13 20:41:16 +00:00
NavigationLink(destination: LazyView(SubscriptionsView()), tag: TabSelection.subscriptions, selection: selection) {
2021-09-19 11:06:54 +00:00
Label("Subscriptions", systemImage: "star.circle")
2021-07-27 22:40:04 +00:00
.accessibility(label: Text("Subscriptions"))
2021-07-11 20:52:49 +00:00
}
2021-09-13 20:41:16 +00:00
NavigationLink(destination: LazyView(PopularView()), tag: TabSelection.popular, selection: selection) {
2021-07-11 20:52:49 +00:00
Label("Popular", systemImage: "chart.bar")
2021-07-27 22:40:04 +00:00
.accessibility(label: Text("Popular"))
}
2021-09-13 20:41:16 +00:00
NavigationLink(destination: LazyView(TrendingView()), tag: TabSelection.trending, selection: selection) {
2021-07-27 22:40:04 +00:00
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
.accessibility(label: Text("Trending"))
}
2021-09-13 20:41:16 +00:00
NavigationLink(destination: LazyView(PlaylistsView()), tag: TabSelection.playlists, selection: selection) {
2021-07-27 22:40:04 +00:00
Label("Playlists", systemImage: "list.and.film")
.accessibility(label: Text("Playlists"))
}
2021-07-11 20:52:49 +00:00
}
2021-08-29 21:36:18 +00:00
}
func scrollScrollViewToItem(scrollView: ScrollViewProxy, for selection: TabSelection) {
if case let .channel(id) = selection {
if subscriptions.isSubscribing(id) {
scrollView.scrollTo(id)
} else {
scrollView.scrollTo("recentlyOpened")
}
} else if case let .playlist(id) = selection {
scrollView.scrollTo(id)
}
}
#if os(macOS)
private func toggleSidebar() {
NSApp.keyWindow?.contentViewController?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
}
#endif
2021-08-29 21:36:18 +00:00
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-11 20:52:49 +00:00
}
}