yattee/Shared/Player/PlayerQueueView.swift

135 lines
4.1 KiB
Swift
Raw Normal View History

2021-11-05 19:57:22 +00:00
import Defaults
import Foundation
import SwiftUI
struct PlayerQueueView: View {
2021-11-03 23:00:17 +00:00
@Binding var sidebarQueue: Bool
@Binding var fullScreen: Bool
@EnvironmentObject<PlayerModel> private var player
2021-11-05 19:57:22 +00:00
@Default(.saveHistory) private var saveHistory
var body: some View {
List {
Group {
playingNext
2021-11-03 23:00:17 +00:00
if sidebarQueue {
related
}
2021-11-05 19:57:22 +00:00
if saveHistory {
playedPreviously
}
}
2021-11-02 23:02:02 +00:00
#if !os(iOS)
.padding(.vertical, 5)
.listRowInsets(EdgeInsets())
#endif
}
#if os(macOS)
2021-10-20 22:21:50 +00:00
.listStyle(.inset)
#elseif os(iOS)
.listStyle(.grouped)
#else
.listStyle(.plain)
#endif
}
var playingNext: some View {
Section(header: Text("Playing Next")) {
if player.queue.isEmpty {
Text("Playback queue is empty")
.foregroundColor(.secondary)
}
ForEach(player.queue) { item in
PlayerQueueRow(item: item, fullScreen: $fullScreen)
.contextMenu {
removeButton(item, history: false)
removeAllButton(history: false)
}
#if os(iOS)
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
removeButton(item, history: false)
}
#endif
}
}
}
var playedPreviously: some View {
Group {
if !player.history.isEmpty {
Section(header: Text("Played Previously")) {
ForEach(player.history) { item in
PlayerQueueRow(item: item, history: true, fullScreen: $fullScreen)
.contextMenu {
removeButton(item, history: true)
removeAllButton(history: true)
}
#if os(iOS)
.swipeActions(edge: .trailing, allowsFullSwipe: true) {
removeButton(item, history: true)
}
#endif
}
}
}
}
}
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
PlayerQueueRow(item: PlayerQueueItem(video), fullScreen: $fullScreen)
.contextMenu {
Button("Play Next") {
player.playNext(video)
}
Button("Play Last") {
player.enqueueVideo(video)
}
}
}
}
}
}
}
private func removeButton(_ item: PlayerQueueItem, history: Bool) -> some View {
Button(role: .destructive) {
if history {
player.removeHistory(item)
} else {
player.remove(item)
}
} label: {
Label("Remove", systemImage: "trash")
}
}
2021-11-02 23:02:02 +00:00
private func removeAllButton(history: Bool) -> some View {
Button(role: .destructive) {
if history {
player.removeHistoryItems()
} else {
player.removeQueueItems()
}
} label: {
Label("Remove All", systemImage: "trash.fill")
}
}
}
struct PlayerQueueView_Previews: PreviewProvider {
static var previews: some View {
VStack {
2021-11-03 23:00:17 +00:00
PlayerQueueView(sidebarQueue: .constant(true), fullScreen: .constant(true))
}
.injectFixtureEnvironmentObjects()
}
}