yattee/tvOS/NowPlayingView.swift

170 lines
6.6 KiB
Swift
Raw Normal View History

import CoreMedia
2021-11-05 19:57:22 +00:00
import Defaults
import SwiftUI
struct NowPlayingView: View {
2021-11-02 23:02:02 +00:00
enum ViewSection: CaseIterable {
2022-11-11 13:54:12 +00:00
case nowPlaying, playingNext, related, comments, chapters
2021-11-02 23:02:02 +00:00
}
2022-11-11 13:54:12 +00:00
var sections = [ViewSection.nowPlaying, .playingNext, .related]
2021-10-22 15:00:09 +00:00
var inInfoViewController = false
2021-12-04 19:35:41 +00:00
@State private var repliesID: Comment.ID?
2024-08-24 12:21:52 +00:00
@State private var availableWidth = 0.0
2021-12-04 19:35:41 +00:00
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@ObservedObject private var comments = CommentsModel.shared
@ObservedObject private var player = PlayerModel.shared
2021-11-05 19:57:22 +00:00
@Default(.saveHistory) private var saveHistory
var body: some View {
2021-10-22 15:00:09 +00:00
if inInfoViewController {
content
.background(.thinMaterial)
.mask(RoundedRectangle(cornerRadius: 24))
} else {
content
}
}
var content: some View {
List {
Group {
2021-11-02 23:02:02 +00:00
if sections.contains(.nowPlaying), let item = player.currentItem {
Section(header: Text("Now Playing")) {
Button {
player.show()
} label: {
VideoBanner(video: item.video)
}
2021-12-02 20:19:10 +00:00
.contextMenu {
Button("Close Video") {
player.closeCurrentItem()
}
Button("Cancel", role: .cancel) {}
}
}
.onPlayPauseCommand(perform: player.togglePlay)
}
2021-11-02 23:02:02 +00:00
if sections.contains(.playingNext) {
Section(header: Text("Playing Next")) {
if player.queue.isEmpty {
2022-09-26 15:30:59 +00:00
Text("Queue is empty")
2021-11-02 23:02:02 +00:00
.padding([.vertical, .leading], 40)
.foregroundColor(.secondary)
}
2021-11-02 23:02:02 +00:00
ForEach(player.queue) { item in
Button {
player.advanceToItem(item)
player.show()
2021-11-02 23:02:02 +00:00
} label: {
VideoBanner(video: item.video)
}
.contextMenu {
Button("Remove", role: .destructive) {
2021-11-02 23:02:02 +00:00
player.remove(item)
}
Button("Remove All", role: .destructive) {
player.removeQueueItems()
}
2021-11-02 23:02:02 +00:00
}
}
2021-11-02 23:02:02 +00:00
}
}
2022-08-20 21:05:40 +00:00
if sections.contains(.related), let video = player.currentVideo, !video.related.isEmpty {
Section(header: Text("Related")) {
ForEach(video.related) { video in
2021-11-02 23:02:02 +00:00
Button {
2022-08-20 21:05:40 +00:00
player.play(video)
2021-11-02 23:02:02 +00:00
} label: {
VideoBanner(video: video)
}
.contextMenu {
2022-08-20 21:05:40 +00:00
VideoContextMenuView(video: video)
}
}
}
}
2021-10-13 22:05:19 +00:00
2021-12-04 19:35:41 +00:00
if sections.contains(.comments) {
2022-01-05 16:12:32 +00:00
if comments.disabled {
2022-09-28 15:45:05 +00:00
NoCommentsView(text: "Comments are disabled".localized(), systemImage: "xmark.circle.fill")
2022-01-05 16:12:32 +00:00
} else if comments.loaded && comments.all.isEmpty {
2022-09-28 15:45:05 +00:00
NoCommentsView(text: "No comments".localized(), systemImage: "0.circle.fill")
2022-01-05 16:12:32 +00:00
} else if !comments.loaded {
VStack {
PlaceholderProgressView()
2021-12-17 19:46:49 +00:00
.onAppear {
2022-11-13 17:52:15 +00:00
comments.loadIfNeeded()
2021-12-17 19:46:49 +00:00
}
}
} else {
Section {
ForEach(comments.all) { comment in
2024-08-24 12:21:52 +00:00
CommentView(comment: comment, repliesID: $repliesID, availableWidth: availableWidth)
2021-12-17 19:46:49 +00:00
}
2022-01-05 16:12:32 +00:00
if comments.nextPageAvailable {
Text("Scroll to load more...")
.foregroundColor(.secondary)
.padding(.leading)
.onAppear {
comments.loadNextPage()
}
}
2021-12-04 19:35:41 +00:00
}
2024-08-24 12:21:52 +00:00
.background(GeometryReader { geometry in
Color.clear
.onAppear {
self.availableWidth = Double(geometry.size.width)
}
})
2021-12-04 19:35:41 +00:00
}
}
2022-08-20 21:05:40 +00:00
if sections.contains(.chapters) {
if let video = player.currentVideo {
if video.chapters.isEmpty {
2022-09-28 15:45:05 +00:00
NoCommentsView(text: "No chapters information available".localized(), systemImage: "xmark.circle.fill")
2022-08-20 21:05:40 +00:00
} else {
Section(header: Text("Chapters")) {
ForEach(video.chapters) { chapter in
2023-11-28 19:05:04 +00:00
ChapterViewTVOS(chapter: chapter)
2023-04-22 18:06:30 +00:00
.padding(.horizontal, 40)
2022-08-20 21:05:40 +00:00
}
}
}
}
}
}
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
.padding(.vertical, 20)
}
.padding(.horizontal, inInfoViewController ? 40 : 0)
.listStyle(.grouped)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 560, maxHeight: .infinity, alignment: .leading)
}
private var visibleWatches: [Watch] {
watches.filter { $0.videoID != player.currentVideo?.videoID }
}
}
struct NowPlayingView_Previews: PreviewProvider {
static var previews: some View {
2022-08-20 21:05:40 +00:00
NowPlayingView(sections: [.chapters])
.injectFixtureEnvironmentObjects()
2021-10-22 15:00:09 +00:00
NowPlayingView(inInfoViewController: true)
.injectFixtureEnvironmentObjects()
}
}