1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 23:00:32 +05:30
yattee/Shared/Player/VideoDetails.swift

397 lines
13 KiB
Swift
Raw Normal View History

2021-11-04 04:44:09 +05:30
import Defaults
2021-08-23 00:43:33 +05:30
import Foundation
2021-12-18 01:31:05 +05:30
import SDWebImageSwiftUI
2021-08-23 00:43:33 +05:30
import SwiftUI
import SwiftUIPager
2021-08-23 00:43:33 +05:30
struct VideoDetails: View {
2022-08-08 23:32:46 +05:30
enum DetailsPage: String, CaseIterable, Defaults.Serializable {
case info, chapters, comments, related, queue
var index: Int {
switch self {
case .info:
return 0
case .chapters:
return 1
case .comments:
return 2
case .related:
return 3
case .queue:
return 4
}
}
}
var sidebarQueue: Bool
2022-06-25 22:03:35 +05:30
@Binding var fullScreen: Bool
2021-08-26 03:42:59 +05:30
@State private var subscribed = false
2022-03-26 23:31:38 +05:30
@State private var subscriptionToggleButtonDisabled = false
2021-08-26 03:42:59 +05:30
@StateObject private var page: Page = .first()
2021-12-18 01:31:05 +05:30
@Environment(\.navigationStyle) private var navigationStyle
2022-08-19 04:10:46 +05:30
#if os(iOS)
@Environment(\.verticalSizeClass) private var verticalSizeClass
#endif
2021-10-21 03:51:50 +05:30
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<CommentsModel> private var comments
2021-12-18 01:31:05 +05:30
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlayerModel> private var player
2021-12-18 01:31:05 +05:30
@EnvironmentObject<RecentsModel> private var recents
@EnvironmentObject<SubscriptionsModel> private var subscriptions
@Default(.playerDetailsPageButtonLabelStyle) private var playerDetailsPageButtonLabelStyle
2021-11-04 04:44:09 +05:30
var currentPage: DetailsPage {
DetailsPage.allCases.first { $0.index == page.index } ?? .info
}
var video: Video? {
player.currentVideo
}
2021-08-23 00:43:33 +05:30
2022-06-25 05:09:29 +05:30
var body: some View {
2022-08-08 23:32:46 +05:30
if #available(iOS 15, macOS 12, *) {
Self._printChanges()
}
return VStack(alignment: .leading, spacing: 0) {
2022-06-25 05:09:29 +05:30
ControlsBar(
2022-06-30 13:35:32 +05:30
fullScreen: $fullScreen,
2022-06-25 05:09:29 +05:30
presentingControls: false,
backgroundEnabled: false,
borderTop: false,
2022-06-25 22:03:35 +05:30
detailsTogglePlayer: false,
2022-06-30 13:35:32 +05:30
detailsToggleFullScreen: true
2022-06-25 05:09:29 +05:30
)
2021-11-03 04:32:02 +05:30
2022-06-25 05:09:29 +05:30
HStack(spacing: 4) {
2022-09-04 20:58:30 +05:30
pageButton(
"Info".localized(),
"info.circle", .info, !video.isNil
)
2022-11-10 22:41:28 +05:30
if let video, !video.isLocal {
pageButton(
"Chapters".localized(),
"bookmark", .chapters, !video.chapters.isEmpty && !video.isLocal
)
pageButton(
"Comments".localized(),
"text.bubble", .comments, !video.isLocal
) { comments.load() }
pageButton(
"Related".localized(),
"rectangle.stack.fill", .related, !video.isLocal
)
}
2022-09-04 20:58:30 +05:30
pageButton(
"Queue".localized(),
"list.number", .queue, !player.queue.isEmpty
)
}
2022-06-25 05:09:29 +05:30
.onChange(of: player.currentItem) { _ in
page.update(.moveToFirst)
}
2022-06-25 05:09:29 +05:30
.padding(.horizontal)
.padding(.vertical, 6)
Pager(page: page, data: DetailsPage.allCases, id: \.self) {
2022-06-26 17:01:14 +05:30
if !player.currentItem.isNil || page.index == DetailsPage.queue.index {
detailsByPage($0)
2022-11-10 22:41:28 +05:30
#if os(iOS)
.padding(.bottom, SafeArea.insets.bottom)
#else
.padding(.bottom, 6)
#endif
2022-06-26 17:01:14 +05:30
} else {
VStack {}
}
}
2022-08-25 22:39:55 +05:30
.onPageWillChange { pageIndex in
if pageIndex == DetailsPage.comments.index {
comments.load()
}
}
2022-08-25 22:39:55 +05:30
.frame(maxWidth: detailsSize.width)
}
.onAppear {
page.update(.moveToFirst)
2021-10-21 03:51:50 +05:30
guard video != nil, accounts.app.supportsSubscriptions else {
subscribed = false
return
}
}
.onChange(of: sidebarQueue) { queue in
2021-11-04 04:30:17 +05:30
if queue {
if currentPage == .related || currentPage == .queue {
page.update(.moveToFirst)
}
2021-11-04 04:30:17 +05:30
} else if video.isNil {
page.update(.moveToLast)
2021-11-04 04:30:17 +05:30
}
}
2022-01-05 23:21:19 +05:30
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.overlay(GeometryReader { proxy in
Color.clear
.onAppear {
detailsSize = proxy.size
}
.onChange(of: proxy.size) { newSize in
detailsSize = newSize
}
})
}
var publishedDateSection: some View {
Group {
2022-09-28 19:57:01 +05:30
if let video {
HStack(spacing: 4) {
if let published = video.publishedDate {
Text(published)
2022-07-11 23:14:25 +05:30
} else {
Text("1 century ago").redacted(reason: .placeholder)
}
2021-08-23 00:43:33 +05:30
}
}
}
}
2021-08-23 00:43:33 +05:30
2021-10-27 04:29:59 +05:30
private var contentItem: ContentItem {
2022-11-10 22:41:28 +05:30
ContentItem(video: player.currentVideo)
}
2021-08-26 03:42:59 +05:30
2022-06-25 05:09:29 +05:30
func pageButton(
_ label: String,
_ symbolName: String,
_ destination: DetailsPage,
_ active: Bool = true,
pageChangeAction: (() -> Void)? = nil
) -> some View {
Button(action: {
page.update(.new(index: destination.index))
pageChangeAction?()
}) {
HStack {
Spacer()
2022-06-25 05:09:29 +05:30
HStack(spacing: 4) {
Image(systemName: symbolName)
2022-06-25 05:09:29 +05:30
if playerDetailsPageButtonLabelStyle.text && player.playerSize.width > 450 {
Text(label)
}
}
2022-06-25 05:09:29 +05:30
.frame(minHeight: 15)
.lineLimit(1)
.padding(.vertical, 4)
.foregroundColor(currentPage == destination ? .white : (active ? Color.accentColor : .gray))
2022-06-25 05:09:29 +05:30
Spacer()
}
.contentShape(Rectangle())
}
.background(currentPage == destination ? (active ? Color.accentColor : .gray) : .clear)
.buttonStyle(.plain)
.font(.system(size: 10).bold())
.overlay(
RoundedRectangle(cornerRadius: 2)
2022-06-26 16:42:32 +05:30
.stroke(active ? Color.accentColor : .gray, lineWidth: 1.2)
2022-06-25 05:09:29 +05:30
.foregroundColor(.clear)
)
.frame(maxWidth: .infinity)
}
2022-06-25 05:09:29 +05:30
@ViewBuilder func detailsByPage(_ page: DetailsPage) -> some View {
Group {
switch page {
case .info:
ScrollView(.vertical, showsIndicators: false) {
detailsPage
}
2022-06-25 05:09:29 +05:30
case .chapters:
ChaptersView()
2022-08-08 23:32:46 +05:30
case .comments:
CommentsView(embedInScrollView: true)
2022-06-25 05:09:29 +05:30
case .related:
RelatedView()
2022-08-08 23:32:46 +05:30
case .queue:
PlayerQueueView(sidebarQueue: sidebarQueue, fullScreen: $fullScreen)
}
}
2022-06-25 05:09:29 +05:30
.contentShape(Rectangle())
}
@State private var detailsSize = CGSize.zero
var detailsPage: some View {
2022-08-25 22:39:55 +05:30
VStack(alignment: .leading, spacing: 0) {
2022-09-28 19:57:01 +05:30
if let video {
2022-11-10 22:41:28 +05:30
if !video.isLocal {
VStack(spacing: 6) {
videoProperties
2021-08-23 00:43:33 +05:30
2022-11-10 22:41:28 +05:30
Divider()
}
.padding(.bottom, 6)
2022-08-25 22:39:55 +05:30
}
VStack(alignment: .leading, spacing: 10) {
if !player.videoBeingOpened.isNil && (video.description.isNil || video.description!.isEmpty) {
VStack(alignment: .leading, spacing: 0) {
ForEach(1 ... Int.random(in: 2 ... 5), id: \.self) { _ in
Text(String(repeating: Video.fixture.description ?? "", count: Int.random(in: 1 ... 4)))
}
2021-11-28 20:07:55 +05:30
}
2022-08-25 22:39:55 +05:30
.redacted(reason: .placeholder)
} else if video.description != nil, !video.description!.isEmpty {
VideoDescription(video: video, detailsSize: detailsSize)
#if os(iOS)
2022-09-02 04:35:31 +05:30
.padding(.bottom, player.playingFullScreen ? 10 : SafeArea.insets.bottom)
2022-08-25 22:39:55 +05:30
#endif
2022-11-10 22:41:28 +05:30
} else if !video.isLocal {
2022-08-25 22:39:55 +05:30
Text("No description")
.foregroundColor(.secondary)
2021-08-23 00:43:33 +05:30
}
}
2022-11-10 22:41:28 +05:30
VStack(spacing: 4) {
Group {
if player.activeBackend == .mpv, player.mpvBackend.videoFormat != "unknown" {
videoDetailGroupHeading("Video")
videoDetailRow("Format", value: player.mpvBackend.videoFormat)
videoDetailRow("Codec", value: player.mpvBackend.videoCodec)
videoDetailRow("Hardware Decoder", value: player.mpvBackend.hwDecoder)
videoDetailRow("Driver", value: player.mpvBackend.currentVo)
videoDetailRow("Size", value: player.formattedSize)
videoDetailRow("FPS", value: player.mpvBackend.formattedOutputFps)
} else if player.activeBackend == .appleAVPlayer, let width = player.backend.videoWidth, width > 0 {
videoDetailGroupHeading("Video")
videoDetailRow("Size", value: player.formattedSize)
}
}
if player.activeBackend == .mpv, player.mpvBackend.audioFormat != "unknown" {
Group {
videoDetailGroupHeading("Audio")
videoDetailRow("Format", value: player.mpvBackend.audioFormat)
videoDetailRow("Codec", value: player.mpvBackend.audioCodec)
videoDetailRow("Driver", value: player.mpvBackend.currentAo)
videoDetailRow("Channels", value: player.mpvBackend.audioChannels)
videoDetailRow("Sample Rate", value: player.mpvBackend.audioSampleRate)
}
}
if video.localStream != nil || video.localStreamFileExtension != nil {
videoDetailGroupHeading("File")
}
if let fileExtension = video.localStreamFileExtension {
videoDetailRow("File Extension", value: fileExtension)
}
if let url = video.localStream?.localURL, video.localStreamIsRemoteURL {
videoDetailRow("URL", value: url.absoluteString)
}
}
.padding(.bottom, 6)
}
2021-08-23 00:43:33 +05:30
}
2022-08-25 22:39:55 +05:30
.padding(.horizontal)
2021-08-26 03:42:59 +05:30
}
2022-11-10 22:41:28 +05:30
@ViewBuilder func videoDetailGroupHeading(_ heading: String) -> some View {
Text(heading.uppercased())
.font(.footnote)
.foregroundColor(.secondary)
}
@ViewBuilder func videoDetailRow(_ detail: String, value: String) -> some View {
HStack {
Text(detail)
.foregroundColor(.secondary)
Spacer()
let value = Text(value)
if #available(iOS 15.0, macOS 12.0, *) {
value
#if !os(tvOS)
.textSelection(.enabled)
#endif
} else {
value
}
}
.font(.caption)
}
2022-07-05 22:51:13 +05:30
@ViewBuilder var videoProperties: some View {
2022-07-11 23:14:25 +05:30
HStack(spacing: 2) {
publishedDateSection
2022-07-05 22:51:13 +05:30
2022-07-11 23:14:25 +05:30
Spacer()
2022-06-25 05:09:29 +05:30
2022-07-11 23:14:25 +05:30
HStack(spacing: 4) {
Image(systemName: "eye")
2022-06-25 05:09:29 +05:30
2022-07-11 23:14:25 +05:30
if let views = video?.viewsCount, player.videoBeingOpened.isNil {
Text(views)
} else {
Text("1,234M").redacted(reason: .placeholder)
2022-06-25 05:09:29 +05:30
}
2022-07-11 23:14:25 +05:30
Image(systemName: "hand.thumbsup")
2022-06-25 05:09:29 +05:30
2022-07-11 23:14:25 +05:30
if let likes = video?.likesCount, player.videoBeingOpened.isNil {
Text(likes)
} else {
Text("1,234M").redacted(reason: .placeholder)
}
2022-07-05 22:51:13 +05:30
2022-07-11 23:14:25 +05:30
if Defaults[.enableReturnYouTubeDislike] {
Image(systemName: "hand.thumbsdown")
2022-07-05 22:51:13 +05:30
2022-07-11 23:14:25 +05:30
if let dislikes = video?.dislikesCount, player.videoBeingOpened.isNil {
Text(dislikes)
} else {
Text("1,234M").redacted(reason: .placeholder)
2022-07-05 22:51:13 +05:30
}
2022-06-25 05:09:29 +05:30
}
}
}
.font(.system(size: 12))
.foregroundColor(.secondary)
}
2021-08-26 03:42:59 +05:30
func videoDetail(label: String, value: String, symbol: String) -> some View {
VStack(spacing: 4) {
HStack(spacing: 2) {
Image(systemName: symbol)
Text(label.uppercased())
}
.font(.system(size: 9))
.opacity(0.6)
Text(value)
}
.frame(maxWidth: 100)
2021-08-23 00:43:33 +05:30
}
}
2021-08-26 03:42:59 +05:30
struct VideoDetails_Previews: PreviewProvider {
static var previews: some View {
2022-06-25 22:03:35 +05:30
VideoDetails(sidebarQueue: true, fullScreen: .constant(false))
2021-09-29 17:15:00 +05:30
.injectFixtureEnvironmentObjects()
2021-08-23 00:43:33 +05:30
}
}