1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Playlists/AddToPlaylistView.swift

177 lines
5.0 KiB
Swift
Raw Normal View History

import Defaults
import Siesta
import SwiftUI
struct AddToPlaylistView: View {
let video: Video
2021-10-25 03:06:24 +05:30
@State private var selectedPlaylistID: Playlist.ID = ""
@Environment(\.colorScheme) private var colorScheme
2021-11-28 20:07:55 +05:30
@Environment(\.presentationMode) private var presentationMode
2021-12-18 01:23:05 +05:30
@ObservedObject private var model = PlaylistsModel.shared
var body: some View {
Group {
VStack {
2022-08-15 17:57:33 +05:30
header
if model.isEmpty {
emptyPlaylistsMessage
} else {
form
}
2022-08-15 17:57:33 +05:30
Spacer()
footer
}
.frame(maxWidth: 1000, maxHeight: height)
}
.onAppear {
model.load {
if let playlist = model.find(id: Defaults[.lastUsedPlaylistID]) ?? model.all.first {
2021-10-25 03:06:24 +05:30
selectedPlaylistID = playlist.id
}
}
}
#if os(macOS)
2021-11-08 21:59:35 +05:30
.frame(width: 500, height: 270)
.padding(.vertical)
#elseif os(tvOS)
2021-11-08 21:59:35 +05:30
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.background(scheme: colorScheme))
#else
2021-11-08 21:59:35 +05:30
.padding(.vertical)
#endif
}
var height: Double {
#if os(tvOS)
600
#else
2023-05-08 01:15:18 +05:30
.infinity
#endif
}
private var emptyPlaylistsMessage: some View {
VStack(spacing: 20) {
Text("You have no Playlists")
.font(.title2.bold())
Text("Open \"Playlists\" tab to create new one")
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
}
}
private var header: some View {
HStack {
Text("Add to Playlist")
.font(.title2.bold())
Spacer()
#if !os(tvOS)
Button("Cancel") {
2021-11-28 20:07:55 +05:30
presentationMode.wrappedValue.dismiss()
}
.keyboardShortcut(.cancelAction)
#endif
}
.padding(.horizontal)
}
private var form: some View {
VStack(alignment: formAlignment) {
VideoBanner(video: video)
.padding(.vertical, 40)
VStack(alignment: formAlignment) {
#if os(tvOS)
selectPlaylistButton
#else
2022-12-16 03:21:06 +05:30
HStack {
Text("Playlist")
Menu {
Picker("Playlist", selection: $selectedPlaylistID) {
2022-12-17 00:04:12 +05:30
ForEach(model.editable) { playlist in
2022-12-16 03:21:06 +05:30
Text(playlist.title).tag(playlist.id)
}
}
} label: {
Text(selectedPlaylist?.title ?? "Select Playlist")
}
2022-12-16 03:21:06 +05:30
.transaction { t in t.animation = nil }
.frame(maxWidth: 500, alignment: .trailing)
#if os(macOS)
.labelsHidden()
#endif
}
#endif
}
}
.padding(.horizontal)
}
private var formAlignment: HorizontalAlignment {
#if os(tvOS)
.trailing
#else
2023-05-08 01:15:18 +05:30
.center
#endif
}
private var footer: some View {
HStack {
Spacer()
Button("Add to Playlist", action: addToPlaylist)
.disabled(selectedPlaylist.isNil)
.padding(.top, 30)
#if !os(tvOS)
.keyboardShortcut(.defaultAction)
#endif
}
.padding(.horizontal)
}
2021-11-11 03:35:59 +05:30
#if os(tvOS)
private var selectPlaylistButton: some View {
Button(selectedPlaylist?.title ?? "Select playlist") {
guard selectedPlaylist != nil else {
return // swiftlint:disable:this implicit_return
}
2021-11-11 03:35:59 +05:30
2022-12-17 00:04:12 +05:30
selectedPlaylistID = model.editable.next(after: selectedPlaylist!)!.id
}
2021-11-11 03:35:59 +05:30
.contextMenu {
2022-12-17 00:04:12 +05:30
ForEach(model.editable) { playlist in
2021-11-11 03:35:59 +05:30
Button(playlist.title) {
selectedPlaylistID = playlist.id
}
}
2021-09-29 18:06:52 +05:30
2021-11-11 03:35:59 +05:30
Button("Cancel", role: .cancel) {}
}
}
2021-11-11 03:35:59 +05:30
#endif
private func addToPlaylist() {
2022-12-17 00:04:12 +05:30
guard let id = selectedPlaylist?.id else { return }
model.addVideo(playlistID: id, videoID: video.videoID)
presentationMode.wrappedValue.dismiss()
}
2021-10-25 03:06:24 +05:30
private var selectedPlaylist: Playlist? {
2021-10-25 03:06:24 +05:30
model.find(id: selectedPlaylistID) ?? model.all.first
}
}
struct AddToPlaylistView_Previews: PreviewProvider {
static var previews: some View {
AddToPlaylistView(video: Video.fixture)
2022-12-16 03:21:06 +05:30
.onAppear {
PlaylistsModel.shared.playlists = [.fixture]
}
}
}