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

539 lines
19 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
struct VideoDetails: View {
enum Page {
case info, comments, related, queue
}
@Binding var sidebarQueue: Bool
@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-11-28 20:07:55 +05:30
@State private var presentingUnsubscribeAlert = false
@State private var presentingAddToPlaylist = false
2021-10-27 04:29:59 +05:30
@State private var presentingShareSheet = false
2021-11-13 21:15:47 +05:30
@State private var shareURL: URL?
2021-08-26 03:42:59 +05:30
2021-12-05 01:05:41 +05:30
@State private var currentPage = Page.info
2021-11-28 20:07:55 +05:30
@Environment(\.presentationMode) private var presentationMode
2021-10-28 22:44:55 +05:30
@Environment(\.inNavigationView) private var inNavigationView
2021-12-18 01:31:05 +05:30
@Environment(\.navigationStyle) private var navigationStyle
2021-10-21 03:51:50 +05:30
@EnvironmentObject<AccountsModel> private var accounts
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
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.currentVideo
}
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)
2021-11-08 21:59:35 +05:30
.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-12-18 01:31:05 +05:30
if !sidebarQueue ||
(CommentsModel.enabled && CommentsModel.placement == .separate)
{
pagePicker
.padding(.horizontal)
}
}
.contentShape(Rectangle())
.onSwipeGesture(
up: {
withAnimation {
fullScreen = true
}
},
down: {
withAnimation {
if fullScreen {
fullScreen = false
} else {
2022-03-28 00:52:13 +05:30
self.player.hide()
2021-08-26 03:42:59 +05:30
}
}
}
)
2021-08-23 00:43:33 +05:30
switch currentPage {
2021-12-05 01:05:41 +05:30
case .info:
2022-03-20 04:35:09 +05:30
ScrollView(.vertical, showsIndicators: false) {
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-12-05 01:05:41 +05:30
case .comments:
2022-01-05 23:21:19 +05:30
CommentsView(embedInScrollView: true)
2021-12-05 01:05:41 +05:30
.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 == .related || currentPage == .queue {
2021-12-05 01:05:41 +05:30
currentPage = .info
}
2021-11-04 04:30:17 +05:30
} else if video.isNil {
currentPage = .queue
}
}
.edgesIgnoringSafeArea(.horizontal)
2022-01-05 23:21:19 +05:30
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
}
var title: some View {
Group {
if video != nil {
Text(video!.title)
.onAppear {
2021-12-05 01:05:41 +05:30
currentPage = .info
}
2021-12-03 01:49:10 +05:30
.contextMenu {
Button {
player.closeCurrentItem()
if !sidebarQueue {
currentPage = .queue
} else {
currentPage = .info
2021-12-03 01:49:10 +05:30
}
} label: {
Label("Close Video", systemImage: "xmark.circle")
}
.disabled(player.currentItem.isNil)
}
.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) {
2021-12-18 01:31:05 +05:30
HStack(spacing: 10) {
Group {
ZStack(alignment: .bottomTrailing) {
authorAvatar
if subscribed {
Image(systemName: "star.circle.fill")
.background(Color.background)
.clipShape(Circle())
.foregroundColor(.secondary)
}
}
VStack(alignment: .leading) {
Text(video!.channel.name)
.font(.system(size: 14))
.bold()
Group {
if let subscribers = video!.channel.subscriptionsString {
Text("\(subscribers) subscribers")
2021-12-18 01:31:05 +05:30
}
}
.foregroundColor(.secondary)
.font(.caption2)
2021-12-18 01:31:05 +05:30
}
}
}
.contentShape(RoundedRectangle(cornerRadius: 12))
.contextMenu {
if let video = video {
Button(action: {
NavigationModel.openChannel(
video.channel,
player: player,
recents: recents,
navigation: navigation,
navigationStyle: navigationStyle
)
}) {
Label("\(video.channel.name) Channel", systemImage: "rectangle.stack.fill.badge.person.crop")
}
}
}
if accounts.app.supportsSubscriptions, accounts.signedIn {
2021-10-21 03:51:50 +05:30
Spacer()
2021-10-21 03:51:50 +05:30
Section {
if subscribed {
Button("Unsubscribe") {
2021-11-28 20:07:55 +05:30
presentingUnsubscribeAlert = true
2021-10-21 03:51:50 +05:30
}
#if os(iOS)
2021-11-28 20:07:55 +05:30
.backport
2021-11-08 21:59:35 +05:30
.tint(.gray)
2021-10-21 03:51:50 +05:30
#endif
2021-11-28 20:07:55 +05:30
.alert(isPresented: $presentingUnsubscribeAlert) {
Alert(
title: Text(
2021-12-18 01:31:05 +05:30
"Are you sure you want to unsubscribe from \(video!.channel.name)?"
2021-11-28 20:07:55 +05:30
),
primaryButton: .destructive(Text("Unsubscribe")) {
2022-03-26 23:31:38 +05:30
subscriptionToggleButtonDisabled = true
2021-11-28 20:07:55 +05:30
2022-03-26 23:31:38 +05:30
subscriptions.unsubscribe(video!.channel.id) {
withAnimation {
subscriptionToggleButtonDisabled = false
subscribed.toggle()
}
2021-11-28 20:07:55 +05:30
}
},
secondaryButton: .cancel()
)
2021-10-21 03:51:50 +05:30
}
} else {
Button("Subscribe") {
2022-03-26 23:31:38 +05:30
subscriptionToggleButtonDisabled = true
2022-03-26 23:31:38 +05:30
subscriptions.subscribe(video!.channel.id) {
withAnimation {
subscriptionToggleButtonDisabled = false
subscribed.toggle()
}
}
}
2021-11-28 20:07:55 +05:30
.backport
2022-03-26 23:31:38 +05:30
.tint(subscriptionToggleButtonDisabled ? .gray : .blue)
}
2021-08-26 03:42:59 +05:30
}
2022-03-26 23:31:38 +05:30
.disabled(subscriptionToggleButtonDisabled)
2021-10-21 03:51:50 +05:30
.font(.system(size: 13))
.buttonStyle(.borderless)
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 {
2021-12-05 01:05:41 +05:30
Text("Info").tag(Page.info)
if CommentsModel.enabled, CommentsModel.placement == .separate {
2022-01-05 23:21:19 +05:30
Text("Comments").tag(Page.comments)
2021-12-05 01:05:41 +05:30
}
if !sidebarQueue {
Text("Related").tag(Page.related)
}
2021-12-05 01:05:41 +05:30
}
if !sidebarQueue {
Text("Queue").tag(Page.queue)
2021-11-04 04:30:17 +05:30
}
}
2021-11-04 04:30:17 +05:30
.labelsHidden()
.pickerStyle(.segmented)
.onDisappear {
2021-12-05 01:05:41 +05:30
currentPage = .info
}
}
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
2021-11-28 20:07:55 +05:30
if let date = video.publishedAt {
if video.publishedDate != nil {
Text("")
.foregroundColor(.secondary)
.opacity(0.3)
}
2021-11-28 20:07:55 +05:30
Text(formattedPublishedAt(date))
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
2021-11-28 20:07:55 +05:30
func formattedPublishedAt(_ date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none
return dateFormatter.string(from: date)
}
var countsSection: some View {
Group {
if let video = player.currentVideo {
HStack {
2021-10-27 04:29:59 +05:30
ShareButton(
2021-11-11 03:35:59 +05:30
contentItem: contentItem,
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 {
2021-12-05 01:05:41 +05:30
videoDetail(label: "Views", value: views, symbol: "eye")
}
2021-08-26 03:42:59 +05:30
if let likes = video.likesCount {
Divider()
2022-01-05 23:21:19 +05:30
.frame(minHeight: 35)
2021-08-23 00:43:33 +05:30
2021-12-05 01:05:41 +05:30
videoDetail(label: "Likes", value: likes, symbol: "hand.thumbsup")
}
2021-08-26 03:42:59 +05:30
if let dislikes = video.dislikesCount {
Divider()
2022-01-05 23:21:19 +05:30
.frame(minHeight: 35)
2021-08-23 00:43:33 +05:30
2021-12-05 01:05:41 +05:30
videoDetail(label: "Dislikes", value: dislikes, symbol: "hand.thumbsdown")
}
2021-08-26 03:42:59 +05:30
Spacer()
2021-12-27 01:57:46 +05:30
Button {
presentingAddToPlaylist = true
} label: {
Label("Add to Playlist", systemImage: "text.badge.plus")
.labelStyle(.iconOnly)
.help("Add to Playlist...")
}
2021-12-27 01:57:46 +05:30
.buttonStyle(.plain)
.opacity(accounts.app.supportsUserPlaylists ? 1 : 0)
#if os(macOS)
.frame(minWidth: 35, alignment: .trailing)
#endif
2021-08-23 00:43:33 +05:30
}
.frame(maxHeight: 35)
.foregroundColor(.secondary)
2021-08-23 00:43:33 +05:30
}
}
2021-12-01 04:28:11 +05:30
.background(
EmptyView().sheet(isPresented: $presentingAddToPlaylist) {
if let video = video {
AddToPlaylistView(video: video)
}
}
2021-12-01 04:28:11 +05:30
)
2021-10-27 04:29:59 +05:30
#if os(iOS)
2021-12-01 04:28:11 +05:30
.background(
EmptyView().sheet(isPresented: $presentingShareSheet) {
if let shareURL = shareURL {
ShareSheet(activityItems: [shareURL])
}
2021-11-13 21:15:47 +05:30
}
2021-12-01 04:28:11 +05:30
)
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
2021-12-18 01:31:05 +05:30
private var authorAvatar: some View {
Group {
if let video = video, let url = video.channel.thumbnailURL {
WebImage(url: url)
.resizable()
.placeholder {
Rectangle().fill(Color("PlaceholderColor"))
}
2021-12-25 00:51:11 +05:30
.retryOnAppear(true)
2021-12-18 01:31:05 +05:30
.indicator(.activity)
.clipShape(Circle())
.frame(width: 45, height: 45, alignment: .leading)
}
}
}
var detailsPage: some View {
Group {
Group {
if let video = player.currentVideo {
2022-01-05 23:21:19 +05:30
VStack(spacing: 6) {
HStack {
publishedDateSection
Spacer()
}
2021-08-23 00:43:33 +05:30
Divider()
countsSection
2021-08-23 00:43:33 +05:30
2022-01-05 23:21:19 +05:30
Divider()
}
2021-08-23 00:43:33 +05:30
VStack(alignment: .leading, spacing: 10) {
if let description = video.description {
Group {
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
Text(description)
.textSelection(.enabled)
} else {
Text(description)
}
2021-11-28 20:07:55 +05:30
}
.frame(maxWidth: .infinity, alignment: .leading)
.font(.system(size: 14))
.lineSpacing(3)
} else {
Text("No description")
.foregroundColor(.secondary)
2021-11-28 20:07:55 +05:30
}
2021-08-23 00:43:33 +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())
Text(keyword)
.frame(maxWidth: 500)
}
.font(.caption)
.foregroundColor(.white)
.padding(.vertical, 4)
.padding(.horizontal, 8)
2021-12-30 00:10:25 +05:30
.background(Color("KeywordBackgroundColor"))
.mask(RoundedRectangle(cornerRadius: 3))
2021-11-04 04:44:09 +05:30
}
}
.padding(.bottom, 10)
}
}
2021-08-23 00:43:33 +05:30
}
}
if !video.isNil, CommentsModel.placement == .info {
Divider()
#if os(macOS)
.padding(.bottom, 20)
#else
.padding(.vertical, 10)
#endif
}
}
.padding(.horizontal)
2022-01-05 23:21:19 +05:30
LazyVStack {
if !video.isNil, CommentsModel.placement == .info {
CommentsView()
}
2021-08-23 00:43:33 +05:30
}
}
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
}
}