Fix menu commands

This commit is contained in:
Arkadiusz Fal 2022-03-27 20:38:59 +02:00
parent 8b4838dca5
commit c893e5dc38
3 changed files with 48 additions and 5 deletions

View File

@ -14,6 +14,31 @@ final class NavigationModel: ObservableObject {
case nowPlaying
case search
var stringValue: String {
switch self {
case .favorites:
return "favorites"
case .subscriptions:
return "subscriptions"
case .popular:
return "popular"
case .trending:
return "trending"
case .playlists:
return "playlists"
case let .channel(string):
return "channel\(string)"
case let .playlist(string):
return "playlist\(string)"
case .recentlyOpened:
return "recentlyOpened"
case .search:
return "search"
default:
return ""
}
}
var playlistID: Playlist.ID? {
if case let .playlist(id) = self {
return id

View File

@ -12,29 +12,29 @@ struct MenuCommands: Commands {
private var navigationMenu: some Commands {
CommandGroup(before: .windowSize) {
Button("Favorites") {
model.navigation?.tabSelection = .favorites
setTabSelection(.favorites)
}
.keyboardShortcut("1")
Button("Subscriptions") {
model.navigation?.tabSelection = .subscriptions
setTabSelection(.subscriptions)
}
.disabled(subscriptionsDisabled)
.keyboardShortcut("2")
Button("Popular") {
model.navigation?.tabSelection = .popular
setTabSelection(.popular)
}
.disabled(!(model.accounts?.app.supportsPopular ?? false))
.keyboardShortcut("3")
Button("Trending") {
model.navigation?.tabSelection = .trending
setTabSelection(.trending)
}
.keyboardShortcut("4")
Button("Search") {
model.navigation?.tabSelection = .search
setTabSelection(.search)
}
.keyboardShortcut("f")
@ -42,6 +42,15 @@ struct MenuCommands: Commands {
}
}
private func setTabSelection(_ tabSelection: NavigationModel.TabSelection) {
guard let navigation = model.navigation else {
return
}
navigation.sidebarSectionChanged.toggle()
navigation.tabSelection = tabSelection
}
private var subscriptionsDisabled: Bool {
!(
(model.accounts?.app.supportsSubscriptions ?? false) && model.accounts?.signedIn ?? false

View File

@ -45,6 +45,7 @@ struct Sidebar: View {
Label("Favorites", systemImage: "heart")
.accessibility(label: Text("Favorites"))
}
.id("favorites")
}
if visibleSections.contains(.subscriptions),
accounts.app.supportsSubscriptions && accounts.signedIn
@ -53,6 +54,7 @@ struct Sidebar: View {
Label("Subscriptions", systemImage: "star.circle")
.accessibility(label: Text("Subscriptions"))
}
.id("subscriptions")
}
if visibleSections.contains(.popular), accounts.app.supportsPopular {
@ -60,6 +62,7 @@ struct Sidebar: View {
Label("Popular", systemImage: "arrow.up.right.circle")
.accessibility(label: Text("Popular"))
}
.id("popular")
}
if visibleSections.contains(.trending) {
@ -67,12 +70,14 @@ struct Sidebar: View {
Label("Trending", systemImage: "chart.bar")
.accessibility(label: Text("Trending"))
}
.id("trending")
}
NavigationLink(destination: LazyView(SearchView()), tag: TabSelection.search, selection: $navigation.tabSelection) {
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
}
.id("search")
.keyboardShortcut("f")
}
}
@ -80,8 +85,12 @@ struct Sidebar: View {
private func scrollScrollViewToItem(scrollView: ScrollViewProxy, for selection: TabSelection) {
if case .recentlyOpened = selection {
scrollView.scrollTo("recentlyOpened")
return
} else if case let .playlist(id) = selection {
scrollView.scrollTo(id)
return
}
scrollView.scrollTo(selection.stringValue)
}
}