2021-07-19 04:02:46 +05:30
|
|
|
import AVKit
|
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct VideoPlayerView: View {
|
2021-08-23 00:43:33 +05:30
|
|
|
static let defaultAspectRatio: CGFloat = 1.77777778
|
|
|
|
static var defaultMinimumHeightLeft: CGFloat {
|
|
|
|
#if os(macOS)
|
|
|
|
300
|
|
|
|
#else
|
|
|
|
200
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-19 04:02:46 +05:30
|
|
|
@EnvironmentObject<NavigationState> private var navigationState
|
2021-08-24 03:01:51 +05:30
|
|
|
@EnvironmentObject<PlaybackState> private var playbackState
|
2021-07-19 04:02:46 +05:30
|
|
|
|
|
|
|
@ObservedObject private var store = Store<Video>()
|
|
|
|
|
2021-08-23 00:43:33 +05:30
|
|
|
#if os(iOS)
|
|
|
|
@Environment(\.verticalSizeClass) private var verticalSizeClass
|
|
|
|
#endif
|
2021-08-17 04:16:18 +05:30
|
|
|
|
2021-07-19 04:02:46 +05:30
|
|
|
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-23 00:43:33 +05:30
|
|
|
VStack(spacing: 0) {
|
|
|
|
#if os(tvOS)
|
2021-08-24 03:01:51 +05:30
|
|
|
Player(video: video)
|
|
|
|
.environmentObject(playbackState)
|
2021-08-23 00:43:33 +05:30
|
|
|
#else
|
|
|
|
GeometryReader { geometry in
|
|
|
|
VStack(spacing: 0) {
|
|
|
|
#if os(iOS)
|
|
|
|
if verticalSizeClass == .regular {
|
2021-08-24 03:01:51 +05:30
|
|
|
PlaybackBar(video: video)
|
2021-08-23 00:43:33 +05:30
|
|
|
}
|
|
|
|
#elseif os(macOS)
|
2021-08-24 03:01:51 +05:30
|
|
|
PlaybackBar(video: video)
|
2021-08-23 00:43:33 +05:30
|
|
|
#endif
|
|
|
|
|
2021-08-24 03:01:51 +05:30
|
|
|
Player(video: video)
|
|
|
|
.environmentObject(playbackState)
|
2021-08-23 00:43:33 +05:30
|
|
|
.modifier(VideoPlayerSizeModifier(geometry: geometry, aspectRatio: playbackState.aspectRatio))
|
2021-08-02 02:55:50 +05:30
|
|
|
}
|
2021-08-23 00:43:33 +05:30
|
|
|
.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-19 04:02:46 +05:30
|
|
|
}
|
2021-08-23 00:43:33 +05:30
|
|
|
.animation(.linear(duration: 0.2), value: playbackState.aspectRatio)
|
2021-07-19 04:02:46 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-08-23 00:43:33 +05:30
|
|
|
|
2021-07-19 04:02:46 +05:30
|
|
|
.onAppear {
|
|
|
|
resource.loadIfNeeded()
|
|
|
|
}
|
|
|
|
.onDisappear {
|
|
|
|
resource.removeObservers(ownedBy: store)
|
|
|
|
resource.invalidate()
|
|
|
|
|
|
|
|
navigationState.showingVideoDetails = navigationState.returnToDetails
|
|
|
|
}
|
2021-08-02 02:55:50 +05:30
|
|
|
#if os(macOS)
|
2021-07-19 04:02:46 +05:30
|
|
|
.navigationTitle(video.title)
|
2021-08-23 00:43:33 +05:30
|
|
|
.frame(maxWidth: 1000, minHeight: 700)
|
2021-07-19 04:02:46 +05:30
|
|
|
#elseif os(iOS)
|
|
|
|
.navigationBarTitle(video.title, displayMode: .inline)
|
|
|
|
#endif
|
|
|
|
}
|
2021-08-23 00:43:33 +05:30
|
|
|
|
|
|
|
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-19 04:02:46 +05:30
|
|
|
}
|