yattee/Shared/Player/VideoPlayerView.swift

115 lines
3.4 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-09-25 08:18:22 +00:00
@StateObject private var store = Store<Video>()
2021-07-18 22:32:46 +00:00
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-09-25 08:18:22 +00:00
@EnvironmentObject<InvidiousAPI> private var api
@EnvironmentObject<PlaybackModel> private var playback
2021-07-18 22:32:46 +00:00
var resource: Resource {
2021-09-25 08:18:22 +00:00
api.video(video.id)
2021-07-18 22:32:46 +00:00
}
var video: Video
init(_ video: Video) {
self.video = video
}
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)
2021-09-25 08:18:22 +00:00
.environmentObject(playback)
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)
2021-09-25 08:18:22 +00:00
.environmentObject(playback)
.modifier(VideoPlayerSizeModifier(geometry: geometry, aspectRatio: playback.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
}
2021-09-25 08:18:22 +00:00
.modifier(VideoDetailsPaddingModifier(geometry: geometry, aspectRatio: playback.aspectRatio))
2021-07-18 22:32:46 +00:00
}
2021-09-25 08:18:22 +00:00
.animation(.linear(duration: 0.2), value: playback.aspectRatio)
2021-07-18 22:32:46 +00:00
#endif
}
.onAppear {
2021-09-25 08:18:22 +00:00
resource.addObserver(store)
2021-07-18 22:32:46 +00:00
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)
2021-09-29 11:45:00 +00:00
.injectFixtureEnvironmentObjects()
2021-08-22 19:13:33 +00:00
}
}
2021-07-18 22:32:46 +00:00
}