mirror of
https://github.com/yattee/yattee.git
synced 2025-08-09 20:24:06 +00:00
Navigation model improvements
This commit is contained in:
@@ -4,27 +4,11 @@ import SwiftUI
|
||||
#endif
|
||||
|
||||
struct AppSidebarNavigation: View {
|
||||
enum SidebarGroup: String, Identifiable {
|
||||
case main
|
||||
|
||||
var id: RawValue {
|
||||
rawValue
|
||||
}
|
||||
}
|
||||
|
||||
@EnvironmentObject<InvidiousAPI> private var api
|
||||
@EnvironmentObject<InstancesModel> private var instances
|
||||
@EnvironmentObject<NavigationModel> private var navigation
|
||||
@EnvironmentObject<PlaylistsModel> private var playlists
|
||||
@EnvironmentObject<RecentsModel> private var recents
|
||||
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
||||
|
||||
@State private var didApplyPrimaryViewWorkAround = false
|
||||
|
||||
var selection: Binding<TabSelection?> {
|
||||
navigation.tabSelectionOptionalBinding
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
#if os(iOS)
|
||||
content.introspectViewController { viewController in
|
||||
@@ -48,7 +32,7 @@ struct AppSidebarNavigation: View {
|
||||
|
||||
var content: some View {
|
||||
NavigationView {
|
||||
sidebar
|
||||
Sidebar()
|
||||
.toolbar { toolbarContent }
|
||||
.frame(minWidth: sidebarMinWidth)
|
||||
|
||||
@@ -57,90 +41,6 @@ struct AppSidebarNavigation: View {
|
||||
.environment(\.navigationStyle, .sidebar)
|
||||
}
|
||||
|
||||
var sidebar: some View {
|
||||
ScrollViewReader { scrollView in
|
||||
List {
|
||||
ForEach(sidebarGroups) { group in
|
||||
sidebarGroupContent(group)
|
||||
.id(group)
|
||||
}
|
||||
|
||||
.onChange(of: navigation.sidebarSectionChanged) { _ in
|
||||
scrollScrollViewToItem(scrollView: scrollView, for: navigation.tabSelection)
|
||||
}
|
||||
}
|
||||
.listStyle(.sidebar)
|
||||
}
|
||||
.toolbar {
|
||||
toolbarContent
|
||||
}
|
||||
}
|
||||
|
||||
var sidebarGroups: [SidebarGroup] {
|
||||
[.main]
|
||||
}
|
||||
|
||||
func sidebarGroupContent(_ group: SidebarGroup) -> some View {
|
||||
switch group {
|
||||
case .main:
|
||||
return Group {
|
||||
mainNavigationLinks
|
||||
|
||||
AppSidebarRecents(selection: selection)
|
||||
.id("recentlyOpened")
|
||||
|
||||
if api.signedIn {
|
||||
AppSidebarSubscriptions(selection: selection)
|
||||
AppSidebarPlaylists(selection: selection)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var mainNavigationLinks: some View {
|
||||
Section("Videos") {
|
||||
NavigationLink(destination: LazyView(WatchNowView()), tag: TabSelection.watchNow, selection: selection) {
|
||||
Label("Watch Now", systemImage: "play.circle")
|
||||
.accessibility(label: Text("Watch Now"))
|
||||
}
|
||||
|
||||
if api.signedIn {
|
||||
NavigationLink(destination: LazyView(SubscriptionsView()), tag: TabSelection.subscriptions, selection: selection) {
|
||||
Label("Subscriptions", systemImage: "star.circle")
|
||||
.accessibility(label: Text("Subscriptions"))
|
||||
}
|
||||
}
|
||||
|
||||
NavigationLink(destination: LazyView(PopularView()), tag: TabSelection.popular, selection: selection) {
|
||||
Label("Popular", systemImage: "chart.bar")
|
||||
.accessibility(label: Text("Popular"))
|
||||
}
|
||||
|
||||
NavigationLink(destination: LazyView(TrendingView()), tag: TabSelection.trending, selection: selection) {
|
||||
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
|
||||
.accessibility(label: Text("Trending"))
|
||||
}
|
||||
|
||||
NavigationLink(destination: LazyView(SearchView()), tag: TabSelection.search, selection: selection) {
|
||||
Label("Search", systemImage: "magnifyingglass")
|
||||
.accessibility(label: Text("Search"))
|
||||
}
|
||||
.keyboardShortcut("f")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
var toolbarContent: some ToolbarContent {
|
||||
Group {
|
||||
#if os(iOS)
|
||||
@@ -171,12 +71,6 @@ struct AppSidebarNavigation: View {
|
||||
#endif
|
||||
}
|
||||
|
||||
#if os(macOS)
|
||||
private func toggleSidebar() {
|
||||
NSApp.keyWindow?.contentViewController?.tryToPerform(#selector(NSSplitViewController.toggleSidebar(_:)), with: nil)
|
||||
}
|
||||
#endif
|
||||
|
||||
static func symbolSystemImage(_ name: String) -> String {
|
||||
let firstLetter = name.first?.lowercased()
|
||||
let regex = #"^[a-z0-9]$"#
|
||||
|
@@ -4,12 +4,10 @@ struct AppSidebarPlaylists: View {
|
||||
@EnvironmentObject<NavigationModel> private var navigation
|
||||
@EnvironmentObject<PlaylistsModel> private var playlists
|
||||
|
||||
@Binding var selection: TabSelection?
|
||||
|
||||
var body: some View {
|
||||
Section(header: Text("Playlists")) {
|
||||
ForEach(playlists.playlists.sorted { $0.title.lowercased() < $1.title.lowercased() }) { playlist in
|
||||
NavigationLink(tag: TabSelection.playlist(playlist.id), selection: $selection) {
|
||||
NavigationLink(tag: TabSelection.playlist(playlist.id), selection: $navigation.tabSelection) {
|
||||
LazyView(PlaylistVideosView(playlist))
|
||||
} label: {
|
||||
Label(playlist.title, systemImage: AppSidebarNavigation.symbolSystemImage(playlist.title))
|
||||
|
@@ -2,8 +2,6 @@ import Defaults
|
||||
import SwiftUI
|
||||
|
||||
struct AppSidebarRecents: View {
|
||||
@Binding var selection: TabSelection?
|
||||
|
||||
@EnvironmentObject<NavigationModel> private var navigation
|
||||
@EnvironmentObject<RecentsModel> private var recents
|
||||
|
||||
@@ -17,11 +15,11 @@ struct AppSidebarRecents: View {
|
||||
Group {
|
||||
switch recent.type {
|
||||
case .channel:
|
||||
RecentNavigationLink(recent: recent, selection: $selection) {
|
||||
LazyView(ChannelVideosView(channel: Channel(id: recent.id, name: recent.title)))
|
||||
RecentNavigationLink(recent: recent) {
|
||||
LazyView(ChannelVideosView(channel: recent.channel!))
|
||||
}
|
||||
case .query:
|
||||
RecentNavigationLink(recent: recent, selection: $selection, systemImage: "magnifyingglass") {
|
||||
RecentNavigationLink(recent: recent, systemImage: "magnifyingglass") {
|
||||
LazyView(SearchView(recent.query!))
|
||||
}
|
||||
}
|
||||
@@ -44,28 +42,25 @@ struct AppSidebarRecents: View {
|
||||
}
|
||||
|
||||
struct RecentNavigationLink<DestinationContent: View>: View {
|
||||
@EnvironmentObject<NavigationModel> private var navigation
|
||||
@EnvironmentObject<RecentsModel> private var recents
|
||||
|
||||
var recent: RecentItem
|
||||
@Binding var selection: TabSelection?
|
||||
|
||||
var systemImage: String?
|
||||
let destination: DestinationContent
|
||||
|
||||
init(
|
||||
recent: RecentItem,
|
||||
selection: Binding<TabSelection?>,
|
||||
systemImage: String? = nil,
|
||||
@ViewBuilder destination: () -> DestinationContent
|
||||
) {
|
||||
self.recent = recent
|
||||
_selection = selection
|
||||
self.systemImage = systemImage
|
||||
self.destination = destination()
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
NavigationLink(tag: TabSelection.recentlyOpened(recent.tag), selection: $selection) {
|
||||
NavigationLink(tag: TabSelection.recentlyOpened(recent.tag), selection: $navigation.tabSelection) {
|
||||
destination
|
||||
} label: {
|
||||
HStack {
|
||||
|
@@ -5,12 +5,10 @@ struct AppSidebarSubscriptions: View {
|
||||
@EnvironmentObject<NavigationModel> private var navigation
|
||||
@EnvironmentObject<SubscriptionsModel> private var subscriptions
|
||||
|
||||
@Binding var selection: TabSelection?
|
||||
|
||||
var body: some View {
|
||||
Section(header: Text("Subscriptions")) {
|
||||
ForEach(subscriptions.all) { channel in
|
||||
NavigationLink(tag: TabSelection.channel(channel.id), selection: $selection) {
|
||||
NavigationLink(tag: TabSelection.channel(channel.id), selection: $navigation.tabSelection) {
|
||||
LazyView(ChannelVideosView(channel: channel))
|
||||
} label: {
|
||||
Label(channel.name, systemImage: AppSidebarNavigation.symbolSystemImage(channel.name))
|
||||
@@ -21,6 +19,7 @@ struct AppSidebarSubscriptions: View {
|
||||
}
|
||||
}
|
||||
.modifier(UnsubscribeAlertModifier())
|
||||
.id("channel\(channel.id)")
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
|
@@ -7,7 +7,7 @@ struct AppTabNavigation: View {
|
||||
@EnvironmentObject<RecentsModel> private var recents
|
||||
|
||||
var body: some View {
|
||||
TabView(selection: $navigation.tabSelection) {
|
||||
TabView(selection: navigation.tabSelectionBinding) {
|
||||
NavigationView {
|
||||
LazyView(WatchNowView())
|
||||
.toolbar { toolbarContent }
|
||||
|
66
Shared/Navigation/Sidebar.swift
Normal file
66
Shared/Navigation/Sidebar.swift
Normal file
@@ -0,0 +1,66 @@
|
||||
import SwiftUI
|
||||
|
||||
struct Sidebar: View {
|
||||
@EnvironmentObject<InvidiousAPI> private var api
|
||||
@EnvironmentObject<NavigationModel> private var navigation
|
||||
|
||||
var body: some View {
|
||||
ScrollViewReader { scrollView in
|
||||
List {
|
||||
mainNavigationLinks
|
||||
|
||||
AppSidebarRecents()
|
||||
.id("recentlyOpened")
|
||||
|
||||
if api.signedIn {
|
||||
AppSidebarSubscriptions()
|
||||
AppSidebarPlaylists()
|
||||
}
|
||||
}
|
||||
.onChange(of: navigation.sidebarSectionChanged) { _ in
|
||||
scrollScrollViewToItem(scrollView: scrollView, for: navigation.tabSelection)
|
||||
}
|
||||
.listStyle(.sidebar)
|
||||
}
|
||||
}
|
||||
|
||||
var mainNavigationLinks: some View {
|
||||
Section("Videos") {
|
||||
NavigationLink(destination: LazyView(WatchNowView()), tag: TabSelection.watchNow, selection: $navigation.tabSelection) {
|
||||
Label("Watch Now", systemImage: "play.circle")
|
||||
.accessibility(label: Text("Watch Now"))
|
||||
}
|
||||
|
||||
if api.signedIn {
|
||||
NavigationLink(destination: LazyView(SubscriptionsView()), tag: TabSelection.subscriptions, selection: $navigation.tabSelection) {
|
||||
Label("Subscriptions", systemImage: "star.circle")
|
||||
.accessibility(label: Text("Subscriptions"))
|
||||
}
|
||||
}
|
||||
|
||||
NavigationLink(destination: LazyView(PopularView()), tag: TabSelection.popular, selection: $navigation.tabSelection) {
|
||||
Label("Popular", systemImage: "chart.bar")
|
||||
.accessibility(label: Text("Popular"))
|
||||
}
|
||||
|
||||
NavigationLink(destination: LazyView(TrendingView()), tag: TabSelection.trending, selection: $navigation.tabSelection) {
|
||||
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
|
||||
.accessibility(label: Text("Trending"))
|
||||
}
|
||||
|
||||
NavigationLink(destination: LazyView(SearchView()), tag: TabSelection.search, selection: $navigation.tabSelection) {
|
||||
Label("Search", systemImage: "magnifyingglass")
|
||||
.accessibility(label: Text("Search"))
|
||||
}
|
||||
.keyboardShortcut("f")
|
||||
}
|
||||
}
|
||||
|
||||
func scrollScrollViewToItem(scrollView: ScrollViewProxy, for selection: TabSelection) {
|
||||
if case .recentlyOpened = selection {
|
||||
scrollView.scrollTo("recentlyOpened")
|
||||
} else if case let .playlist(id) = selection {
|
||||
scrollView.scrollTo(id)
|
||||
}
|
||||
}
|
||||
}
|
@@ -11,62 +11,70 @@ struct VideoContextMenuView: View {
|
||||
let video: Video
|
||||
|
||||
var body: some View {
|
||||
Section {
|
||||
openChannelButton
|
||||
openChannelButton
|
||||
|
||||
subscriptionButton
|
||||
subscriptionButton
|
||||
|
||||
if case let .playlist(id) = navigation.tabSelection {
|
||||
removeFromPlaylistButton(playlistID: id)
|
||||
}
|
||||
if navigation.tabSelection != .playlists {
|
||||
addToPlaylistButton
|
||||
} else if let playlist = playlists.currentPlaylist {
|
||||
removeFromPlaylistButton(playlistID: playlist.id)
|
||||
}
|
||||
|
||||
if navigation.tabSelection == .playlists {
|
||||
removeFromPlaylistButton(playlistID: playlists.currentPlaylist!.id)
|
||||
} else {
|
||||
addToPlaylistButton
|
||||
}
|
||||
if case let .playlist(id) = navigation.tabSelection {
|
||||
removeFromPlaylistButton(playlistID: id)
|
||||
}
|
||||
}
|
||||
|
||||
var openChannelButton: some View {
|
||||
Button("\(video.author) Channel") {
|
||||
Button {
|
||||
let recent = RecentItem(from: video.channel)
|
||||
recents.add(recent)
|
||||
navigation.tabSelection = .recentlyOpened(recent.tag)
|
||||
navigation.isChannelOpen = true
|
||||
navigation.sidebarSectionChanged.toggle()
|
||||
navigation.tabSelection = .recentlyOpened(recent.tag)
|
||||
} label: {
|
||||
Label("\(video.author) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
|
||||
}
|
||||
}
|
||||
|
||||
var subscriptionButton: some View {
|
||||
Group {
|
||||
if subscriptions.isSubscribing(video.channel.id) {
|
||||
Button("Unsubscribe", role: .destructive) {
|
||||
Button(role: .destructive) {
|
||||
#if os(tvOS)
|
||||
subscriptions.unsubscribe(video.channel.id)
|
||||
#else
|
||||
navigation.presentUnsubscribeAlert(video.channel)
|
||||
#endif
|
||||
} label: {
|
||||
Label("Unsubscribe", systemImage: "xmark.circle")
|
||||
}
|
||||
} else {
|
||||
Button("Subscribe") {
|
||||
Button {
|
||||
subscriptions.subscribe(video.channel.id) {
|
||||
navigation.sidebarSectionChanged.toggle()
|
||||
}
|
||||
} label: {
|
||||
Label("Subscribe", systemImage: "star.circle")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var addToPlaylistButton: some View {
|
||||
Button("Add to playlist...") {
|
||||
Button {
|
||||
navigation.presentAddToPlaylist(video)
|
||||
} label: {
|
||||
Label("Add to playlist...", systemImage: "text.badge.plus")
|
||||
}
|
||||
}
|
||||
|
||||
func removeFromPlaylistButton(playlistID: String) -> some View {
|
||||
Button("Remove from playlist", role: .destructive) {
|
||||
Button(role: .destructive) {
|
||||
playlists.removeVideoFromPlaylist(videoIndexID: video.indexID!, playlistID: playlistID)
|
||||
} label: {
|
||||
Label("Remove from playlist", systemImage: "text.badge.minus")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user