yattee/Shared/Player/VideoPlayerView.swift

118 lines
3.6 KiB
Swift
Raw Normal View History

2021-07-18 22:32:46 +00:00
import AVKit
import Siesta
import SwiftUI
struct VideoPlayerView: View {
2021-08-22 19:13:33 +00:00
static let defaultAspectRatio: CGFloat = 1.77777778
static var defaultMinimumHeightLeft: CGFloat {
#if os(macOS)
300
#else
200
#endif
}
2021-07-18 22:32:46 +00:00
@EnvironmentObject<NavigationState> private var navigationState
@ObservedObject private var store = Store<Video>()
2021-08-22 19:13:33 +00:00
@ObservedObject private var playbackState = PlaybackState()
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
2021-08-16 22:46:18 +00:00
2021-07-18 22:32:46 +00:00
var resource: Resource {
InvidiousAPI.shared.video(video.id)
}
var video: Video
init(_ video: Video) {
self.video = video
resource.addObserver(store)
}
var body: some View {
2021-08-22 19:13:33 +00:00
VStack(spacing: 0) {
#if os(tvOS)
Player(playbackState: playbackState, video: video)
#else
GeometryReader { geometry in
VStack(spacing: 0) {
#if os(iOS)
if verticalSizeClass == .regular {
PlaybackBar(playbackState: playbackState, video: video)
}
#elseif os(macOS)
PlaybackBar(playbackState: playbackState, video: video)
#endif
Player(playbackState: playbackState, video: video)
.modifier(VideoPlayerSizeModifier(geometry: geometry, aspectRatio: playbackState.aspectRatio))
}
2021-08-22 19:13:33 +00:00
.background(.black)
VStack(spacing: 0) {
#if os(iOS)
if verticalSizeClass == .regular {
ScrollView(.vertical, showsIndicators: showScrollIndicators) {
if let video = store.item {
VideoDetails(video: video)
} else {
VideoDetails(video: video)
}
}
}
#else
if let video = store.item {
VideoDetails(video: video)
} else {
VideoDetails(video: video)
}
#endif
}
.modifier(VideoDetailsPaddingModifier(geometry: geometry, aspectRatio: playbackState.aspectRatio))
2021-07-18 22:32:46 +00:00
}
2021-08-22 19:13:33 +00:00
.animation(.linear(duration: 0.2), value: playbackState.aspectRatio)
2021-07-18 22:32:46 +00:00
#endif
}
2021-08-22 19:13:33 +00:00
2021-07-18 22:32:46 +00:00
.onAppear {
resource.loadIfNeeded()
}
.onDisappear {
resource.removeObservers(ownedBy: store)
resource.invalidate()
navigationState.showingVideoDetails = navigationState.returnToDetails
}
#if os(macOS)
2021-07-18 22:32:46 +00:00
.navigationTitle(video.title)
2021-08-22 19:13:33 +00:00
.frame(maxWidth: 1000, minHeight: 700)
2021-07-18 22:32:46 +00:00
#elseif os(iOS)
.navigationBarTitle(video.title, displayMode: .inline)
#endif
}
2021-08-22 19:13:33 +00:00
var showScrollIndicators: Bool {
#if os(macOS)
false
#else
true
#endif
}
}
struct VideoPlayerView_Previews: PreviewProvider {
static var previews: some View {
VStack {
Spacer()
}
.sheet(isPresented: .constant(true)) {
VideoPlayerView(Video.fixture)
.environmentObject(NavigationState())
}
}
2021-07-18 22:32:46 +00:00
}