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
|
|
|
|
@State private var showingDeleteConfirmation = false
|
|
|
|
|
2021-08-16 22:46:18 +00:00
|
|
|
@FocusState private var focused: Bool
|
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@EnvironmentObject<InvidiousAPI> private var api
|
|
|
|
@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 {
|
|
|
|
#if os(macOS) || os(iOS)
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text(editing ? "Edit Playlist" : "Create Playlist")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Cancel") {
|
|
|
|
dismiss()
|
|
|
|
}.keyboardShortcut(.cancelAction)
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
.padding(.horizontal)
|
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
Form {
|
|
|
|
TextField("Name", text: $name, onCommit: validate)
|
|
|
|
.frame(maxWidth: 450)
|
|
|
|
.padding(.leading, 10)
|
2021-08-16 22:46:18 +00:00
|
|
|
.focused($focused)
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
visibilityFormItem
|
|
|
|
.pickerStyle(.segmented)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
HStack {
|
|
|
|
if editing {
|
|
|
|
deletePlaylistButton
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
2021-08-19 22:38:31 +00:00
|
|
|
Button("Save", action: submitForm)
|
|
|
|
.disabled(!valid)
|
2021-08-16 15:52:42 +00:00
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
.frame(minHeight: 35)
|
|
|
|
.padding(.horizontal)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
.onChange(of: name) { _ in validate() }
|
2021-08-16 22:46:18 +00:00
|
|
|
.onAppear(perform: initializeForm)
|
2021-09-25 08:18:22 +00:00
|
|
|
#if os(iOS)
|
|
|
|
.padding(.vertical)
|
|
|
|
#else
|
2021-08-16 15:52:42 +00:00
|
|
|
.frame(width: 400, height: 150)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#else
|
2021-09-28 18:06:05 +00:00
|
|
|
VStack {
|
|
|
|
Group {
|
|
|
|
header
|
|
|
|
form
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
.frame(maxWidth: 1000)
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
guard editing else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.name = self.playlist.title
|
|
|
|
self.visibility = self.playlist.visibility
|
|
|
|
|
|
|
|
validate()
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
|
|
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
|
|
|
|
.background(.thickMaterial)
|
2021-08-16 15:52:42 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:06:05 +00:00
|
|
|
var header: some View {
|
|
|
|
HStack(alignment: .center) {
|
|
|
|
Text(editing ? "Edit Playlist" : "Create Playlist")
|
|
|
|
.font(.title2.bold())
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
#if !os(tvOS)
|
2021-09-29 12:36:52 +00:00
|
|
|
Button("Cancel") {
|
|
|
|
dismiss()
|
|
|
|
}
|
2021-09-28 18:06:05 +00:00
|
|
|
.keyboardShortcut(.cancelAction)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
|
|
|
var form: some View {
|
|
|
|
VStack(alignment: .trailing) {
|
|
|
|
VStack {
|
|
|
|
Text("Name")
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
|
|
|
TextField("Playlist Name", text: $name, onCommit: validate)
|
|
|
|
}
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Visibility")
|
|
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
|
2021-09-29 14:29:17 +00:00
|
|
|
visibilityFormItem
|
2021-09-28 18:06:05 +00:00
|
|
|
}
|
|
|
|
.padding(.top, 10)
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
|
|
|
|
Button("Save", action: submitForm).disabled(!valid)
|
|
|
|
}
|
|
|
|
.padding(.top, 40)
|
|
|
|
|
|
|
|
if editing {
|
|
|
|
Divider()
|
|
|
|
HStack {
|
|
|
|
Text("Delete playlist")
|
|
|
|
.font(.title2.bold())
|
|
|
|
Spacer()
|
|
|
|
deletePlaylistButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}
|
|
|
|
|
2021-08-16 22:46:18 +00:00
|
|
|
func initializeForm() {
|
|
|
|
focused = true
|
2021-08-22 19:13:33 +00:00
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
guard editing else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name = playlist.title
|
|
|
|
visibility = playlist.visibility
|
|
|
|
|
|
|
|
validate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func validate() {
|
|
|
|
valid = !name.isEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
func submitForm() {
|
|
|
|
guard valid else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let body = ["title": name, "privacy": visibility.rawValue]
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
resource?.request(editing ? .patch : .post, json: body).onSuccess { response in
|
2021-08-16 15:52:42 +00:00
|
|
|
if let modifiedPlaylist: Playlist = response.typedContent() {
|
|
|
|
playlist = modifiedPlaylist
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
playlists.load(force: true)
|
2021-08-29 21:36:18 +00:00
|
|
|
|
2021-08-16 15:52:42 +00:00
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
var resource: Resource? {
|
2021-09-25 08:18:22 +00:00
|
|
|
editing ? api.playlist(playlist.id) : api.playlists
|
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
|
|
|
|
Text(visibility.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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 {
|
|
|
|
Button("Delete", role: .destructive) {
|
|
|
|
showingDeleteConfirmation = true
|
|
|
|
}.alert(isPresented: $showingDeleteConfirmation) {
|
|
|
|
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()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
#if os(macOS)
|
|
|
|
.foregroundColor(.red)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func deletePlaylistAndDismiss() {
|
2021-10-20 22:21:50 +00:00
|
|
|
api.playlist(playlist.id)?.request(.delete).onSuccess { _ in
|
2021-08-16 15:52:42 +00:00
|
|
|
playlist = nil
|
2021-09-25 08:18:22 +00:00
|
|
|
playlists.load(force: true)
|
2021-08-16 15:52:42 +00:00
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PlaylistFormView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
PlaylistFormView(playlist: .constant(Playlist.fixture))
|
|
|
|
}
|
|
|
|
}
|