From 414b6210c05858b9c5c2ef237f300091db3be1f0 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 18 Nov 2022 22:22:57 +0100 Subject: [PATCH] Fix toolbar tabs --- Shared/Player/Controls/VideoDetailsOverlay.swift | 4 +++- Shared/Player/Video Details/VideoDetails.swift | 11 +++++++---- Shared/Player/Video Details/VideoDetailsTool.swift | 13 +++++++++++++ .../Player/Video Details/VideoDetailsToolbar.swift | 12 ++++-------- Shared/Player/VideoPlayerView.swift | 3 ++- 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Shared/Player/Controls/VideoDetailsOverlay.swift b/Shared/Player/Controls/VideoDetailsOverlay.swift index 33430b7e..7a2c423d 100644 --- a/Shared/Player/Controls/VideoDetailsOverlay.swift +++ b/Shared/Player/Controls/VideoDetailsOverlay.swift @@ -4,8 +4,10 @@ import SwiftUI struct VideoDetailsOverlay: View { @EnvironmentObject private var controls + @State private var detailsPage = VideoDetails.DetailsPage.queue + var body: some View { - VideoDetails(sidebarQueue: .constant(false), fullScreen: fullScreenBinding) + VideoDetails(page: $detailsPage, sidebarQueue: .constant(false), fullScreen: fullScreenBinding) .clipShape(RoundedRectangle(cornerRadius: 4)) } diff --git a/Shared/Player/Video Details/VideoDetails.swift b/Shared/Player/Video Details/VideoDetails.swift index 0e05c7e5..0b6afa24 100644 --- a/Shared/Player/Video Details/VideoDetails.swift +++ b/Shared/Player/Video Details/VideoDetails.swift @@ -8,6 +8,7 @@ struct VideoDetails: View { case info, inspector, chapters, comments, related, queue } + @Binding var page: DetailsPage @Binding var sidebarQueue: Bool @Binding var fullScreen: Bool var bottomPadding = false @@ -15,8 +16,6 @@ struct VideoDetails: View { @State private var subscribed = false @State private var subscriptionToggleButtonDisabled = false - @State private var page = DetailsPage.queue - @Environment(\.navigationStyle) private var navigationStyle #if os(iOS) @Environment(\.verticalSizeClass) private var verticalSizeClass @@ -84,7 +83,11 @@ struct VideoDetails: View { } } .onAppear { - page = sidebarQueue ? .inspector : .queue + if video.isNil || + !VideoDetailsTool.find(for: page)!.isAvailable(for: video!, sidebarQueue: sidebarQueue) + { + page = .info + } guard video != nil, accounts.app.supportsSubscriptions else { subscribed = false @@ -243,7 +246,7 @@ struct VideoDetails: View { struct VideoDetails_Previews: PreviewProvider { static var previews: some View { - VideoDetails(sidebarQueue: .constant(true), fullScreen: .constant(false)) + VideoDetails(page: .constant(.info), sidebarQueue: .constant(true), fullScreen: .constant(false)) .injectFixtureEnvironmentObjects() } } diff --git a/Shared/Player/Video Details/VideoDetailsTool.swift b/Shared/Player/Video Details/VideoDetailsTool.swift index 5db6ce2b..6ec045cc 100644 --- a/Shared/Player/Video Details/VideoDetailsTool.swift +++ b/Shared/Player/Video Details/VideoDetailsTool.swift @@ -2,6 +2,19 @@ import Defaults import Foundation struct VideoDetailsTool: Identifiable { + static let all = [ + Self(icon: "info.circle", name: "Info", page: .info), + Self(icon: "wand.and.stars", name: "Inspector", page: .inspector), + Self(icon: "bookmark", name: "Chapters", page: .chapters), + Self(icon: "text.bubble", name: "Comments", page: .comments), + Self(icon: "rectangle.stack.fill", name: "Related", page: .related), + Self(icon: "list.number", name: "Queue", page: .queue) + ] + + static func find(for page: VideoDetails.DetailsPage) -> Self? { + all.first { $0.page == page } + } + var id: String { page.rawValue } diff --git a/Shared/Player/Video Details/VideoDetailsToolbar.swift b/Shared/Player/Video Details/VideoDetailsToolbar.swift index 45d21b5b..6aa79a7a 100644 --- a/Shared/Player/Video Details/VideoDetailsToolbar.swift +++ b/Shared/Player/Video Details/VideoDetailsToolbar.swift @@ -7,14 +7,7 @@ struct VideoDetailsToolbar: View { @Binding var page: VideoDetails.DetailsPage var sidebarQueue: Bool - @State private var tools: [VideoDetailsTool] = [ - .init(icon: "info.circle", name: "Info", page: .info), - .init(icon: "wand.and.stars", name: "Inspector", page: .inspector), - .init(icon: "bookmark", name: "Chapters", page: .chapters), - .init(icon: "text.bubble", name: "Comments", page: .comments), - .init(icon: "rectangle.stack.fill", name: "Related", page: .related), - .init(icon: "list.number", name: "Queue", page: .queue) - ] + @State private var tools = VideoDetailsTool.all @State private var activeTool: VideoDetailsTool? @State private var startedToolPosition: CGRect = .zero @@ -34,6 +27,9 @@ struct VideoDetailsToolbar: View { } } .id(video?.id) + .onAppear { + activeTool = .find(for: page) + } .onChange(of: page) { newValue in activeTool = tools.first { $0.id == newValue.rawValue } } diff --git a/Shared/Player/VideoPlayerView.swift b/Shared/Player/VideoPlayerView.swift index 21c2b16a..9223d517 100644 --- a/Shared/Player/VideoPlayerView.swift +++ b/Shared/Player/VideoPlayerView.swift @@ -30,6 +30,7 @@ struct VideoPlayerView: View { @State private var playerSize: CGSize = .zero { didSet { updateSidebarQueue() } } @State private var hoveringPlayer = false + @State private var detailsPage = VideoDetails.DetailsPage.queue @State private var fullScreenDetails = false @State private var sidebarQueue = defaultSidebarQueueValue @@ -339,7 +340,7 @@ struct VideoPlayerView: View { #if !os(tvOS) if !fullScreenPlayer { - VideoDetails(sidebarQueue: $sidebarQueue, fullScreen: $fullScreenDetails, bottomPadding: detailsNeedBottomPadding) + VideoDetails(page: $detailsPage, sidebarQueue: $sidebarQueue, fullScreen: $fullScreenDetails, bottomPadding: detailsNeedBottomPadding) #if os(iOS) .ignoresSafeArea(.all, edges: .bottom) #endif