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

189 lines
6.2 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 {
2021-09-14 02:11:16 +05:30
enum SidebarGroup: String, Identifiable {
case main
var id: RawValue {
rawValue
}
}
2021-09-25 13:48:22 +05:30
@EnvironmentObject<InvidiousAPI> private var api
@EnvironmentObject<InstancesModel> private var instances
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlaylistsModel> private var playlists
2021-09-25 17:47:58 +05:30
@EnvironmentObject<RecentsModel> private var recents
2021-09-25 13:48:22 +05:30
@EnvironmentObject<SubscriptionsModel> 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?> {
2021-09-25 13:48:22 +05:30
navigation.tabSelectionOptionalBinding
2021-08-30 03:06:18 +05:30
}
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
2021-09-25 13:48:22 +05:30
// We work around that by showing the primary view
2021-07-12 02:22:49 +05:30
if !didApplyPrimaryViewWorkAround, let splitVC = viewController.children.first as? UISplitViewController {
UIView.performWithoutAnimation {
splitVC.show(.primary)
}
didApplyPrimaryViewWorkAround = true
}
}
#else
content
#endif
}
2021-09-25 13:48:22 +05:30
let sidebarMinWidth: Double = 280
2021-07-12 02:22:49 +05:30
var content: some View {
NavigationView {
sidebar
2021-09-25 13:48:22 +05:30
.toolbar { toolbarContent }
.frame(minWidth: sidebarMinWidth)
2021-07-12 02:22:49 +05:30
Text("Select section")
}
2021-09-19 18:12:47 +05:30
.environment(\.navigationStyle, .sidebar)
2021-07-12 02:22:49 +05:30
}
var sidebar: some View {
ScrollViewReader { scrollView in
List {
2021-09-14 02:11:16 +05:30
ForEach(sidebarGroups) { group in
sidebarGroupContent(group)
.id(group)
}
2021-09-14 02:11:16 +05:30
2021-09-25 13:48:22 +05:30
.onChange(of: navigation.sidebarSectionChanged) { _ in
scrollScrollViewToItem(scrollView: scrollView, for: navigation.tabSelection)
}
}
.listStyle(.sidebar)
2021-08-30 03:06:18 +05:30
}
2021-09-14 02:11:16 +05:30
.toolbar {
2021-09-25 13:48:22 +05:30
toolbarContent
2021-09-14 02:11:16 +05:30
}
}
var sidebarGroups: [SidebarGroup] {
[.main]
}
func sidebarGroupContent(_ group: SidebarGroup) -> some View {
switch group {
case .main:
return Group {
mainNavigationLinks
2021-09-19 16:36:54 +05:30
AppSidebarRecents(selection: selection)
2021-09-14 02:11:16 +05:30
.id("recentlyOpened")
2021-09-25 13:48:22 +05:30
if api.signedIn {
AppSidebarSubscriptions(selection: selection)
AppSidebarPlaylists(selection: selection)
}
2021-08-30 03:06:18 +05:30
}
2021-09-14 02:11:16 +05:30
}
2021-08-30 03:06:18 +05:30
}
var mainNavigationLinks: some View {
2021-09-14 02:11:16 +05:30
Section("Videos") {
2021-09-25 13:48:22 +05:30
NavigationLink(destination: LazyView(WatchNowView()), tag: TabSelection.watchNow, selection: selection) {
2021-09-19 02:06:42 +05:30
Label("Watch Now", systemImage: "play.circle")
.accessibility(label: Text("Watch Now"))
}
2021-09-25 13:48:22 +05:30
if api.signedIn {
NavigationLink(destination: LazyView(SubscriptionsView()), tag: TabSelection.subscriptions, selection: selection) {
Label("Subscriptions", systemImage: "star.circle")
.accessibility(label: Text("Subscriptions"))
}
2021-07-12 02:22:49 +05:30
}
2021-09-14 02:11:16 +05:30
NavigationLink(destination: LazyView(PopularView()), tag: TabSelection.popular, selection: selection) {
2021-07-12 02:22:49 +05:30
Label("Popular", systemImage: "chart.bar")
2021-07-28 04:10:04 +05:30
.accessibility(label: Text("Popular"))
}
2021-09-14 02:11:16 +05:30
NavigationLink(destination: LazyView(TrendingView()), tag: TabSelection.trending, selection: selection) {
2021-07-28 04:10:04 +05:30
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
.accessibility(label: Text("Trending"))
}
2021-09-25 17:47:58 +05:30
NavigationLink(destination: LazyView(SearchView()), tag: TabSelection.search, selection: selection) {
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
}
.keyboardShortcut("f")
2021-07-12 02:22:49 +05:30
}
2021-08-30 03:06:18 +05:30
}
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)
}
}
2021-09-25 13:48:22 +05:30
var toolbarContent: some ToolbarContent {
Group {
#if os(iOS)
ToolbarItemGroup(placement: .navigationBarTrailing) {
Button(action: { navigation.presentingSettings = true }) {
Image(systemName: "gearshape.2")
}
}
#endif
ToolbarItem(placement: accountsMenuToolbarItemPlacement) {
AccountsMenuView()
.help(
"Switch Instances and Accounts\n" +
"Current Instance: \n" +
"\(api.account?.url ?? "Not Set")\n" +
"Current User: \(api.account?.description ?? "Not set")"
)
}
}
}
var accountsMenuToolbarItemPlacement: ToolbarItemPlacement {
#if os(iOS)
return .bottomBar
#else
return .automatic
#endif
}
#if os(macOS)
private func toggleSidebar() {
NSApp.keyWindow?.contentViewController?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
}
#endif
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"
2021-09-25 13:48:22 +05:30
return "\(symbolName).circle"
2021-07-12 02:22:49 +05:30
}
}