yattee/Shared/Player/PlayerQueueView.swift

174 lines
5.2 KiB
Swift
Raw Normal View History

2021-11-05 19:57:22 +00:00
import Defaults
import Foundation
import SwiftUI
struct PlayerQueueView: View {
var sidebarQueue: Bool
2022-06-25 16:33:35 +00:00
@Binding var fullScreen: Bool
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch>
@EnvironmentObject<AccountsModel> private var accounts
2022-06-26 11:31:26 +00:00
@EnvironmentObject<NavigationModel> private var navigation
@EnvironmentObject<PlaylistsModel> private var playlists
@EnvironmentObject<PlayerModel> private var player
2021-11-05 19:57:22 +00:00
@Default(.saveHistory) private var saveHistory
@Default(.showHistoryInPlayer) private var showHistoryInPlayer
2021-11-05 19:57:22 +00:00
var body: some View {
List {
Group {
2022-07-11 17:44:49 +00:00
if player.playbackMode == .related {
autoplaying
}
playingNext
2021-11-03 23:00:17 +00:00
if sidebarQueue {
related
}
if saveHistory, showHistoryInPlayer {
2021-11-05 19:57:22 +00:00
playedPreviously
}
}
2022-07-10 17:51:46 +00:00
.listRowBackground(Color.clear)
2021-11-02 23:02:02 +00:00
#if !os(iOS)
2022-07-10 17:51:46 +00:00
.padding(.vertical, 5)
.listRowInsets(EdgeInsets())
#endif
}
#if os(macOS)
2021-11-08 16:29:35 +00:00
.listStyle(.inset)
#elseif os(iOS)
2021-11-08 16:29:35 +00:00
.listStyle(.grouped)
2022-07-10 17:51:46 +00:00
.backport
.scrollContentBackground(false)
#else
2021-11-08 16:29:35 +00:00
.listStyle(.plain)
#endif
}
2022-07-11 17:44:49 +00:00
@ViewBuilder var autoplaying: some View {
Section(header: autoplayingHeader) {
if let item = player.autoplayItem {
PlayerQueueRow(item: item, autoplay: true)
} else {
Group {
if player.currentItem.isNil {
Text("Not Playing")
} else {
Text("Finding something to play...")
}
}
.foregroundColor(.secondary)
}
}
}
var autoplayingHeader: some View {
HStack {
Text("Autoplaying Next")
Spacer()
Button {
player.setRelatedAutoplayItem()
} label: {
Label("Find Other", systemImage: "arrow.triangle.2.circlepath.circle")
.labelStyle(.iconOnly)
}
.disabled(player.currentItem.isNil)
.buttonStyle(.plain)
}
}
var playingNext: some View {
2022-07-11 17:44:49 +00:00
Section(header: Text("Queue")) {
if player.queue.isEmpty {
2022-07-11 17:44:49 +00:00
Text("Queue is empty")
.foregroundColor(.secondary)
}
ForEach(player.queue) { item in
2022-06-25 16:33:35 +00:00
PlayerQueueRow(item: item, fullScreen: $fullScreen)
.onAppear {
player.loadQueueVideoDetails(item)
}
.contextMenu {
removeButton(item)
removeAllButton()
2022-08-14 17:01:22 +00:00
if let video = item.video {
VideoContextMenuView(video: video)
}
}
}
}
}
private var visibleWatches: [Watch] {
watches.filter { $0.videoID != player.currentVideo?.videoID }
}
var playedPreviously: some View {
Group {
if !visibleWatches.isEmpty {
2022-07-11 17:44:49 +00:00
Section(header: Text("History")) {
ForEach(visibleWatches, id: \.videoID) { watch in
PlayerQueueRow(
item: PlayerQueueItem.from(watch, video: player.historyVideo(watch.videoID)),
history: true,
2022-06-25 16:33:35 +00:00
fullScreen: $fullScreen
)
.onAppear {
player.loadHistoryVideoDetails(watch.videoID)
}
.contextMenu {
2022-08-14 17:01:22 +00:00
VideoContextMenuView(video: watch.video)
}
}
}
}
}
}
2021-11-02 23:02:02 +00:00
private var related: some View {
Group {
if !player.currentVideo.isNil, !player.currentVideo!.related.isEmpty {
Section(header: Text("Related")) {
ForEach(player.currentVideo!.related) { video in
2022-06-25 16:33:35 +00:00
PlayerQueueRow(item: PlayerQueueItem(video), fullScreen: $fullScreen)
2021-11-02 23:02:02 +00:00
.contextMenu {
2022-08-14 17:01:22 +00:00
VideoContextMenuView(video: video)
2021-11-02 23:02:02 +00:00
}
}
}
}
}
}
private func removeButton(_ item: PlayerQueueItem) -> some View {
Button {
player.remove(item)
} label: {
2022-08-14 17:01:22 +00:00
Label("Remove from the queue", systemImage: "trash")
}
}
private func removeAllButton() -> some View {
Button {
player.removeQueueItems()
} label: {
2022-08-14 17:01:22 +00:00
Label("Clear the queue", systemImage: "trash.fill")
}
2021-11-28 14:37:55 +00:00
}
}
struct PlayerQueueView_Previews: PreviewProvider {
static var previews: some View {
VStack {
2022-06-25 16:33:35 +00:00
PlayerQueueView(sidebarQueue: true, fullScreen: .constant(true))
}
.injectFixtureEnvironmentObjects()
}
}