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

408 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
import SwiftUI
struct VideoDetails: View {
enum Page {
2021-11-03 04:32:02 +05:30
case details, queue, related
}
@Binding var sidebarQueue: Bool
@Binding var fullScreen: Bool
2021-08-26 03:42:59 +05:30
@State private var subscribed = false
@State private var confirmationShown = false
@State private var presentingAddToPlaylist = false
2021-10-27 04:29:59 +05:30
@State private var presentingShareSheet = false
2021-11-03 01:10:49 +05:30
@State private var shareURL = ""
2021-08-26 03:42:59 +05:30
@State private var currentPage = Page.details
@Environment(\.dismiss) private var dismiss
2021-10-28 22:44:55 +05:30
@Environment(\.inNavigationView) private var inNavigationView
2021-10-21 03:51:50 +05:30
@EnvironmentObject<AccountsModel> private var accounts
@EnvironmentObject<PlayerModel> private var player
@EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<SubscriptionsModel> private var subscriptions
2021-11-04 04:44:09 +05:30
@Default(.showKeywords) private var showKeywords
init(
sidebarQueue: Binding<Bool>? = nil,
fullScreen: Binding<Bool>? = nil
) {
_sidebarQueue = sidebarQueue ?? .constant(true)
_fullScreen = fullScreen ?? .constant(false)
}
var video: Video? {
player.currentItem?.video
}
2021-08-23 00:43:33 +05:30
var body: some View {
VStack(alignment: .leading) {
Group {
Group {
HStack(spacing: 0) {
title
2021-08-26 03:42:59 +05:30
toggleFullScreenDetailsButton
}
#if os(macOS)
.padding(.top, 10)
#endif
2021-08-23 00:43:33 +05:30
if !video.isNil {
Divider()
2021-08-26 03:42:59 +05:30
}
subscriptionsSection
2021-11-05 23:53:28 +05:30
.onChange(of: video) { video in
if let video = video {
subscribed = subscriptions.isSubscribing(video.channel.id)
}
}
}
.padding(.horizontal)
2021-11-04 04:30:17 +05:30
if !sidebarQueue {
pagePicker
.padding(.horizontal)
}
}
.contentShape(Rectangle())
.onSwipeGesture(
up: {
withAnimation {
fullScreen = true
}
},
down: {
withAnimation {
if fullScreen {
fullScreen = false
} else {
self.dismiss()
2021-08-26 03:42:59 +05:30
}
}
}
)
2021-08-23 00:43:33 +05:30
switch currentPage {
case .details:
ScrollView(.vertical) {
detailsPage
}
case .queue:
2021-11-04 04:30:17 +05:30
PlayerQueueView(sidebarQueue: $sidebarQueue, fullScreen: $fullScreen)
.edgesIgnoringSafeArea(.horizontal)
2021-11-03 04:32:02 +05:30
case .related:
2021-11-04 04:30:17 +05:30
RelatedView()
.edgesIgnoringSafeArea(.horizontal)
}
}
2021-10-28 22:44:55 +05:30
.padding(.top, inNavigationView && fullScreen ? 10 : 0)
.onAppear {
2021-11-06 01:27:22 +05:30
if video.isNil && !sidebarQueue {
2021-11-04 04:30:17 +05:30
currentPage = .queue
}
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 == .queue {
currentPage = .details
}
2021-11-04 04:30:17 +05:30
} else if video.isNil {
currentPage = .queue
}
}
.edgesIgnoringSafeArea(.horizontal)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
}
var title: some View {
Group {
if video != nil {
Text(video!.title)
.onAppear {
2021-11-04 04:30:17 +05:30
currentPage = .details
}
.font(.title2.bold())
} else {
Text("Not playing")
.foregroundColor(.secondary)
}
Spacer()
}
}
var toggleFullScreenDetailsButton: some View {
Button {
withAnimation {
fullScreen.toggle()
}
} label: {
Label("Resize", systemImage: fullScreen ? "chevron.down" : "chevron.up")
.labelStyle(.iconOnly)
}
.help("Toggle fullscreen details")
.buttonStyle(.plain)
.keyboardShortcut("t")
}
var subscriptionsSection: some View {
Group {
if video != nil {
HStack(alignment: .center) {
HStack(spacing: 4) {
if subscribed {
Image(systemName: "star.circle.fill")
}
VStack(alignment: .leading) {
Text(video!.channel.name)
.font(.system(size: 13))
.bold()
if let subscribers = video!.channel.subscriptionsString {
Text("\(subscribers) subscribers")
.font(.caption2)
}
}
}
.foregroundColor(.secondary)
2021-10-21 03:51:50 +05:30
if accounts.app.supportsSubscriptions {
Spacer()
2021-10-21 03:51:50 +05:30
Section {
if subscribed {
Button("Unsubscribe") {
2021-10-21 03:51:50 +05:30
confirmationShown = true
}
#if os(iOS)
.tint(.gray)
#endif
.confirmationDialog("Are you you want to unsubscribe from \(video!.channel.name)?", isPresented: $confirmationShown) {
Button("Unsubscribe") {
subscriptions.unsubscribe(video!.channel.id)
withAnimation {
subscribed.toggle()
}
}
}
} else {
Button("Subscribe") {
subscriptions.subscribe(video!.channel.id)
withAnimation {
subscribed.toggle()
}
}
2021-10-21 03:51:50 +05:30
.tint(.blue)
}
2021-08-26 03:42:59 +05:30
}
2021-10-21 03:51:50 +05:30
.font(.system(size: 13))
.buttonStyle(.borderless)
.buttonBorderShape(.roundedRectangle)
2021-08-26 03:42:59 +05:30
}
}
}
}
}
2021-08-26 03:42:59 +05:30
var pagePicker: some View {
Picker("Page", selection: $currentPage) {
2021-11-04 04:30:17 +05:30
if !video.isNil {
Text("Details").tag(Page.details)
Text("Related").tag(Page.related)
}
Text("Queue").tag(Page.queue)
}
2021-11-04 04:30:17 +05:30
.labelsHidden()
.pickerStyle(.segmented)
.onDisappear {
currentPage = .details
}
}
var publishedDateSection: some View {
Group {
if let video = player.currentVideo {
HStack(spacing: 4) {
if let published = video.publishedDate {
Text(published)
}
2021-08-23 00:43:33 +05:30
if let publishedAt = video.publishedAt {
if video.publishedDate != nil {
Text("")
.foregroundColor(.secondary)
.opacity(0.3)
}
Text(publishedAt.formatted(date: .abbreviated, time: .omitted))
2021-08-23 00:43:33 +05:30
}
}
.font(.system(size: 12))
.padding(.bottom, -1)
.foregroundColor(.secondary)
2021-08-23 00:43:33 +05:30
}
}
}
2021-08-23 00:43:33 +05:30
var countsSection: some View {
Group {
if let video = player.currentVideo {
HStack {
2021-10-27 04:29:59 +05:30
ShareButton(
contentItem: ContentItem(video: video),
2021-11-03 01:10:49 +05:30
presentingShareSheet: $presentingShareSheet,
shareURL: $shareURL
2021-10-27 04:29:59 +05:30
)
Spacer()
2021-08-26 03:42:59 +05:30
if let views = video.viewsCount {
videoDetail(label: "Views", value: views, symbol: "eye.fill")
}
2021-08-26 03:42:59 +05:30
if let likes = video.likesCount {
Divider()
2021-08-23 00:43:33 +05:30
videoDetail(label: "Likes", value: likes, symbol: "hand.thumbsup.circle.fill")
}
2021-08-26 03:42:59 +05:30
if let dislikes = video.dislikesCount {
Divider()
2021-08-23 00:43:33 +05:30
videoDetail(label: "Dislikes", value: dislikes, symbol: "hand.thumbsdown.circle.fill")
}
2021-08-26 03:42:59 +05:30
Spacer()
2021-10-27 04:29:59 +05:30
if accounts.app.supportsUserPlaylists {
Button {
presentingAddToPlaylist = true
} label: {
Label("Add to Playlist", systemImage: "text.badge.plus")
.labelStyle(.iconOnly)
.help("Add to Playlist...")
}
.buttonStyle(.plain)
}
2021-08-23 00:43:33 +05:30
}
.frame(maxHeight: 35)
.foregroundColor(.secondary)
2021-08-23 00:43:33 +05:30
}
}
.sheet(isPresented: $presentingAddToPlaylist) {
if let video = video {
AddToPlaylistView(video: video)
}
}
2021-10-27 04:29:59 +05:30
#if os(iOS)
.sheet(isPresented: $presentingShareSheet) {
2021-11-03 01:10:49 +05:30
ShareSheet(activityItems: [shareURL])
2021-10-27 04:29:59 +05:30
}
#endif
}
private var contentItem: ContentItem {
ContentItem(video: player.currentVideo!)
}
2021-08-26 03:42:59 +05:30
var detailsPage: some View {
Group {
if let video = player.currentItem?.video {
Group {
2021-10-21 03:51:50 +05:30
HStack {
publishedDateSection
Spacer()
}
2021-08-23 00:43:33 +05:30
Divider()
countsSection
2021-08-23 00:43:33 +05:30
}
Divider()
2021-08-23 00:43:33 +05:30
VStack(alignment: .leading, spacing: 10) {
2021-10-21 03:51:50 +05:30
if let description = video.description {
Text(description)
2021-11-04 05:21:46 +05:30
.frame(maxWidth: .infinity, alignment: .leading)
2021-10-21 03:51:50 +05:30
.font(.caption)
2021-11-04 05:21:46 +05:30
.padding(.bottom, 4)
2021-10-21 03:51:50 +05:30
} else {
Text("No description")
.foregroundColor(.secondary)
}
2021-08-23 00:43:33 +05:30
2021-11-04 04:44:09 +05:30
if showKeywords {
ScrollView(.horizontal, showsIndicators: showScrollIndicators) {
HStack {
ForEach(video.keywords, id: \.self) { keyword in
HStack(alignment: .center, spacing: 0) {
Text("#")
.font(.system(size: 11).bold())
2021-08-23 00:43:33 +05:30
2021-11-04 04:44:09 +05:30
Text(keyword)
.frame(maxWidth: 500)
}
.font(.caption)
.foregroundColor(.white)
.padding(.vertical, 4)
.padding(.horizontal, 8)
.background(Color("VideoDetailLikesSymbolColor"))
.mask(RoundedRectangle(cornerRadius: 3))
}
}
2021-11-04 04:44:09 +05:30
.padding(.bottom, 10)
}
2021-08-23 00:43:33 +05:30
}
}
}
}
.padding(.horizontal)
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
}
var showScrollIndicators: Bool {
#if os(macOS)
false
#else
true
#endif
}
}
2021-08-26 03:42:59 +05:30
struct VideoDetails_Previews: PreviewProvider {
static var previews: some View {
VideoDetails(sidebarQueue: .constant(true))
2021-09-29 17:15:00 +05:30
.injectFixtureEnvironmentObjects()
2021-08-23 00:43:33 +05:30
}
}