1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Player/Video Details/VideoDetails.swift

201 lines
6.4 KiB
Swift
Raw Normal View History

2022-11-13 23:22:15 +05:30
import Defaults
import Foundation
import SDWebImageSwiftUI
import SwiftUI
struct VideoDetails: View {
enum DetailsPage: String, CaseIterable, Defaults.Serializable {
case info, inspector, chapters, comments, related, queue
}
2022-12-18 04:38:30 +05:30
var video: Video?
2022-11-13 23:22:15 +05:30
@Binding var fullScreen: Bool
var bottomPadding = false
2022-12-18 17:41:06 +05:30
@State private var detailsSize = CGSize.zero
@State private var descriptionVisibility = Constants.descriptionVisibility
2022-11-13 23:22:15 +05:30
@State private var subscribed = false
@State private var subscriptionToggleButtonDisabled = false
@Environment(\.navigationStyle) private var navigationStyle
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
@Environment(\.colorScheme) private var colorScheme
@ObservedObject private var accounts = AccountsModel.shared
let comments = CommentsModel.shared
2022-12-18 04:38:30 +05:30
var player = PlayerModel.shared
2022-11-13 23:22:15 +05:30
2022-11-19 03:13:16 +05:30
@Default(.enableReturnYouTubeDislike) private var enableReturnYouTubeDislike
@Default(.detailsToolbarPosition) private var detailsToolbarPosition
2022-11-13 23:22:15 +05:30
@Default(.playerSidebar) private var playerSidebar
var body: some View {
VStack(alignment: .leading, spacing: 0) {
ControlsBar(
fullScreen: $fullScreen,
expansionState: .constant(.full),
2022-11-13 23:22:15 +05:30
presentingControls: false,
backgroundEnabled: false,
borderTop: false,
detailsTogglePlayer: false,
detailsToggleFullScreen: true
)
2022-12-18 04:38:30 +05:30
.animation(nil, value: player.currentItem)
2022-11-13 23:22:15 +05:30
VideoActions(video: video)
2022-12-18 04:38:30 +05:30
.animation(nil, value: player.currentItem)
2022-11-13 23:22:15 +05:30
2022-12-18 17:41:06 +05:30
detailsPage
#if os(iOS)
.frame(maxWidth: maxWidth)
#endif
2022-11-13 23:22:15 +05:30
}
.overlay(GeometryReader { proxy in
Color.clear
.onAppear {
detailsSize = proxy.size
}
.onChange(of: proxy.size) { newSize in
2022-12-18 17:41:06 +05:30
guard !player.playingFullScreen else { return }
2022-11-13 23:22:15 +05:30
detailsSize = newSize
}
})
.background(colorScheme == .dark ? Color.black : .white)
}
2022-12-18 17:41:06 +05:30
#if os(iOS)
private var maxWidth: Double {
let width = min(detailsSize.width, player.playerSize.width)
if width.isNormal, width > 0 {
return width
2022-11-13 23:22:15 +05:30
}
2022-12-18 17:41:06 +05:30
return 0
2022-11-13 23:22:15 +05:30
}
2022-12-18 17:41:06 +05:30
#endif
2022-11-13 23:22:15 +05:30
2022-12-18 17:41:06 +05:30
private var contentItem: ContentItem {
ContentItem(video: player.currentVideo)
}
2022-11-13 23:22:15 +05:30
var detailsPage: some View {
ScrollView(.vertical, showsIndicators: false) {
2022-12-18 17:41:06 +05:30
if let video {
2022-11-13 23:22:15 +05:30
VStack(alignment: .leading, spacing: 10) {
2022-11-14 02:25:19 +05:30
videoProperties
2022-12-18 17:41:06 +05:30
#if os(iOS)
.opacity(descriptionVisibility ? 1 : 0)
#endif
2022-11-14 02:25:19 +05:30
2022-11-13 23:22:15 +05:30
if !player.videoBeingOpened.isNil && (video.description.isNil || video.description!.isEmpty) {
2022-12-18 17:41:06 +05:30
VStack {
ProgressView()
.progressViewStyle(.circular)
2022-11-13 23:22:15 +05:30
}
2022-12-18 17:41:06 +05:30
.frame(maxWidth: .infinity)
.opacity(descriptionVisibility ? 1 : 0)
2022-11-13 23:22:15 +05:30
} else if video.description != nil, !video.description!.isEmpty {
VideoDescription(video: video, detailsSize: detailsSize)
#if os(iOS)
2022-12-18 17:41:06 +05:30
.opacity(descriptionVisibility ? 1 : 0)
2022-11-13 23:22:15 +05:30
.padding(.bottom, player.playingFullScreen ? 10 : SafeArea.insets.bottom)
#endif
} else if !video.isLocal {
Text("No description")
2022-12-18 17:41:06 +05:30
.font(.caption)
2022-11-13 23:22:15 +05:30
.foregroundColor(.secondary)
}
}
.padding(.top, 10)
.padding(.bottom, 60)
}
}
2022-12-18 17:41:06 +05:30
#if os(iOS)
.onAppear {
if fullScreen {
descriptionVisibility = true
return
}
2022-12-18 18:09:39 +05:30
Delay.by(0.4) { withAnimation(.easeIn(duration: 0.25)) { self.descriptionVisibility = true } }
2022-12-18 17:41:06 +05:30
}
#endif
.transition(.opacity)
.animation(nil, value: player.currentItem)
2022-11-13 23:22:15 +05:30
.padding(.horizontal)
}
2022-11-14 02:25:19 +05:30
@ViewBuilder var videoProperties: some View {
HStack(spacing: 2) {
publishedDateSection
Spacer()
HStack(spacing: 4) {
Image(systemName: "eye")
2022-12-18 17:41:06 +05:30
if let views = video?.viewsCount {
2022-11-14 02:25:19 +05:30
Text(views)
} else {
2022-11-14 03:20:42 +05:30
if player.videoBeingOpened == nil {
Text("?")
} else {
Text("1,234M").redacted(reason: .placeholder)
}
2022-11-14 02:25:19 +05:30
}
Image(systemName: "hand.thumbsup")
2022-12-18 17:41:06 +05:30
if let likes = video?.likesCount {
2022-11-14 02:25:19 +05:30
Text(likes)
} else {
2022-11-14 03:20:42 +05:30
if player.videoBeingOpened == nil {
Text("?")
} else {
Text("1,234M").redacted(reason: .placeholder)
}
2022-11-14 02:25:19 +05:30
}
2022-11-19 03:13:16 +05:30
if enableReturnYouTubeDislike {
2022-11-14 02:25:19 +05:30
Image(systemName: "hand.thumbsdown")
2022-12-18 17:41:06 +05:30
if let dislikes = video?.dislikesCount {
2022-11-14 02:25:19 +05:30
Text(dislikes)
} else {
2022-11-14 03:20:42 +05:30
if player.videoBeingOpened == nil {
Text("?")
} else {
Text("1,234M").redacted(reason: .placeholder)
}
2022-11-14 02:25:19 +05:30
}
}
}
}
.font(.system(size: 12))
.foregroundColor(.secondary)
}
var publishedDateSection: some View {
Group {
if let video {
HStack(spacing: 4) {
if let published = video.publishedDate {
Text(published)
} else {
Text("1 century ago").redacted(reason: .placeholder)
}
}
}
}
}
2022-11-13 23:22:15 +05:30
}
struct VideoDetails_Previews: PreviewProvider {
static var previews: some View {
2022-12-18 17:41:06 +05:30
VideoDetails(video: .fixture, fullScreen: .constant(false))
2022-11-13 23:22:15 +05:30
}
}