yattee/Shared/Navigation/Sidebar.swift

134 lines
5.2 KiB
Swift
Raw Normal View History

import Defaults
2021-09-28 23:01:49 +00:00
import SwiftUI
struct Sidebar: View {
@ObservedObject private var accounts = AccountsModel.shared
@ObservedObject private var navigation = NavigationModel.shared
2022-12-12 23:39:50 +00:00
@ObservedObject private var feed = FeedModel.shared
2021-09-28 23:01:49 +00:00
2022-11-11 20:28:40 +00:00
@Default(.showHome) private var showHome
@Default(.visibleSections) private var visibleSections
#if os(iOS)
@Default(.showDocuments) private var showDocuments
#endif
2021-09-28 23:01:49 +00:00
var body: some View {
ScrollViewReader { scrollView in
List {
2022-11-10 22:55:39 +00:00
mainNavigationLinks
2021-09-28 23:01:49 +00:00
2022-11-10 22:55:39 +00:00
if !accounts.isEmpty {
2021-10-17 23:06:00 +00:00
AppSidebarRecents()
.id("recentlyOpened")
2021-09-28 23:01:49 +00:00
if accounts.api.signedIn {
if visibleSections.contains(.subscriptions), accounts.app.supportsSubscriptions {
AppSidebarSubscriptions()
}
if visibleSections.contains(.playlists), accounts.app.supportsUserPlaylists {
AppSidebarPlaylists()
}
2021-10-17 23:06:00 +00:00
}
2021-09-28 23:01:49 +00:00
}
}
.onChange(of: navigation.sidebarSectionChanged) { _ in
scrollScrollViewToItem(scrollView: scrollView, for: navigation.tabSelection)
}
.listStyle(.sidebar)
}
2022-12-12 23:39:50 +00:00
.onAppear {
feed.calculateUnwatchedFeed()
}
.onChange(of: accounts.current) { _ in
feed.calculateUnwatchedFeed()
}
2021-11-07 13:32:01 +00:00
.navigationTitle("Yattee")
#if os(iOS)
2021-11-07 16:52:42 +00:00
.navigationBarTitleDisplayMode(.inline)
2021-11-07 13:32:01 +00:00
#endif
2021-09-28 23:01:49 +00:00
}
var mainNavigationLinks: some View {
2021-11-28 14:37:55 +00:00
Section(header: Text("Videos")) {
2022-11-11 20:28:40 +00:00
if showHome {
2022-12-10 21:37:14 +00:00
NavigationLink(destination: LazyView(HomeView().modifier(PlayerOverlayModifier())), tag: TabSelection.home, selection: $navigation.tabSelection) {
2022-11-09 13:34:04 +00:00
Label("Home", systemImage: "house")
.accessibility(label: Text("Home"))
}
2022-11-12 23:07:23 +00:00
.id("home")
2021-09-28 23:01:49 +00:00
}
2022-11-12 23:01:04 +00:00
#if os(iOS)
if showDocuments {
2022-12-10 21:37:14 +00:00
NavigationLink(destination: LazyView(DocumentsView().modifier(PlayerOverlayModifier())), tag: TabSelection.documents, selection: $navigation.tabSelection) {
2022-11-12 23:01:04 +00:00
Label("Documents", systemImage: "folder")
.accessibility(label: Text("Documents"))
}
.id("documents")
}
#endif
2022-11-11 20:28:40 +00:00
if !accounts.isEmpty {
if visibleSections.contains(.subscriptions),
accounts.app.supportsSubscriptions && accounts.signedIn
{
2022-12-10 21:37:14 +00:00
NavigationLink(destination: LazyView(SubscriptionsView().modifier(PlayerOverlayModifier())), tag: TabSelection.subscriptions, selection: $navigation.tabSelection) {
2022-11-11 20:28:40 +00:00
Label("Subscriptions", systemImage: "star.circle")
.accessibility(label: Text("Subscriptions"))
}
2022-12-12 23:39:50 +00:00
.backport
.badge(subscriptionsBadge)
2022-11-11 20:28:40 +00:00
.id("subscriptions")
2021-09-28 23:01:49 +00:00
}
2022-11-11 20:28:40 +00:00
if visibleSections.contains(.popular), accounts.app.supportsPopular {
2022-12-10 21:37:14 +00:00
NavigationLink(destination: LazyView(PopularView().modifier(PlayerOverlayModifier())), tag: TabSelection.popular, selection: $navigation.tabSelection) {
2022-11-11 20:28:40 +00:00
Label("Popular", systemImage: "arrow.up.right.circle")
.accessibility(label: Text("Popular"))
}
.id("popular")
2021-10-20 22:21:50 +00:00
}
2021-09-28 23:01:49 +00:00
2022-11-11 20:28:40 +00:00
if visibleSections.contains(.trending) {
2022-12-10 21:37:14 +00:00
NavigationLink(destination: LazyView(TrendingView().modifier(PlayerOverlayModifier())), tag: TabSelection.trending, selection: $navigation.tabSelection) {
2022-11-11 20:28:40 +00:00
Label("Trending", systemImage: "chart.bar")
.accessibility(label: Text("Trending"))
}
.id("trending")
}
2021-09-28 23:01:49 +00:00
2022-12-10 21:37:14 +00:00
NavigationLink(destination: LazyView(SearchView().modifier(PlayerOverlayModifier())), tag: TabSelection.search, selection: $navigation.tabSelection) {
2022-11-11 19:34:20 +00:00
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
}
.id("search")
.keyboardShortcut("f")
2021-09-28 23:01:49 +00:00
}
}
}
2022-12-12 23:39:50 +00:00
private var subscriptionsBadge: Text? {
guard let account = accounts.current,
let unwatched = feed.unwatched[account],
unwatched > 0
else {
return nil
}
return Text("\(String(unwatched))")
}
private func scrollScrollViewToItem(scrollView: ScrollViewProxy, for selection: TabSelection) {
2021-09-28 23:01:49 +00:00
if case .recentlyOpened = selection {
scrollView.scrollTo("recentlyOpened")
2022-03-27 18:38:59 +00:00
return
2021-09-28 23:01:49 +00:00
} else if case let .playlist(id) = selection {
scrollView.scrollTo(id)
2022-03-27 18:38:59 +00:00
return
2021-09-28 23:01:49 +00:00
}
2022-03-27 18:38:59 +00:00
scrollView.scrollTo(selection.stringValue)
2021-09-28 23:01:49 +00:00
}
}