1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-14 14:20:32 +05:30
yattee/Shared/Player/PlayerQueueView.swift

157 lines
5.0 KiB
Swift
Raw Normal View History

2021-11-06 01:27:22 +05:30
import Defaults
import Foundation
import SwiftUI
struct PlayerQueueView: View {
2021-11-04 04:30:17 +05:30
@Binding var sidebarQueue: Bool
@Binding var fullScreen: Bool
@EnvironmentObject<PlayerModel> private var player
2021-11-06 01:27:22 +05:30
@Default(.saveHistory) private var saveHistory
var body: some View {
List {
Group {
playingNext
2021-11-04 04:30:17 +05:30
if sidebarQueue {
related
}
2021-11-06 01:27:22 +05:30
if saveHistory {
playedPreviously
}
}
2021-11-03 04:32:02 +05:30
#if !os(iOS)
2021-11-08 21:59:35 +05:30
.padding(.vertical, 5)
.listRowInsets(EdgeInsets())
#endif
}
#if os(macOS)
2021-11-08 21:59:35 +05:30
.listStyle(.inset)
#elseif os(iOS)
2021-11-08 21:59:35 +05:30
.listStyle(.grouped)
#else
2021-11-08 21:59:35 +05:30
.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
2021-11-28 20:07:55 +05:30
let row = PlayerQueueRow(item: item, fullScreen: $fullScreen)
.contextMenu {
removeButton(item, history: false)
removeAllButton(history: false)
}
2021-11-28 20:07:55 +05:30
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
row.swipeActions(edge: .trailing, allowsFullSwipe: true) {
removeButton(item, history: false)
}
2021-11-28 20:07:55 +05:30
} else {
row
}
}
}
}
var playedPreviously: some View {
Group {
if !player.history.isEmpty {
Section(header: Text("Played Previously")) {
ForEach(player.history) { item in
2021-11-28 20:07:55 +05:30
let row = PlayerQueueRow(item: item, history: true, fullScreen: $fullScreen)
.contextMenu {
removeButton(item, history: true)
removeAllButton(history: true)
}
#if os(iOS)
2021-11-28 20:07:55 +05:30
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
row.swipeActions(edge: .trailing, allowsFullSwipe: true) {
removeButton(item, history: true)
}
} else {
row
}
#endif
}
}
}
}
}
2021-11-03 04:32:02 +05:30
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 {
2021-11-28 20:07:55 +05:30
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
return Button(role: .destructive) {
removeButtonAction(item, history: history)
} label: {
Label("Remove", systemImage: "trash")
}
} else {
return Button {
removeButtonAction(item, history: history)
} label: {
Label("Remove", systemImage: "trash")
}
}
}
2021-11-28 20:07:55 +05:30
private func removeButtonAction(_ item: PlayerQueueItem, history: Bool) {
_ = history ? player.removeHistory(item) : player.remove(item)
}
2021-11-03 04:32:02 +05:30
private func removeAllButton(history: Bool) -> some View {
2021-11-28 20:07:55 +05:30
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, *) {
return Button(role: .destructive) {
removeAllButtonAction(history: history)
} label: {
Label("Remove All", systemImage: "trash.fill")
}
} else {
return Button {
removeAllButtonAction(history: history)
} label: {
Label("Remove All", systemImage: "trash.fill")
}
}
}
2021-11-28 20:07:55 +05:30
private func removeAllButtonAction(history: Bool) {
_ = history ? player.removeHistoryItems() : player.removeQueueItems()
}
}
struct PlayerQueueView_Previews: PreviewProvider {
static var previews: some View {
VStack {
2021-11-04 04:30:17 +05:30
PlayerQueueView(sidebarQueue: .constant(true), fullScreen: .constant(true))
}
.injectFixtureEnvironmentObjects()
}
}