yattee/Shared/Player/VideoPlayerView.swift

116 lines
3.5 KiB
Swift
Raw Normal View History

2021-07-18 22:32:46 +00:00
import AVKit
import Siesta
import SwiftUI
struct VideoPlayerView: View {
2021-09-18 20:36:42 +00:00
static let defaultAspectRatio: Double = 1.77777778
static var defaultMinimumHeightLeft: Double {
2021-08-22 19:13:33 +00:00
#if os(macOS)
300
#else
200
#endif
}
2021-07-18 22:32:46 +00:00
@EnvironmentObject<NavigationState> private var navigationState
2021-08-23 21:31:51 +00:00
@EnvironmentObject<PlaybackState> private var playbackState
2021-07-18 22:32:46 +00:00
@ObservedObject private var store = Store<Video>()
2021-08-22 19:13:33 +00:00
#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)
2021-08-23 21:31:51 +00:00
Player(video: video)
.environmentObject(playbackState)
2021-08-22 19:13:33 +00:00
#else
GeometryReader { geometry in
VStack(spacing: 0) {
#if os(iOS)
if verticalSizeClass == .regular {
2021-08-23 21:31:51 +00:00
PlaybackBar(video: video)
2021-08-22 19:13:33 +00:00
}
#elseif os(macOS)
2021-08-23 21:31:51 +00:00
PlaybackBar(video: video)
2021-08-22 19:13:33 +00:00
#endif
2021-08-23 21:31:51 +00:00
Player(video: video)
.environmentObject(playbackState)
2021-08-22 19:13:33 +00:00
.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()
}
#if os(macOS)
2021-08-22 19:13:33 +00:00
.frame(maxWidth: 1000, minHeight: 700)
2021-07-18 22:32:46 +00:00
#elseif os(iOS)
.navigationBarHidden(true)
2021-07-18 22:32:46 +00:00
#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
}