2021-12-26 21:14:46 +00:00
|
|
|
import CoreMedia
|
2021-11-05 19:57:22 +00:00
|
|
|
import Defaults
|
2021-10-05 20:20:09 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct NowPlayingView: View {
|
2021-11-02 23:02:02 +00:00
|
|
|
enum ViewSection: CaseIterable {
|
2021-12-04 19:35:41 +00:00
|
|
|
case nowPlaying, playingNext, playedPreviously, related, comments
|
2021-11-02 23:02:02 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
var sections = [ViewSection.nowPlaying, .playingNext, .playedPreviously, .related]
|
2021-10-22 15:00:09 +00:00
|
|
|
var inInfoViewController = false
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
@State private var repliesID: Comment.ID?
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
|
|
|
|
var watches: FetchedResults<Watch>
|
|
|
|
|
2021-12-04 19:35:41 +00:00
|
|
|
@EnvironmentObject<CommentsModel> private var comments
|
2021-10-05 20:20:09 +00:00
|
|
|
@EnvironmentObject<PlayerModel> private var player
|
2021-12-04 19:35:41 +00:00
|
|
|
@EnvironmentObject<RecentsModel> private var recents
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2021-11-05 19:57:22 +00:00
|
|
|
@Default(.saveHistory) private var saveHistory
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
var body: some View {
|
2021-10-22 15:00:09 +00:00
|
|
|
if inInfoViewController {
|
2021-10-05 20:20:09 +00:00
|
|
|
content
|
|
|
|
.background(.thinMaterial)
|
|
|
|
.mask(RoundedRectangle(cornerRadius: 24))
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var content: some View {
|
2021-10-23 10:13:05 +00:00
|
|
|
List {
|
|
|
|
Group {
|
2021-11-02 23:02:02 +00:00
|
|
|
if sections.contains(.nowPlaying), let item = player.currentItem {
|
2021-10-23 10:13:05 +00:00
|
|
|
Section(header: Text("Now Playing")) {
|
2021-10-05 20:20:09 +00:00
|
|
|
Button {
|
2021-12-19 17:17:04 +00:00
|
|
|
player.show()
|
2021-10-05 20:20:09 +00:00
|
|
|
} label: {
|
|
|
|
VideoBanner(video: item.video)
|
|
|
|
}
|
2021-12-02 20:19:10 +00:00
|
|
|
.contextMenu {
|
|
|
|
Button("Close Video") {
|
|
|
|
player.closeCurrentItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
.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 {
|
|
|
|
Text("Playback queue is empty")
|
|
|
|
.padding([.vertical, .leading], 40)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
|
2021-11-02 23:02:02 +00:00
|
|
|
ForEach(player.queue) { item in
|
|
|
|
Button {
|
|
|
|
player.advanceToItem(item)
|
2021-12-19 17:17:04 +00:00
|
|
|
player.show()
|
2021-11-02 23:02:02 +00:00
|
|
|
} label: {
|
|
|
|
VideoBanner(video: item.video)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Delete", role: .destructive) {
|
|
|
|
player.remove(item)
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
}
|
2021-11-02 23:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-12-19 17:17:04 +00:00
|
|
|
player.show()
|
2021-11-02 23:02:02 +00:00
|
|
|
} label: {
|
|
|
|
VideoBanner(video: video)
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
Button("Play Next") {
|
|
|
|
player.playNext(video)
|
|
|
|
}
|
|
|
|
Button("Play Last") {
|
|
|
|
player.enqueueVideo(video)
|
|
|
|
}
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-10-23 10:13:05 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:05:19 +00:00
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
if sections.contains(.playedPreviously), saveHistory, !visibleWatches.isEmpty {
|
2021-10-23 10:13:05 +00:00
|
|
|
Section(header: Text("Played Previously")) {
|
2021-12-26 21:14:46 +00:00
|
|
|
ForEach(visibleWatches, id: \.videoID) { watch in
|
2021-10-23 10:13:05 +00:00
|
|
|
Button {
|
2021-12-26 21:14:46 +00:00
|
|
|
player.playHistory(
|
|
|
|
PlayerQueueItem.from(watch, video: player.historyVideo(watch.videoID))
|
|
|
|
)
|
2021-12-19 17:17:04 +00:00
|
|
|
player.show()
|
2021-10-23 10:13:05 +00:00
|
|
|
} label: {
|
2021-12-26 21:14:46 +00:00
|
|
|
VideoBanner(
|
|
|
|
video: player.historyVideo(watch.videoID),
|
|
|
|
playbackTime: CMTime.secondsInDefaultTimescale(watch.stoppedAt),
|
|
|
|
videoDuration: watch.videoDuration
|
|
|
|
)
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
player.loadHistoryVideoDetails(watch.videoID)
|
2021-10-23 10:13:05 +00:00
|
|
|
}
|
|
|
|
.contextMenu {
|
2021-11-05 18:23:28 +00:00
|
|
|
Button("Remove", role: .destructive) {
|
2021-12-26 21:14:46 +00:00
|
|
|
player.removeWatch(watch)
|
2021-10-23 10:13:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 19:35:41 +00:00
|
|
|
|
|
|
|
if sections.contains(.comments) {
|
2021-12-17 19:46:49 +00:00
|
|
|
if !comments.loaded {
|
|
|
|
VStack(alignment: .center) {
|
2021-12-29 18:39:38 +00:00
|
|
|
PlaceholderProgressView()
|
2021-12-17 19:46:49 +00:00
|
|
|
.onAppear {
|
|
|
|
comments.load()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Section {
|
|
|
|
ForEach(comments.all) { comment in
|
|
|
|
CommentView(comment: comment, repliesID: $repliesID)
|
|
|
|
}
|
2021-12-04 19:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 20))
|
|
|
|
.padding(.vertical, 20)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
2021-10-23 10:13:05 +00:00
|
|
|
.padding(.horizontal, inInfoViewController ? 40 : 0)
|
|
|
|
.listStyle(.grouped)
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 560, maxHeight: .infinity, alignment: .leading)
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
private var visibleWatches: [Watch] {
|
|
|
|
watches.filter { $0.videoID != player.currentVideo?.videoID }
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct NowPlayingView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
NowPlayingView()
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-10-22 15:00:09 +00:00
|
|
|
|
|
|
|
NowPlayingView(inInfoViewController: true)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
}
|