2021-08-16 15:52:42 +00:00
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct PlaylistFormView: View {
|
2021-09-25 08:18:22 +00:00
|
|
|
@Binding var playlist: Playlist!
|
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
@State private var name = ""
|
|
|
|
@State private var visibility = Playlist.Visibility.public
|
|
|
|
|
|
|
|
@State private var valid = false
|
2021-12-17 19:53:05 +00:00
|
|
|
@State private var presentingDeleteConfirmation = false
|
|
|
|
|
|
|
|
@State private var formError = ""
|
|
|
|
@State private var presentingErrorAlert = false
|
2021-08-16 15:52:42 +00:00
|
|
|
|
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-08-16 15:52:42 +00:00
|
|
|
|
2021-11-07 20:51:22 +00:00
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2021-09-25 08:18:22 +00:00
|
|
|
@EnvironmentObject<PlaylistsModel> private var playlists
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
var editing: Bool {
|
|
|
|
playlist != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
Group {
|
|
|
|
#if os(macOS) || os(iOS)
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text(editing ? "Edit Playlist" : "Create Playlist")
|
|
|
|
.font(.title2.bold())
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
Spacer()
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
Button("Cancel") {
|
|
|
|
presentationMode.wrappedValue.dismiss()
|
|
|
|
}.keyboardShortcut(.cancelAction)
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
Form {
|
|
|
|
TextField("Name", text: $name, onCommit: validate)
|
|
|
|
.frame(maxWidth: 450)
|
|
|
|
.padding(.leading, 10)
|
2022-05-21 22:29:51 +00:00
|
|
|
.disabled(editing && !accounts.app.userPlaylistsAreEditable)
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2022-05-21 22:29:51 +00:00
|
|
|
if accounts.app.userPlaylistsHaveVisibility {
|
|
|
|
visibilityFormItem
|
|
|
|
.pickerStyle(.segmented)
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
#if os(macOS)
|
|
|
|
.padding(.horizontal)
|
|
|
|
#endif
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
HStack {
|
|
|
|
if editing {
|
|
|
|
deletePlaylistButton
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Save", action: submitForm)
|
2022-05-21 22:29:51 +00:00
|
|
|
.disabled(!valid || (editing && !accounts.app.userPlaylistsAreEditable))
|
2021-12-17 19:53:05 +00:00
|
|
|
.alert(isPresented: $presentingErrorAlert) {
|
|
|
|
Alert(
|
|
|
|
title: Text("Error when accessing playlist"),
|
|
|
|
message: Text(formError)
|
|
|
|
)
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
}
|
|
|
|
.frame(minHeight: 35)
|
|
|
|
.padding(.horizontal)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
|
|
|
|
#if os(iOS)
|
2021-09-25 08:18:22 +00:00
|
|
|
.padding(.vertical)
|
2021-11-28 14:37:55 +00:00
|
|
|
#else
|
2022-05-21 22:29:51 +00:00
|
|
|
.frame(width: 400, height: accounts.app.userPlaylistsHaveVisibility ? 150 : 120)
|
2021-11-28 14:37:55 +00:00
|
|
|
#endif
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
#else
|
|
|
|
VStack {
|
|
|
|
Group {
|
|
|
|
header
|
|
|
|
form
|
|
|
|
}
|
|
|
|
.frame(maxWidth: 1000)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-11-28 14:37:55 +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-11-28 14:37:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.onChange(of: name) { _ in validate() }
|
|
|
|
.onAppear(perform: initializeForm)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
var header: some View {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text(editing ? "Edit Playlist" : "Create Playlist")
|
|
|
|
.font(.title2.bold())
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
Spacer()
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
#if !os(tvOS)
|
|
|
|
Button("Cancel") {
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
.keyboardShortcut(.cancelAction)
|
|
|
|
#endif
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
2021-11-10 22:05:59 +00:00
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
var form: some View {
|
|
|
|
VStack(alignment: .trailing) {
|
|
|
|
VStack {
|
|
|
|
Text("Name")
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2021-11-10 22:05:59 +00:00
|
|
|
TextField("Playlist Name", text: $name, onCommit: validate)
|
2022-05-21 22:29:51 +00:00
|
|
|
.disabled(editing && !accounts.app.userPlaylistsAreEditable)
|
2021-11-10 22:05:59 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2022-05-22 15:53:12 +00:00
|
|
|
if accounts.app.userPlaylistsHaveVisibility {
|
|
|
|
HStack {
|
|
|
|
Text("Visibility")
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
2021-09-28 18:06:05 +00:00
|
|
|
|
2022-05-22 15:53:12 +00:00
|
|
|
visibilityFormItem
|
|
|
|
}
|
|
|
|
.padding(.top, 10)
|
2021-11-10 22:05:59 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
2021-11-10 22:05:59 +00:00
|
|
|
|
2022-05-21 22:29:51 +00:00
|
|
|
Button("Save", action: submitForm)
|
|
|
|
.disabled(!valid || (editing && !accounts.app.userPlaylistsAreEditable))
|
2021-11-10 22:05:59 +00:00
|
|
|
}
|
|
|
|
.padding(.top, 40)
|
|
|
|
|
|
|
|
if editing {
|
|
|
|
Divider()
|
|
|
|
HStack {
|
|
|
|
Text("Delete playlist")
|
|
|
|
.font(.title2.bold())
|
|
|
|
Spacer()
|
|
|
|
deletePlaylistButton
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-10 22:05:59 +00:00
|
|
|
.padding(.horizontal)
|
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
|
|
|
|
2021-08-16 22:46:18 +00:00
|
|
|
func initializeForm() {
|
2021-08-16 15:52:42 +00:00
|
|
|
guard editing else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
|
|
|
name = playlist.title
|
|
|
|
visibility = playlist.visibility
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
validate()
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func validate() {
|
|
|
|
valid = !name.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
func submitForm() {
|
|
|
|
guard valid else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-21 22:29:51 +00:00
|
|
|
accounts.api.playlistForm(name, visibility.rawValue, playlist: playlist, onFailure: { error in
|
|
|
|
formError = "(\(error.httpStatusCode ?? -1)) \(error.userMessage)"
|
|
|
|
presentingErrorAlert = true
|
|
|
|
}) { modifiedPlaylist in
|
|
|
|
self.playlist = modifiedPlaylist
|
|
|
|
playlists.load(force: true)
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2022-05-21 22:29:51 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
var visibilityFormItem: some View {
|
2021-08-16 15:52:42 +00:00
|
|
|
#if os(macOS)
|
|
|
|
Picker("Visibility", selection: $visibility) {
|
|
|
|
ForEach(Playlist.Visibility.allCases) { visibility in
|
2021-11-28 14:37:55 +00:00
|
|
|
Text(visibility.name).tag(visibility)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Button(self.visibility.name) {
|
|
|
|
self.visibility = self.visibility.next()
|
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(Playlist.Visibility.allCases) { visibility in
|
|
|
|
Button(visibility.name) {
|
|
|
|
self.visibility = visibility
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 12:36:52 +00:00
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
#endif
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var deletePlaylistButton: some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
Button("Delete") {
|
2021-12-17 19:53:05 +00:00
|
|
|
presentingDeleteConfirmation = true
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
2021-12-17 19:53:05 +00:00
|
|
|
.alert(isPresented: $presentingDeleteConfirmation) {
|
2021-08-16 15:52:42 +00:00
|
|
|
Alert(
|
|
|
|
title: Text("Are you sure you want to delete playlist?"),
|
2022-08-16 22:34:48 +00:00
|
|
|
message: Text("Playlist \"\(playlist.title)\" will be deleted.\nIt cannot be reverted."),
|
2021-08-16 15:52:42 +00:00
|
|
|
primaryButton: .destructive(Text("Delete"), action: deletePlaylistAndDismiss),
|
|
|
|
secondaryButton: .cancel()
|
|
|
|
)
|
|
|
|
}
|
2021-11-08 16:29:35 +00:00
|
|
|
.foregroundColor(.red)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deletePlaylistAndDismiss() {
|
2022-05-21 22:29:51 +00:00
|
|
|
accounts.api.deletePlaylist(playlist, onFailure: { error in
|
|
|
|
formError = "(\(error.httpStatusCode ?? -1)) \(error.localizedDescription)"
|
|
|
|
presentingErrorAlert = true
|
|
|
|
}) {
|
|
|
|
playlist = nil
|
|
|
|
playlists.load(force: true)
|
|
|
|
presentationMode.wrappedValue.dismiss()
|
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PlaylistFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
PlaylistFormView(playlist: .constant(Playlist.fixture))
|
|
|
|
}
|
|
|
|
}
|