2021-09-28 18:06:05 +00:00
|
|
|
import Defaults
|
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct AddToPlaylistView: View {
|
2021-09-28 20:33:12 +00:00
|
|
|
let video: Video
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-10-24 21:36:24 +00:00
|
|
|
@State private var selectedPlaylistID: Playlist.ID = ""
|
|
|
|
|
2021-12-17 19:53:05 +00:00
|
|
|
@State private var error = ""
|
|
|
|
@State private var presentingErrorAlert = false
|
|
|
|
|
2021-12-05 17:09:25 +00:00
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2021-11-28 14:37:55 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2021-12-17 19:53:05 +00:00
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
@EnvironmentObject<NavigationModel> private var navigation
|
2021-10-24 21:36:24 +00:00
|
|
|
@EnvironmentObject<PlaylistsModel> private var model
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
Group {
|
|
|
|
VStack {
|
2022-08-15 12:27:33 +00:00
|
|
|
header
|
|
|
|
Spacer()
|
2021-09-28 18:06:05 +00:00
|
|
|
if model.isEmpty {
|
|
|
|
emptyPlaylistsMessage
|
|
|
|
} else {
|
|
|
|
form
|
|
|
|
}
|
2022-08-15 12:27:33 +00:00
|
|
|
Spacer()
|
|
|
|
footer
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
.frame(maxWidth: 1000, maxHeight: height)
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
model.load {
|
2021-10-25 21:29:06 +00:00
|
|
|
if let playlist = model.find(id: Defaults[.lastUsedPlaylistID]) ?? model.all.first {
|
2021-10-24 21:36:24 +00:00
|
|
|
selectedPlaylistID = playlist.id
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.frame(width: 500, height: 270)
|
|
|
|
.padding(.vertical)
|
2021-09-28 18:06:05 +00:00
|
|
|
#elseif os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
2021-12-05 17:09:25 +00:00
|
|
|
.background(Color.background(scheme: colorScheme))
|
2021-09-28 18:06:05 +00:00
|
|
|
#else
|
2021-11-08 16:29:35 +00:00
|
|
|
.padding(.vertical)
|
2021-09-28 18:06:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var height: Double {
|
|
|
|
#if os(tvOS)
|
|
|
|
600
|
|
|
|
#else
|
2021-11-08 16:29:35 +00:00
|
|
|
.infinity
|
2021-09-28 18:06:05 +00:00
|
|
|
#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(alignment: .center) {
|
|
|
|
Text("Add to Playlist")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
#if !os(tvOS)
|
|
|
|
Button("Cancel") {
|
2021-11-28 14:37:55 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
.keyboardShortcut(.cancelAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var form: some View {
|
|
|
|
VStack(alignment: formAlignment) {
|
2021-10-05 20:20:09 +00:00
|
|
|
VideoBanner(video: video)
|
|
|
|
.padding(.vertical, 40)
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
VStack(alignment: formAlignment) {
|
|
|
|
#if os(tvOS)
|
|
|
|
selectPlaylistButton
|
|
|
|
#else
|
2021-10-24 21:36:24 +00:00
|
|
|
Picker("Playlist", selection: $selectedPlaylistID) {
|
2021-09-28 18:06:05 +00:00
|
|
|
ForEach(model.all) { playlist in
|
|
|
|
Text(playlist.title).tag(playlist.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 500)
|
|
|
|
#if os(iOS)
|
|
|
|
.pickerStyle(.inline)
|
|
|
|
#elseif os(macOS)
|
|
|
|
.labelsHidden()
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
private var formAlignment: HorizontalAlignment {
|
|
|
|
#if os(tvOS)
|
|
|
|
.trailing
|
|
|
|
#else
|
|
|
|
.center
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private var footer: some View {
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button("Add to Playlist", action: addToPlaylist)
|
2022-06-18 12:39:49 +00:00
|
|
|
.disabled(selectedPlaylist.isNil)
|
2021-10-25 21:29:06 +00:00
|
|
|
.padding(.top, 30)
|
2021-09-28 18:06:05 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
private var selectPlaylistButton: some View {
|
|
|
|
Button(selectedPlaylist?.title ?? "Select playlist") {
|
|
|
|
guard selectedPlaylist != nil else {
|
|
|
|
return // swiftlint:disable:this implicit_return
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-11-10 22:05:59 +00:00
|
|
|
|
|
|
|
selectedPlaylistID = model.all.next(after: selectedPlaylist!)!.id
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-11-10 22:05:59 +00:00
|
|
|
.contextMenu {
|
|
|
|
ForEach(model.all) { playlist in
|
|
|
|
Button(playlist.title) {
|
|
|
|
selectedPlaylistID = playlist.id
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 12:36:52 +00:00
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-11-10 22:05:59 +00:00
|
|
|
#endif
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
private func addToPlaylist() {
|
2021-10-25 21:29:06 +00:00
|
|
|
guard let id = selectedPlaylist?.id else {
|
2021-09-28 18:06:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
Defaults[.lastUsedPlaylistID] = id
|
|
|
|
|
2022-06-18 12:39:49 +00:00
|
|
|
model.addVideo(playlistID: id, videoID: video.videoID, navigation: navigation)
|
|
|
|
|
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-10-24 21:36:24 +00:00
|
|
|
|
2021-10-25 21:29:06 +00:00
|
|
|
private var selectedPlaylist: Playlist? {
|
2021-10-24 21:36:24 +00:00
|
|
|
model.find(id: selectedPlaylistID) ?? model.all.first
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AddToPlaylistView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
AddToPlaylistView(video: Video.fixture)
|
2021-09-29 11:45:00 +00:00
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|