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

359 lines
11 KiB
Swift
Raw Normal View History

2022-12-18 04:38:30 +05:30
import Defaults
import SwiftUI
struct WatchNextView: View {
@ObservedObject private var model = WatchNextViewModel.shared
@ObservedObject private var player = PlayerModel.shared
@Default(.saveHistory) private var saveHistory
@Environment(\.colorScheme) private var colorScheme
var body: some View {
Group {
if model.isPresenting {
#if os(iOS)
NavigationView {
watchNext
.toolbar {
ToolbarItem(placement: .principal) {
watchNextMenu
}
2022-12-19 00:09:03 +05:30
}
}
.navigationViewStyle(.stack)
#else
VStack {
HStack {
hideCloseButton
.labelStyle(.iconOnly)
.frame(maxWidth: .infinity, alignment: .leading)
2022-12-19 00:09:03 +05:30
Spacer()
2022-12-19 00:09:03 +05:30
watchNextMenu
.frame(maxWidth: .infinity)
2022-12-19 00:09:03 +05:30
Spacer()
2022-12-19 00:09:03 +05:30
HStack {
#if os(macOS)
Text("Mode")
.foregroundColor(.secondary)
#endif
2022-12-19 15:18:30 +05:30
playbackModeControl
2022-12-19 15:18:30 +05:30
HStack {
if model.isRestartable {
reopenButton
}
2022-12-19 15:18:30 +05:30
}
2022-12-19 00:09:03 +05:30
}
.frame(maxWidth: .infinity, alignment: .trailing)
2022-12-19 00:09:03 +05:30
}
#if os(macOS)
.padding()
#endif
watchNext
2022-12-18 04:38:30 +05:30
}
#endif
}
2022-12-18 04:38:30 +05:30
}
.transition(.opacity)
.zIndex(0)
2022-12-18 04:38:30 +05:30
#if os(tvOS)
.background(Color.background(scheme: colorScheme))
2022-12-18 04:38:30 +05:30
#else
.background(Color.background)
2022-12-18 04:38:30 +05:30
#endif
}
var watchNext: some View {
ScrollView {
VStack(alignment: .leading) {
if model.isAutoplaying,
2022-12-19 00:09:03 +05:30
let item = model.nextFromTheQueue
2022-12-18 04:38:30 +05:30
{
HStack {
2022-12-19 00:09:03 +05:30
Text("Playing Next in \(Int(model.countdown.rounded()))...")
.font(.headline.monospacedDigit())
2022-12-18 04:38:30 +05:30
Spacer()
Button {
2022-12-19 00:09:03 +05:30
model.keepFromAutoplaying()
2022-12-18 04:38:30 +05:30
} label: {
2022-12-19 00:09:03 +05:30
Label("Cancel", systemImage: "pause.fill")
#if os(iOS)
.imageScale(.large)
.padding([.vertical, .leading])
.font(.headline.bold())
#endif
2022-12-18 04:38:30 +05:30
}
}
2022-12-19 00:09:03 +05:30
#if os(tvOS)
.padding(.top, 10)
#endif
2022-12-18 04:38:30 +05:30
PlayerQueueRow(item: item)
2022-12-19 15:18:30 +05:30
Divider()
.padding(.vertical, 5)
2022-12-18 04:38:30 +05:30
}
2022-12-19 00:09:03 +05:30
2022-12-18 04:38:30 +05:30
moreVideos
2022-12-19 00:09:03 +05:30
.padding(.top, 15)
2022-12-18 04:38:30 +05:30
}
.padding(.horizontal)
}
2022-12-18 17:41:06 +05:30
#if os(iOS)
2022-12-18 04:38:30 +05:30
.navigationBarTitleDisplayMode(.inline)
2022-12-18 17:41:06 +05:30
#endif
2022-12-18 04:38:30 +05:30
#if !os(macOS)
2022-12-19 00:09:03 +05:30
.navigationTitle(model.page.title)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
hideCloseButton
}
ToolbarItem(placement: .primaryAction) {
reopenButton
}
}
#endif
}
2022-12-18 04:38:30 +05:30
2022-12-19 00:09:03 +05:30
var watchNextMenu: some View {
#if os(tvOS)
Button {
model.page = model.page.next()
} label: {
menuLabel
}
#elseif os(macOS)
pagePicker
.modifier(SettingsPickerModifier())
#if os(macOS)
.frame(maxWidth: 150)
#endif
#else
Menu {
pagePicker
2022-12-19 15:18:30 +05:30
playbackModePicker
2022-12-19 00:09:03 +05:30
} label: {
HStack(spacing: 12) {
menuLabel
.foregroundColor(.primary)
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
2022-12-18 04:38:30 +05:30
}
2022-12-19 00:09:03 +05:30
.transaction { t in t.animation = nil }
2022-12-18 04:38:30 +05:30
}
2022-12-19 00:09:03 +05:30
2022-12-18 04:38:30 +05:30
#endif
}
2022-12-19 00:09:03 +05:30
var menuLabel: some View {
HStack {
Image(systemName: model.page.systemImageName)
.imageScale(.small)
2022-12-19 15:18:30 +05:30
Text(model.page == .queue ? queueTitle : model.page.title)
2022-12-19 00:09:03 +05:30
.font(.headline)
}
}
var pagePicker: some View {
Picker("Page", selection: $model.page) {
ForEach(WatchNextViewModel.Page.allCases, id: \.rawValue) { page in
2022-12-19 15:18:30 +05:30
Label(
page == .queue ? queueTitle : page.title,
systemImage: page.systemImageName
)
.tag(page)
2022-12-19 00:09:03 +05:30
}
}
}
2022-12-19 15:18:30 +05:30
var queueTitle: String {
"\(WatchNextViewModel.Page.queue.title)\(player.queue.count)"
}
2022-12-19 00:09:03 +05:30
@ViewBuilder var hideCloseButton: some View {
if model.isHideable {
hideButton
} else {
closeButton
}
}
var hideButton: some View {
Button {
model.hide()
} label: {
2022-12-21 04:51:00 +05:30
Label("Hide", systemImage: "xmark")
2022-12-19 00:09:03 +05:30
}
}
2022-12-18 04:38:30 +05:30
var closeButton: some View {
Button {
2022-12-19 00:09:03 +05:30
model.close()
2022-12-18 04:38:30 +05:30
} label: {
Label("Close", systemImage: "xmark")
}
}
@ViewBuilder var reopenButton: some View {
2022-12-19 00:09:03 +05:30
if model.isRestartable {
2022-12-18 04:38:30 +05:30
Button {
2022-12-19 00:09:03 +05:30
model.restart()
2022-12-18 04:38:30 +05:30
} label: {
2022-12-19 00:09:03 +05:30
Label(model.reason == .userInteracted ? "Back" : "Reopen", systemImage: "arrow.counterclockwise")
2022-12-18 04:38:30 +05:30
}
}
}
var queueForMoreVideos: [ContentItem] {
guard !player.queue.isEmpty else { return [] }
let suffix = player.playbackMode == .queue && model.isAutoplaying && model.canAutoplay ? 1 : 0
return player.queue.suffix(from: suffix).map(\.contentItem)
}
2022-12-18 04:38:30 +05:30
@ViewBuilder var moreVideos: some View {
VStack(spacing: 12) {
2022-12-19 00:09:03 +05:30
switch model.page {
case .queue:
2022-12-19 15:18:30 +05:30
if player.playbackMode == .related, !(model.isAutoplaying && model.canAutoplay) {
autoplaying
Divider()
}
if (model.isAutoplaying && model.canAutoplay && !queueForMoreVideos.isEmpty) ||
(!model.isAutoplaying && !queueForMoreVideos.isEmpty)
{
HStack {
Text("Next in queue")
.font(.headline)
.frame(maxWidth: .infinity, alignment: .leading)
Spacer()
ClearQueueButton()
}
2022-12-19 15:18:30 +05:30
}
2022-12-19 00:09:03 +05:30
if !queueForMoreVideos.isEmpty {
LazyVStack {
ForEach(queueForMoreVideos) { item in
ContentItemView(item: item)
.environment(\.inQueueListing, true)
.environment(\.listingStyle, .list)
}
2022-12-19 00:09:03 +05:30
}
2022-12-19 15:18:30 +05:30
} else {
2022-12-19 00:09:03 +05:30
Label(
model.isAutoplaying ? "Nothing more in the queue" : "Queue is empty",
systemImage: WatchNextViewModel.Page.queue.systemImageName
)
.foregroundColor(.secondary)
}
case .related:
if let item = model.item {
2022-12-18 04:38:30 +05:30
ForEach(item.video.related) { video in
ContentItemView(item: .init(video: video))
.environment(\.listingStyle, .list)
}
2022-12-19 00:09:03 +05:30
} else {
Label("Nothing was played",
systemImage: WatchNextViewModel.Page.related.systemImageName)
.foregroundColor(.secondary)
2022-12-18 04:38:30 +05:30
}
2022-12-19 00:09:03 +05:30
case .history:
if saveHistory {
2022-12-18 04:38:30 +05:30
HistoryView(limit: 15)
}
}
}
}
2022-12-19 15:18:30 +05:30
@ViewBuilder var playbackModeControl: some View {
#if os(tvOS)
Button {
player.playbackMode = player.playbackMode.next()
} label: {
Label(player.playbackMode.description, systemImage: player.playbackMode.systemImage)
2022-12-19 18:05:37 +05:30
.transaction { t in t.animation = nil }
.frame(minWidth: 350)
2022-12-19 15:18:30 +05:30
}
#elseif os(macOS)
playbackModePicker
.modifier(SettingsPickerModifier())
#if os(macOS)
.frame(maxWidth: 150)
#endif
#else
Menu {
playbackModePicker
} label: {
Label(player.playbackMode.description, systemImage: player.playbackMode.systemImage)
}
#endif
}
var playbackModePicker: some View {
Picker("Playback Mode", selection: $model.player.playbackMode) {
ForEach(PlayerModel.PlaybackMode.allCases, id: \.rawValue) { mode in
Label(mode.description, systemImage: mode.systemImage).tag(mode)
}
}
.labelsHidden()
}
@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")
.font(.headline)
Spacer()
Button {
player.setRelatedAutoplayItem()
} label: {
Label("Find Other", systemImage: "arrow.triangle.2.circlepath.circle")
.labelStyle(.iconOnly)
.foregroundColor(.accentColor)
}
.disabled(player.currentItem.isNil)
.buttonStyle(.plain)
}
}
2022-12-18 04:38:30 +05:30
}
2022-12-19 00:09:03 +05:30
struct WatchNextView_Previews: PreviewProvider {
2022-12-18 04:38:30 +05:30
static var previews: some View {
WatchNextView()
.onAppear {
2022-12-19 00:09:03 +05:30
WatchNextViewModel.shared.finishedWatching(.init(.fixture))
2022-12-18 04:38:30 +05:30
}
}
}