Add settings for selecting visible sections (fixes #16)

This commit is contained in:
Arkadiusz Fal 2021-12-01 12:22:19 +01:00
parent ff83abd103
commit e61d1dfe2e
11 changed files with 265 additions and 129 deletions

View File

@ -23,7 +23,7 @@ final class NavigationModel: ObservableObject {
} }
} }
@Published var tabSelection: TabSelection! = .favorites @Published var tabSelection: TabSelection!
@Published var presentingAddToPlaylist = false @Published var presentingAddToPlaylist = false
@Published var videoToAddToPlaylist: Video! @Published var videoToAddToPlaylist: Video!

View File

@ -44,9 +44,7 @@ extension Defaults.Keys {
static let trendingCategory = Key<TrendingCategory>("trendingCategory", default: .default) static let trendingCategory = Key<TrendingCategory>("trendingCategory", default: .default)
static let trendingCountry = Key<Country>("trendingCountry", default: .us) static let trendingCountry = Key<Country>("trendingCountry", default: .us)
#if os(iOS) static let visibleSections = Key<Set<VisibleSection>>("visibleSections", default: [.favorites, .subscriptions, .trending, .playlists])
static let tabNavigationSection = Key<TabNavigationSectionSetting>("tabNavigationSection", default: .trending)
#endif
} }
enum ResolutionSetting: String, CaseIterable, Defaults.Serializable { enum ResolutionSetting: String, CaseIterable, Defaults.Serializable {
@ -83,8 +81,48 @@ enum PlayerSidebarSetting: String, CaseIterable, Defaults.Serializable {
} }
} }
#if os(iOS) enum VisibleSection: String, CaseIterable, Comparable, Defaults.Serializable {
enum TabNavigationSectionSetting: String, Defaults.Serializable { case favorites, subscriptions, popular, trending, playlists
case trending, popular
static func from(_ string: String) -> VisibleSection {
allCases.first { $0.rawValue == string }!
}
var title: String {
rawValue.localizedCapitalized
}
var tabSelection: TabSelection {
switch self {
case .favorites:
return TabSelection.favorites
case .subscriptions:
return TabSelection.subscriptions
case .popular:
return TabSelection.popular
case .trending:
return TabSelection.trending
case .playlists:
return TabSelection.playlists
}
}
private var sortOrder: Int {
switch self {
case .favorites:
return 0
case .subscriptions:
return 1
case .popular:
return 2
case .trending:
return 3
case .playlists:
return 4
}
}
static func < (lhs: Self, rhs: Self) -> Bool {
lhs.sortOrder < rhs.sortOrder
}
} }
#endif

View File

@ -1,3 +1,4 @@
import Defaults
import SwiftUI import SwiftUI
#if os(iOS) #if os(iOS)
import Introspect import Introspect
@ -5,6 +6,9 @@ import SwiftUI
struct AppSidebarNavigation: View { struct AppSidebarNavigation: View {
@EnvironmentObject<AccountsModel> private var accounts @EnvironmentObject<AccountsModel> private var accounts
@Default(.visibleSections) private var visibleSections
#if os(iOS) #if os(iOS)
@EnvironmentObject<NavigationModel> private var navigation @EnvironmentObject<NavigationModel> private var navigation
@State private var didApplyPrimaryViewWorkAround = false @State private var didApplyPrimaryViewWorkAround = false

View File

@ -8,69 +8,31 @@ struct AppTabNavigation: View {
@EnvironmentObject<RecentsModel> private var recents @EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var search @EnvironmentObject<SearchModel> private var search
@Default(.tabNavigationSection) private var tabNavigationSection @Default(.visibleSections) private var visibleSections
var body: some View { var body: some View {
TabView(selection: navigation.tabSelectionBinding) { TabView(selection: navigation.tabSelectionBinding) {
NavigationView { if visibleSections.contains(.favorites) {
LazyView(FavoritesView()) favoritesNavigationView
.toolbar { toolbarContent }
}
.tabItem {
Label("Favorites", systemImage: "heart")
.accessibility(label: Text("Favorites"))
}
.tag(TabSelection.favorites)
if subscriptionsVisible {
NavigationView {
LazyView(SubscriptionsView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Subscriptions", systemImage: "star.circle.fill")
.accessibility(label: Text("Subscriptions"))
}
.tag(TabSelection.subscriptions)
} }
if subscriptionsVisible { if subscriptionsVisible {
if accounts.app.supportsPopular { subscriptionsNavigationView
if tabNavigationSection == .popular {
popularNavigationView
} else {
trendingNavigationView
} }
} else {
trendingNavigationView if visibleSections.contains(.popular), accounts.app.supportsPopular {
}
} else {
if accounts.app.supportsPopular {
popularNavigationView popularNavigationView
} }
if visibleSections.contains(.trending) {
trendingNavigationView trendingNavigationView
} }
if accounts.app.supportsUserPlaylists { if visibleSections.contains(.playlists), accounts.app.supportsUserPlaylists {
NavigationView { playlistsNavigationView
LazyView(PlaylistsView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Playlists", systemImage: "list.and.film")
.accessibility(label: Text("Playlists"))
}
.tag(TabSelection.playlists)
} }
NavigationView { searchNavigationView
LazyView(SearchView())
}
.tabItem {
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
}
.tag(TabSelection.search)
} }
.id(accounts.current?.id ?? "") .id(accounts.current?.id ?? "")
.environment(\.navigationStyle, .tab) .environment(\.navigationStyle, .tab)
@ -107,7 +69,32 @@ struct AppTabNavigation: View {
) )
} }
private var favoritesNavigationView: some View {
NavigationView {
LazyView(FavoritesView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Favorites", systemImage: "heart")
.accessibility(label: Text("Favorites"))
}
.tag(TabSelection.favorites)
}
private var subscriptionsNavigationView: some View {
NavigationView {
LazyView(SubscriptionsView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Subscriptions", systemImage: "star.circle.fill")
.accessibility(label: Text("Subscriptions"))
}
.tag(TabSelection.subscriptions)
}
private var subscriptionsVisible: Bool { private var subscriptionsVisible: Bool {
visibleSections.contains(.subscriptions) &&
accounts.app.supportsSubscriptions && !(accounts.current?.anonymous ?? true) accounts.app.supportsSubscriptions && !(accounts.current?.anonymous ?? true)
} }
@ -135,6 +122,30 @@ struct AppTabNavigation: View {
.tag(TabSelection.trending) .tag(TabSelection.trending)
} }
private var playlistsNavigationView: some View {
NavigationView {
LazyView(PlaylistsView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Playlists", systemImage: "list.and.film")
.accessibility(label: Text("Playlists"))
}
.tag(TabSelection.playlists)
}
private var searchNavigationView: some View {
NavigationView {
LazyView(SearchView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Search", systemImage: "magnifyingglass")
.accessibility(label: Text("Search"))
}
.tag(TabSelection.search)
}
private var playerNavigationLink: some View { private var playerNavigationLink: some View {
NavigationLink(isActive: $player.playerNavigationLinkActive, destination: { NavigationLink(isActive: $player.playerNavigationLinkActive, destination: {
VideoPlayerView() VideoPlayerView()

View File

@ -140,6 +140,16 @@ struct ContentView: View {
if !accounts.current.isNil { if !accounts.current.isNil {
player.loadHistoryDetails() player.loadHistoryDetails()
} }
var section = Defaults[.visibleSections].min()?.tabSelection
#if os(macOS)
if section == .playlists {
section = .search
}
#endif
navigation.tabSelection = section ?? .search
} }
func openWelcomeScreenIfAccountEmpty() { func openWelcomeScreenIfAccountEmpty() {

View File

@ -1,3 +1,4 @@
import Defaults
import SwiftUI import SwiftUI
struct Sidebar: View { struct Sidebar: View {
@ -6,6 +7,8 @@ struct Sidebar: View {
@EnvironmentObject<PlaylistsModel> private var playlists @EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<SubscriptionsModel> private var subscriptions @EnvironmentObject<SubscriptionsModel> private var subscriptions
@Default(.visibleSections) private var visibleSections
var body: some View { var body: some View {
ScrollViewReader { scrollView in ScrollViewReader { scrollView in
List { List {
@ -16,11 +19,11 @@ struct Sidebar: View {
.id("recentlyOpened") .id("recentlyOpened")
if accounts.api.signedIn { if accounts.api.signedIn {
if accounts.app.supportsSubscriptions { if visibleSections.contains(.subscriptions), accounts.app.supportsSubscriptions {
AppSidebarSubscriptions() AppSidebarSubscriptions()
} }
if accounts.app.supportsUserPlaylists { if visibleSections.contains(.playlists), accounts.app.supportsUserPlaylists {
AppSidebarPlaylists() AppSidebarPlaylists()
} }
} }
@ -47,28 +50,34 @@ struct Sidebar: View {
var mainNavigationLinks: some View { var mainNavigationLinks: some View {
Section(header: Text("Videos")) { Section(header: Text("Videos")) {
if visibleSections.contains(.favorites) {
NavigationLink(destination: LazyView(FavoritesView()), tag: TabSelection.favorites, selection: $navigation.tabSelection) { NavigationLink(destination: LazyView(FavoritesView()), tag: TabSelection.favorites, selection: $navigation.tabSelection) {
Label("Favorites", systemImage: "heart") Label("Favorites", systemImage: "heart")
.accessibility(label: Text("Favorites")) .accessibility(label: Text("Favorites"))
} }
if accounts.app.supportsSubscriptions && accounts.signedIn { }
if visibleSections.contains(.subscriptions),
accounts.app.supportsSubscriptions && accounts.signedIn
{
NavigationLink(destination: LazyView(SubscriptionsView()), tag: TabSelection.subscriptions, selection: $navigation.tabSelection) { NavigationLink(destination: LazyView(SubscriptionsView()), tag: TabSelection.subscriptions, selection: $navigation.tabSelection) {
Label("Subscriptions", systemImage: "star.circle") Label("Subscriptions", systemImage: "star.circle")
.accessibility(label: Text("Subscriptions")) .accessibility(label: Text("Subscriptions"))
} }
} }
if accounts.app.supportsPopular { if visibleSections.contains(.popular), accounts.app.supportsPopular {
NavigationLink(destination: LazyView(PopularView()), tag: TabSelection.popular, selection: $navigation.tabSelection) { NavigationLink(destination: LazyView(PopularView()), tag: TabSelection.popular, selection: $navigation.tabSelection) {
Label("Popular", systemImage: "arrow.up.right.circle") Label("Popular", systemImage: "arrow.up.right.circle")
.accessibility(label: Text("Popular")) .accessibility(label: Text("Popular"))
} }
} }
if visibleSections.contains(.trending) {
NavigationLink(destination: LazyView(TrendingView()), tag: TabSelection.trending, selection: $navigation.tabSelection) { NavigationLink(destination: LazyView(TrendingView()), tag: TabSelection.trending, selection: $navigation.tabSelection) {
Label("Trending", systemImage: "chart.bar") Label("Trending", systemImage: "chart.bar")
.accessibility(label: Text("Trending")) .accessibility(label: Text("Trending"))
} }
}
NavigationLink(destination: LazyView(SearchView()), tag: TabSelection.search, selection: $navigation.tabSelection) { NavigationLink(destination: LazyView(SearchView()), tag: TabSelection.search, selection: $navigation.tabSelection) {
Label("Search", systemImage: "magnifyingglass") Label("Search", systemImage: "magnifyingglass")
@ -78,7 +87,7 @@ struct Sidebar: View {
} }
} }
func scrollScrollViewToItem(scrollView: ScrollViewProxy, for selection: TabSelection) { private func scrollScrollViewToItem(scrollView: ScrollViewProxy, for selection: TabSelection) {
if case .recentlyOpened = selection { if case .recentlyOpened = selection {
scrollView.scrollTo("recentlyOpened") scrollView.scrollTo("recentlyOpened")
} else if case let .playlist(id) = selection { } else if case let .playlist(id) = selection {

View File

@ -176,7 +176,7 @@ struct SearchView: View {
.navigationTitle("Search") .navigationTitle("Search")
#endif #endif
#if os(iOS) #if os(iOS)
.navigationBarHidden(true) .navigationBarHidden(!Defaults[.visibleSections].isEmpty || navigationStyle == .sidebar)
#endif #endif
} }

View File

@ -4,43 +4,99 @@ import SwiftUI
struct BrowsingSettings: View { struct BrowsingSettings: View {
@Default(.channelOnThumbnail) private var channelOnThumbnail @Default(.channelOnThumbnail) private var channelOnThumbnail
@Default(.timeOnThumbnail) private var timeOnThumbnail @Default(.timeOnThumbnail) private var timeOnThumbnail
#if os(iOS) @Default(.saveHistory) private var saveHistory
@Default(.tabNavigationSection) private var tabNavigationSection @Default(.visibleSections) private var visibleSections
#endif
var body: some View { var body: some View {
Section(header: SettingsHeader(text: "Browsing"), footer: footer) { Group {
Toggle("Display channel names on thumbnails", isOn: $channelOnThumbnail) Section(header: SettingsHeader(text: "Browsing")) {
Toggle("Display video length on thumbnails", isOn: $timeOnThumbnail) Toggle("Show channel name on thumbnail", isOn: $channelOnThumbnail)
Toggle("Show video length on thumbnail", isOn: $timeOnThumbnail)
Toggle("Save history of played videos", isOn: $saveHistory)
}
Section(header: SettingsHeader(text: "Sections")) {
#if os(macOS)
let list = List(VisibleSection.allCases, id: \.self) { section in
VisibleSectionSelectionRow(
title: section.title,
selected: visibleSections.contains(section)
) { value in
toggleSection(section, value: value)
}
}
#if os(iOS) Group {
preferredTabPicker if #available(macOS 12.0, *) {
list
.listStyle(.inset(alternatesRowBackgrounds: true))
} else {
list
.listStyle(.inset)
}
}
#else
ForEach(VisibleSection.allCases, id: \.self) { section in
VisibleSectionSelectionRow(
title: section.title,
selected: visibleSections.contains(section)
) { value in
toggleSection(section, value: value)
}
}
#endif #endif
} }
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}
func toggleSection(_ section: VisibleSection, value: Bool) {
if value {
visibleSections.insert(section)
} else {
visibleSections.remove(section)
}
}
struct VisibleSectionSelectionRow: View {
let title: String
let selected: Bool
var action: (Bool) -> Void
@State private var toggleChecked = false
var body: some View {
Button(action: { action(!selected) }) {
HStack {
#if os(macOS) #if os(macOS)
Toggle(isOn: $toggleChecked) {
Text(self.title)
Spacer() Spacer()
#endif
} }
.onAppear {
var footer: some View { toggleChecked = selected
#if os(iOS) }
Text("This tab will be displayed when there is no space to display all tabs") .onChange(of: toggleChecked) { new in
action(new)
}
#else #else
EmptyView() Text(self.title)
#endif Spacer()
} if selected {
Image(systemName: "checkmark")
#if os(iOS) #if os(iOS)
var preferredTabPicker: some View { .foregroundColor(.accentColor)
Picker("Preferred tab", selection: $tabNavigationSection) { #endif
Text("Trending").tag(TabNavigationSectionSetting.trending)
Text("Popular").tag(TabNavigationSectionSetting.popular)
}
} }
#endif #endif
} }
.contentShape(Rectangle())
}
#if !os(tvOS)
.buttonStyle(.plain)
#endif
}
}
}
struct BrowsingSettings_Previews: PreviewProvider { struct BrowsingSettings_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {

View File

@ -27,7 +27,6 @@ struct PlaybackSettings: View {
} }
keywordsToggle keywordsToggle
saveHistoryToggle
} }
#else #else
Section(header: SettingsHeader(text: "Source")) { Section(header: SettingsHeader(text: "Source")) {
@ -45,7 +44,6 @@ struct PlaybackSettings: View {
#endif #endif
keywordsToggle keywordsToggle
saveHistoryToggle
#endif #endif
} }
@ -109,10 +107,6 @@ struct PlaybackSettings: View {
private var keywordsToggle: some View { private var keywordsToggle: some View {
Toggle("Show video keywords", isOn: $showKeywords) Toggle("Show video keywords", isOn: $showKeywords)
} }
private var saveHistoryToggle: some View {
Toggle("Save history of played videos", isOn: $saveHistory)
}
} }
struct PlaybackSettings_Previews: PreviewProvider { struct PlaybackSettings_Previews: PreviewProvider {

View File

@ -1,3 +1,4 @@
import Defaults
import Foundation import Foundation
import SwiftUI import SwiftUI
@ -7,7 +8,11 @@ struct FavoriteButton: View {
@State private var isFavorite = false @State private var isFavorite = false
@Default(.visibleSections) private var visibleSections
var body: some View { var body: some View {
Group {
if visibleSections.contains(.favorites) {
Button { Button {
favorites.toggle(item) favorites.toggle(item)
isFavorite.toggle() isFavorite.toggle()
@ -21,5 +26,9 @@ struct FavoriteButton: View {
.onAppear { .onAppear {
isFavorite = favorites.contains(item) isFavorite = favorites.contains(item)
} }
} else {
EmptyView()
}
}
} }
} }

View File

@ -8,29 +8,34 @@ struct TVNavigationView: View {
@EnvironmentObject<RecentsModel> private var recents @EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var search @EnvironmentObject<SearchModel> private var search
@Default(.visibleSections) private var visibleSections
var body: some View { var body: some View {
TabView(selection: navigation.tabSelectionBinding) { TabView(selection: navigation.tabSelectionBinding) {
if visibleSections.contains(.favorites) {
FavoritesView() FavoritesView()
.tabItem { Text("Favorites") } .tabItem { Text("Favorites") }
.tag(TabSelection.favorites) .tag(TabSelection.favorites)
}
if accounts.app.supportsSubscriptions { if visibleSections.contains(.subscriptions), accounts.app.supportsSubscriptions {
SubscriptionsView() SubscriptionsView()
.tabItem { Text("Subscriptions") } .tabItem { Text("Subscriptions") }
.tag(TabSelection.subscriptions) .tag(TabSelection.subscriptions)
} }
if accounts.app.supportsPopular { if visibleSections.contains(.popular), accounts.app.supportsPopular {
PopularView() PopularView()
.tabItem { Text("Popular") } .tabItem { Text("Popular") }
.tag(TabSelection.popular) .tag(TabSelection.popular)
} }
if visibleSections.contains(.trending) {
TrendingView() TrendingView()
.tabItem { Text("Trending") } .tabItem { Text("Trending") }
.tag(TabSelection.trending) .tag(TabSelection.trending)
}
if accounts.app.supportsUserPlaylists { if visibleSections.contains(.playlists), accounts.app.supportsUserPlaylists {
PlaylistsView() PlaylistsView()
.tabItem { Text("Playlists") } .tabItem { Text("Playlists") }
.tag(TabSelection.playlists) .tag(TabSelection.playlists)