Setting for app tab navigation section

This commit is contained in:
Arkadiusz Fal 2021-11-07 21:51:22 +01:00
parent 3ca2105b9b
commit 278c4cad69
9 changed files with 150 additions and 66 deletions

View File

@ -57,6 +57,10 @@ extension Defaults.Keys {
static let trendingCategory = Key<TrendingCategory>("trendingCategory", default: .default)
static let trendingCountry = Key<Country>("trendingCountry", default: .us)
#if os(iOS)
static let tabNavigationSection = Key<TabNavigationSectionSetting>("tabNavigationSection", default: .trending)
#endif
}
enum ResolutionSetting: String, CaseIterable, Defaults.Serializable {
@ -92,3 +96,9 @@ enum PlayerSidebarSetting: String, CaseIterable, Defaults.Serializable {
#endif
}
}
#if os(iOS)
enum TabNavigationSectionSetting: String, Defaults.Serializable {
case trending, popular
}
#endif

View File

@ -25,43 +25,58 @@ struct FavoriteItemView: View {
}
var body: some View {
VStack(alignment: .leading, spacing: 2) {
Text(label)
.font(.title3.bold())
.foregroundColor(.secondary)
.contextMenu {
Button {
favoritesModel.remove(item)
} label: {
Label("Remove from Favorites", systemImage: "trash")
}
Group {
if isVisible {
VStack(alignment: .leading, spacing: 2) {
Text(label)
.font(.title3.bold())
.foregroundColor(.secondary)
.contextMenu {
Button {
favoritesModel.remove(item)
} label: {
Label("Remove from Favorites", systemImage: "trash")
}
}
.contentShape(Rectangle())
#if os(tvOS)
.padding(.leading, 40)
#else
.padding(.leading, 15)
#endif
HorizontalCells(items: store.contentItems)
}
.contentShape(Rectangle())
#if os(tvOS)
.padding(.leading, 40)
#else
.padding(.leading, 15)
#endif
HorizontalCells(items: store.contentItems)
}
.contentShape(Rectangle())
.opacity(dragging?.id == item.id ? 0.5 : 1)
.onAppear {
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if !os(tvOS)
.onDrag {
dragging = item
return NSItemProvider(object: item.id as NSString)
.opacity(dragging?.id == item.id ? 0.5 : 1)
.onAppear {
resource?.addObserver(store)
resource?.loadIfNeeded()
}
#if !os(tvOS)
.onDrag {
dragging = item
return NSItemProvider(object: item.id as NSString)
}
.onDrop(
of: [UTType.text],
delegate: DropFavorite(item: item, favorites: $favorites, current: $dragging)
)
#endif
}
.onDrop(
of: [UTType.text],
delegate: DropFavorite(item: item, favorites: $favorites, current: $dragging)
)
#endif
}
}
private var isVisible: Bool {
switch item.section {
case .subscriptions:
return accounts.app.supportsSubscriptions
case .popular:
return accounts.app.supportsPopular
default:
return true
}
}
private var resource: Resource? {

View File

@ -8,6 +8,8 @@ struct AppTabNavigation: View {
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SearchModel> private var search
@Default(.tabNavigationSection) private var tabNavigationSection
var body: some View {
TabView(selection: navigation.tabSelectionBinding) {
NavigationView {
@ -20,7 +22,7 @@ struct AppTabNavigation: View {
}
.tag(TabSelection.favorites)
if accounts.app.supportsSubscriptions {
if subscriptionsVisible {
NavigationView {
LazyView(SubscriptionsView())
.toolbar { toolbarContent }
@ -32,29 +34,21 @@ struct AppTabNavigation: View {
.tag(TabSelection.subscriptions)
}
// TODO: reenable with settings
if accounts.app.supportsPopular && false {
NavigationView {
LazyView(PopularView())
.toolbar { toolbarContent }
if subscriptionsVisible {
if accounts.app.supportsPopular {
if tabNavigationSection == .popular {
popularNavigationView
} else {
trendingNavigationView
}
}
.tabItem {
Label("Popular", systemImage: "chart.bar")
.accessibility(label: Text("Popular"))
} else {
if accounts.app.supportsPopular {
popularNavigationView
}
.tag(TabSelection.popular)
trendingNavigationView
}
NavigationView {
LazyView(TrendingView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
.accessibility(label: Text("Trending"))
}
.tag(TabSelection.trending)
if accounts.app.supportsUserPlaylists {
NavigationView {
LazyView(PlaylistsView())
@ -125,6 +119,34 @@ struct AppTabNavigation: View {
}
}
private var subscriptionsVisible: Bool {
accounts.app.supportsSubscriptions && !(accounts.current?.anonymous ?? true)
}
private var popularNavigationView: some View {
NavigationView {
LazyView(PopularView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Popular", systemImage: "chart.bar")
.accessibility(label: Text("Popular"))
}
.tag(TabSelection.popular)
}
private var trendingNavigationView: some View {
NavigationView {
LazyView(TrendingView())
.toolbar { toolbarContent }
}
.tabItem {
Label("Trending", systemImage: "chart.line.uptrend.xyaxis")
.accessibility(label: Text("Trending"))
}
.tag(TabSelection.trending)
}
private var playerNavigationLink: some View {
NavigationLink(isActive: $player.playerNavigationLinkActive, destination: {
VideoPlayerView()

View File

@ -85,6 +85,7 @@ struct ContentView: View {
}
.sheet(isPresented: $navigation.presentingPlaylistForm) {
PlaylistFormView(playlist: $navigation.editedPlaylist)
.environmentObject(accounts)
.environmentObject(playlists)
}
.sheet(isPresented: $navigation.presentingSettings, onDismiss: openWelcomeScreenIfAccountEmpty) {

View File

@ -8,15 +8,17 @@ final class PlayerViewController: UIViewController {
var playerModel: PlayerModel!
var playerViewController = AVPlayerViewController()
var aspectRatio: Double? {
let ratio = Double(playerViewController.videoBounds.width) / Double(playerViewController.videoBounds.height)
#if !os(tvOS)
var aspectRatio: Double? {
let ratio = Double(playerViewController.videoBounds.width) / Double(playerViewController.videoBounds.height)
if !ratio.isFinite {
return VideoPlayerView.defaultAspectRatio
guard ratio.isFinite else {
return VideoPlayerView.defaultAspectRatio // swiftlint:disable:this implicit_return
}
return [ratio, 1.0].max()!
}
return [ratio, 1.0].max()!
}
#endif
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

View File

@ -14,7 +14,7 @@ struct PlaylistFormView: View {
@Environment(\.dismiss) private var dismiss
@EnvironmentObject<InvidiousAPI> private var api
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlaylistsModel> private var playlists
var editing: Bool {
@ -185,7 +185,7 @@ struct PlaylistFormView: View {
}
var resource: Resource? {
editing ? api.playlist(playlist.id) : api.playlists
editing ? accounts.api.playlist(playlist.id) : accounts.api.playlists
}
var visibilityFormItem: some View {
@ -230,7 +230,7 @@ struct PlaylistFormView: View {
}
func deletePlaylistAndDismiss() {
api.playlist(playlist.id)?.request(.delete).onSuccess { _ in
accounts.api.playlist(playlist.id)?.request(.delete).onSuccess { _ in
playlist = nil
playlists.load(force: true)
dismiss()

View File

@ -51,16 +51,20 @@ struct PlaylistsView: View {
#if os(tvOS)
.fullScreenCover(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
.environmentObject(accounts)
}
.fullScreenCover(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
.environmentObject(accounts)
}
#else
.sheet(isPresented: $showingNewPlaylist, onDismiss: selectCreatedPlaylist) {
PlaylistFormView(playlist: $createdPlaylist)
.environmentObject(accounts)
}
.sheet(isPresented: $showingEditPlaylist, onDismiss: selectEditedPlaylist) {
PlaylistFormView(playlist: $editedPlaylist)
.environmentObject(accounts)
}
#endif
.toolbar {

View File

@ -4,11 +4,18 @@ import SwiftUI
struct BrowsingSettings: View {
@Default(.channelOnThumbnail) private var channelOnThumbnail
@Default(.timeOnThumbnail) private var timeOnThumbnail
#if os(iOS)
@Default(.tabNavigationSection) private var tabNavigationSection
#endif
var body: some View {
Section(header: SettingsHeader(text: "Thumbnails")) {
Section(header: SettingsHeader(text: "Browsing"), footer: footer) {
Toggle("Display channel names on thumbnails", isOn: $channelOnThumbnail)
Toggle("Display video length on thumbnails", isOn: $timeOnThumbnail)
#if os(iOS)
preferredTabPicker
#endif
}
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
@ -16,6 +23,23 @@ struct BrowsingSettings: View {
Spacer()
#endif
}
var footer: some View {
#if os(iOS)
Text("This tab will be displayed when there is no space to display all tabs")
#else
EmptyView()
#endif
}
#if os(iOS)
var preferredTabPicker: some View {
Picker("Preferred tab", selection: $tabNavigationSection) {
Text("Trending").tag(TabNavigationSectionSetting.trending)
Text("Popular").tag(TabNavigationSectionSetting.popular)
}
}
#endif
}
struct BrowsingSettings_Previews: PreviewProvider {

View File

@ -44,7 +44,12 @@ struct EditFavorites: View {
ForEach(model.addableItems()) { item in
HStack {
Text(label(item))
HStack {
Text(label(item))
Spacer()
Text("only with Invidious")
.foregroundColor(.secondary)
}
Spacer()
@ -52,6 +57,7 @@ struct EditFavorites: View {
model.add(item)
} label: {
Label("Add to Favorites", systemImage: "heart")
.font(.system(size: 30))
}
}
}