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

184 lines
7.4 KiB
Swift
Raw Normal View History

import CoreMedia
2021-11-06 01:27:22 +05:30
import Defaults
import SwiftUI
struct NowPlayingView: View {
2021-11-03 04:32:02 +05:30
enum ViewSection: CaseIterable {
2021-12-05 01:05:41 +05:30
case nowPlaying, playingNext, playedPreviously, related, comments
2021-11-03 04:32:02 +05:30
}
2021-12-05 01:05:41 +05:30
var sections = [ViewSection.nowPlaying, .playingNext, .playedPreviously, .related]
2021-10-22 20:30:09 +05:30
var inInfoViewController = false
2021-12-05 01:05:41 +05:30
@State private var repliesID: Comment.ID?
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
2021-12-05 01:05:41 +05:30
@EnvironmentObject<CommentsModel> private var comments
@EnvironmentObject<PlayerModel> private var player
2021-12-05 01:05:41 +05:30
@EnvironmentObject<RecentsModel> private var recents
2021-11-06 01:27:22 +05:30
@Default(.saveHistory) private var saveHistory
@Default(.showHistoryInPlayer) private var showHistoryInPlayer
2021-11-06 01:27:22 +05:30
var body: some View {
2021-10-22 20:30:09 +05:30
if inInfoViewController {
content
.background(.thinMaterial)
.mask(RoundedRectangle(cornerRadius: 24))
} else {
content
}
}
var content: some View {
List {
Group {
2021-11-03 04:32:02 +05:30
if sections.contains(.nowPlaying), let item = player.currentItem {
Section(header: Text("Now Playing")) {
Button {
player.show()
} label: {
VideoBanner(video: item.video)
}
2021-12-03 01:49:10 +05:30
.contextMenu {
Button("Close Video") {
player.closeCurrentItem()
}
Button("Cancel", role: .cancel) {}
}
}
.onPlayPauseCommand(perform: player.togglePlay)
}
2021-11-03 04:32:02 +05:30
if sections.contains(.playingNext) {
Section(header: Text("Playing Next")) {
if player.queue.isEmpty {
Text("Playback queue is empty")
.padding([.vertical, .leading], 40)
.foregroundColor(.secondary)
}
2021-11-03 04:32:02 +05:30
ForEach(player.queue) { item in
Button {
player.advanceToItem(item)
player.show()
2021-11-03 04:32:02 +05:30
} label: {
VideoBanner(video: item.video)
}
.contextMenu {
Button("Remove", role: .destructive) {
2021-11-03 04:32:02 +05:30
player.remove(item)
}
Button("Remove All", role: .destructive) {
player.removeQueueItems()
}
2021-11-03 04:32:02 +05:30
}
}
2021-11-03 04:32:02 +05:30
}
}
if sections.contains(.related), !player.currentVideo.isNil, !player.currentVideo!.related.isEmpty {
Section(header: inInfoViewController ? AnyView(EmptyView()) : AnyView(Text("Related"))) {
ForEach(player.currentVideo!.related) { video in
Button {
player.playNow(video)
player.show()
2021-11-03 04:32:02 +05:30
} label: {
VideoBanner(video: video)
}
.contextMenu {
Button("Play Next") {
player.playNext(video)
}
Button("Play Last") {
player.enqueueVideo(video)
}
Button("Cancel", role: .cancel) {}
}
}
}
}
2021-10-14 03:35:19 +05:30
if sections.contains(.playedPreviously), saveHistory, showHistoryInPlayer, !visibleWatches.isEmpty {
Section(header: Text("Played Previously")) {
ForEach(visibleWatches, id: \.videoID) { watch in
Button {
player.playHistory(
PlayerQueueItem.from(watch, video: player.historyVideo(watch.videoID))
)
player.show()
} label: {
VideoBanner(
video: player.historyVideo(watch.videoID),
playbackTime: CMTime.secondsInDefaultTimescale(watch.stoppedAt),
videoDuration: watch.videoDuration
)
}
.onAppear {
player.loadHistoryVideoDetails(watch.videoID)
}
.contextMenu {
2021-11-05 23:53:28 +05:30
Button("Remove", role: .destructive) {
player.removeWatch(watch)
}
}
2021-10-14 03:35:19 +05:30
}
}
}
2021-12-05 01:05:41 +05:30
if sections.contains(.comments) {
2022-01-05 21:42:32 +05:30
if comments.disabled {
NoCommentsView(text: "Comments are disabled", systemImage: "xmark.circle.fill")
} else if comments.loaded && comments.all.isEmpty {
NoCommentsView(text: "No comments", systemImage: "0.circle.fill")
} else if !comments.loaded {
2021-12-18 01:16:49 +05:30
VStack(alignment: .center) {
PlaceholderProgressView()
2021-12-18 01:16:49 +05:30
.onAppear {
comments.load()
}
}
} else {
Section {
ForEach(comments.all) { comment in
CommentView(comment: comment, repliesID: $repliesID)
}
2022-01-05 21:42:32 +05:30
if comments.nextPageAvailable {
Text("Scroll to load more...")
.foregroundColor(.secondary)
.padding(.leading)
.onAppear {
comments.loadNextPage()
}
}
2021-12-05 01:05:41 +05:30
}
}
}
}
.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 {
NowPlayingView()
.injectFixtureEnvironmentObjects()
2021-10-22 20:30:09 +05:30
NowPlayingView(inInfoViewController: true)
.injectFixtureEnvironmentObjects()
}
}