Navigation model improvements

This commit is contained in:
Arkadiusz Fal 2021-09-29 01:01:49 +02:00
parent d061664570
commit bd813e6771
10 changed files with 113 additions and 149 deletions

View File

@ -6,7 +6,7 @@ final class NavigationModel: ObservableObject {
case watchNow, subscriptions, popular, trending, playlists, channel(String), playlist(String), recentlyOpened(String), search
}
@Published var tabSelection: TabSelection = .watchNow
@Published var tabSelection: TabSelection! = .watchNow
@Published var showingVideo = false
@Published var video: Video?
@ -32,15 +32,13 @@ final class NavigationModel: ObservableObject {
showingVideo = true
}
var tabSelectionOptionalBinding: Binding<TabSelection?> {
Binding<TabSelection?>(
var tabSelectionBinding: Binding<TabSelection> {
Binding<TabSelection>(
get: {
self.tabSelection
self.tabSelection ?? .watchNow
},
set: { newValue in
if newValue != nil {
self.tabSelection = newValue!
}
self.tabSelection = newValue
}
)
}

View File

@ -104,6 +104,8 @@
37732FF02703A26300F04329 /* ValidationStatusView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37732FEF2703A26300F04329 /* ValidationStatusView.swift */; };
37732FF12703A26300F04329 /* ValidationStatusView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37732FEF2703A26300F04329 /* ValidationStatusView.swift */; };
37732FF22703A26300F04329 /* ValidationStatusView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37732FEF2703A26300F04329 /* ValidationStatusView.swift */; };
37732FF42703D32400F04329 /* Sidebar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37732FF32703D32400F04329 /* Sidebar.swift */; };
37732FF52703D32400F04329 /* Sidebar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37732FF32703D32400F04329 /* Sidebar.swift */; };
377A20A92693C9A2002842B8 /* TypedContentAccessors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 377A20A82693C9A2002842B8 /* TypedContentAccessors.swift */; };
377A20AA2693C9A2002842B8 /* TypedContentAccessors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 377A20A82693C9A2002842B8 /* TypedContentAccessors.swift */; };
377A20AB2693C9A2002842B8 /* TypedContentAccessors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 377A20A82693C9A2002842B8 /* TypedContentAccessors.swift */; };
@ -331,6 +333,7 @@
376B2E0626F920D600B1D64D /* SignInRequiredView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInRequiredView.swift; sourceTree = "<group>"; };
376CD21526FBE18D001E1AC1 /* Instance+Fixtures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Instance+Fixtures.swift"; sourceTree = "<group>"; };
37732FEF2703A26300F04329 /* ValidationStatusView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidationStatusView.swift; sourceTree = "<group>"; };
37732FF32703D32400F04329 /* Sidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sidebar.swift; sourceTree = "<group>"; };
377A20A82693C9A2002842B8 /* TypedContentAccessors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypedContentAccessors.swift; sourceTree = "<group>"; };
3788AC2626F6840700F6BAA9 /* WatchNowSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchNowSection.swift; sourceTree = "<group>"; };
3788AC2A26F6842D00F6BAA9 /* WatchNowSectionBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WatchNowSectionBody.swift; sourceTree = "<group>"; };
@ -478,6 +481,7 @@
37BA794626DC2E56002A0235 /* AppSidebarSubscriptions.swift */,
37D4B0C32671614700C925CA /* AppTabNavigation.swift */,
37BD07B42698AA4D003EBB87 /* ContentView.swift */,
37732FF32703D32400F04329 /* Sidebar.swift */,
);
path = Navigation;
sourceTree = "<group>";
@ -1115,6 +1119,7 @@
373CFAEB26975CBF003CB2C6 /* PlaylistFormView.swift in Sources */,
37B81B0226D2CAE700675966 /* PlaybackBar.swift in Sources */,
372915E62687E3B900F5A35B /* Defaults.swift in Sources */,
37732FF42703D32400F04329 /* Sidebar.swift in Sources */,
37D4B19726717E1500C925CA /* Video.swift in Sources */,
37484C2926FC83FF00287258 /* AccountFormView.swift in Sources */,
371F2F1A269B43D300E4A7AB /* NavigationModel.swift in Sources */,
@ -1182,6 +1187,7 @@
37B81AFD26D2C9C900675966 /* VideoDetailsPaddingModifier.swift in Sources */,
37A9965B26D6F8CA006E3224 /* VideosCellsHorizontal.swift in Sources */,
3761AC1026F0F9A600AA496F /* UnsubscribeAlertModifier.swift in Sources */,
37732FF52703D32400F04329 /* Sidebar.swift in Sources */,
379775942689365600DD52A8 /* Array+Next.swift in Sources */,
3748186726A7627F0084E870 /* Video+Fixtures.swift in Sources */,
37BE0BDA26A214630092E2DB /* PlayerViewController.swift in Sources */,

View File

@ -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]$"#

View File

@ -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))

View File

@ -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 {

View File

@ -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 {

View File

@ -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 }

View 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)
}
}
}

View File

@ -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")
}
}
}

View File

@ -8,7 +8,7 @@ struct TVNavigationView: View {
@EnvironmentObject<SearchModel> private var search
var body: some View {
TabView(selection: $navigation.tabSelection) {
TabView(selection: navigation.tabSelectionBinding) {
WatchNowView()
.tabItem { Text("Watch Now") }
.tag(TabSelection.watchNow)