yattee/Shared/Playlists/PlaylistFormView.swift

248 lines
7.7 KiB
Swift
Raw Normal View History

import Siesta
import SwiftUI
struct PlaylistFormView: View {
2021-09-25 08:18:22 +00:00
@Binding var playlist: Playlist!
@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
@Environment(\.colorScheme) private var colorScheme
2021-11-28 14:37:55 +00:00
@Environment(\.presentationMode) private var presentationMode
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
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-11-28 14:37:55 +00:00
Spacer()
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)
.disabled(editing && !accounts.app.userPlaylistsAreEditable)
if accounts.app.userPlaylistsHaveVisibility {
visibilityFormItem
.pickerStyle(.segmented)
}
}
2021-11-28 14:37:55 +00:00
#if os(macOS)
.padding(.horizontal)
#endif
2021-11-28 14:37:55 +00:00
HStack {
if editing {
deletePlaylistButton
}
2021-11-28 14:37:55 +00:00
Spacer()
Button("Save", action: submitForm)
.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-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
.frame(width: 400, height: accounts.app.userPlaylistsHaveVisibility ? 150 : 120)
2021-11-28 14:37:55 +00:00
#endif
2021-11-28 14:37:55 +00:00
#else
VStack {
Group {
header
form
}
.frame(maxWidth: 1000)
}
2021-11-28 14:37:55 +00:00
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.background(scheme: colorScheme))
2021-11-28 14:37:55 +00:00
#endif
}
.onChange(of: name) { _ in validate() }
.onAppear(perform: initializeForm)
}
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-11-10 22:05:59 +00:00
Spacer()
2021-11-10 22:05:59 +00:00
#if !os(tvOS)
Button("Cancel") {
dismiss()
}
.keyboardShortcut(.cancelAction)
#endif
}
2021-11-10 22:05:59 +00:00
.padding(.horizontal)
}
2021-11-10 22:05:59 +00:00
var form: some View {
VStack(alignment: .trailing) {
VStack {
Text("Name")
.frame(maxWidth: .infinity, alignment: .leading)
2021-11-10 22:05:59 +00:00
TextField("Playlist Name", text: $name, onCommit: validate)
.disabled(editing && !accounts.app.userPlaylistsAreEditable)
2021-11-10 22:05:59 +00:00
}
2021-11-10 22:05:59 +00:00
HStack {
Text("Visibility")
.frame(maxWidth: .infinity, alignment: .leading)
2021-11-10 22:05:59 +00:00
visibilityFormItem
}
.padding(.top, 10)
HStack {
Spacer()
2021-11-10 22:05:59 +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-11-10 22:05:59 +00:00
.padding(.horizontal)
}
2021-11-10 22:05:59 +00:00
#endif
2021-08-16 22:46:18 +00:00
func initializeForm() {
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-11-28 14:37:55 +00:00
validate()
}
}
func validate() {
valid = !name.isEmpty
}
func submitForm() {
guard valid else {
return
}
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)
presentationMode.wrappedValue.dismiss()
}
}
var visibilityFormItem: some View {
#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)
}
}
#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
}
#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) {
Alert(
title: Text("Are you sure you want to delete playlist?"),
message: Text("Playlist \"\(playlist.title)\" will be deleted.\nIt cannot be undone."),
primaryButton: .destructive(Text("Delete"), action: deletePlaylistAndDismiss),
secondaryButton: .cancel()
)
}
2021-11-08 16:29:35 +00:00
.foregroundColor(.red)
}
func deletePlaylistAndDismiss() {
accounts.api.deletePlaylist(playlist, onFailure: { error in
formError = "(\(error.httpStatusCode ?? -1)) \(error.localizedDescription)"
presentingErrorAlert = true
}) {
playlist = nil
playlists.load(force: true)
presentationMode.wrappedValue.dismiss()
}
}
}
struct PlaylistFormView_Previews: PreviewProvider {
static var previews: some View {
PlaylistFormView(playlist: .constant(Playlist.fixture))
}
}