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

258 lines
7.7 KiB
Swift
Raw Normal View History

import Siesta
import SwiftUI
struct PlaylistFormView: View {
2021-09-25 13:48:22 +05:30
@Binding var playlist: Playlist!
@State private var name = ""
@State private var visibility = Playlist.Visibility.public
@State private var valid = false
2021-12-18 01:23:05 +05:30
@State private var presentingDeleteConfirmation = false
@State private var formError = ""
@State private var presentingErrorAlert = false
@Environment(\.colorScheme) private var colorScheme
2021-11-28 20:07:55 +05:30
@Environment(\.presentationMode) private var presentationMode
2021-11-08 02:21:22 +05:30
@EnvironmentObject<AccountsModel> private var accounts
2021-09-25 13:48:22 +05:30
@EnvironmentObject<PlaylistsModel> private var playlists
2021-08-30 03:06:18 +05:30
var editing: Bool {
playlist != nil
}
var body: some View {
2021-11-28 20:07:55 +05:30
Group {
#if os(macOS) || os(iOS)
VStack(alignment: .leading) {
HStack(alignment: .center) {
Text(editing ? "Edit Playlist" : "Create Playlist")
.font(.title2.bold())
2021-11-28 20:07:55 +05:30
Spacer()
2021-11-28 20:07:55 +05:30
Button("Cancel") {
presentationMode.wrappedValue.dismiss()
}.keyboardShortcut(.cancelAction)
}
.padding(.horizontal)
2021-09-25 13:48:22 +05:30
2021-11-28 20:07:55 +05:30
Form {
TextField("Name", text: $name, onCommit: validate)
.frame(maxWidth: 450)
.padding(.leading, 10)
2021-11-28 20:07:55 +05:30
visibilityFormItem
.pickerStyle(.segmented)
}
2021-11-28 20:07:55 +05:30
#if os(macOS)
.padding(.horizontal)
#endif
2021-11-28 20:07:55 +05:30
HStack {
if editing {
deletePlaylistButton
}
2021-11-28 20:07:55 +05:30
Spacer()
Button("Save", action: submitForm)
.disabled(!valid)
2021-12-18 01:23:05 +05:30
.alert(isPresented: $presentingErrorAlert) {
Alert(
title: Text("Error when accessing playlist"),
message: Text(formError)
)
}
2021-11-28 20:07:55 +05:30
.keyboardShortcut(.defaultAction)
}
.frame(minHeight: 35)
.padding(.horizontal)
}
2021-11-28 20:07:55 +05:30
#if os(iOS)
2021-09-25 13:48:22 +05:30
.padding(.vertical)
2021-11-28 20:07:55 +05:30
#else
.frame(width: 400, height: 150)
2021-11-28 20:07:55 +05:30
#endif
2021-11-28 20:07:55 +05:30
#else
VStack {
Group {
header
form
}
.frame(maxWidth: 1000)
}
2021-11-28 20:07:55 +05:30
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.background(scheme: colorScheme))
2021-11-28 20:07:55 +05:30
#endif
}
.onChange(of: name) { _ in validate() }
.onAppear(perform: initializeForm)
}
2021-11-11 03:35:59 +05:30
#if os(tvOS)
var header: some View {
HStack(alignment: .center) {
Text(editing ? "Edit Playlist" : "Create Playlist")
.font(.title2.bold())
2021-11-11 03:35:59 +05:30
Spacer()
2021-11-11 03:35:59 +05:30
#if !os(tvOS)
Button("Cancel") {
dismiss()
}
.keyboardShortcut(.cancelAction)
#endif
}
2021-11-11 03:35:59 +05:30
.padding(.horizontal)
}
2021-11-11 03:35:59 +05:30
var form: some View {
VStack(alignment: .trailing) {
VStack {
Text("Name")
.frame(maxWidth: .infinity, alignment: .leading)
2021-11-11 03:35:59 +05:30
TextField("Playlist Name", text: $name, onCommit: validate)
}
2021-11-11 03:35:59 +05:30
HStack {
Text("Visibility")
.frame(maxWidth: .infinity, alignment: .leading)
2021-11-11 03:35:59 +05:30
visibilityFormItem
}
.padding(.top, 10)
HStack {
Spacer()
2021-11-11 03:35:59 +05:30
Button("Save", action: submitForm).disabled(!valid)
}
.padding(.top, 40)
if editing {
Divider()
HStack {
Text("Delete playlist")
.font(.title2.bold())
Spacer()
deletePlaylistButton
}
}
}
2021-11-11 03:35:59 +05:30
.padding(.horizontal)
}
2021-11-11 03:35:59 +05:30
#endif
2021-08-17 04:16:18 +05:30
func initializeForm() {
guard editing else {
return
}
2021-11-28 20:07:55 +05:30
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
name = playlist.title
visibility = playlist.visibility
2021-11-28 20:07:55 +05:30
validate()
}
}
func validate() {
valid = !name.isEmpty
}
func submitForm() {
guard valid else {
return
}
let body = ["title": name, "privacy": visibility.rawValue]
2021-12-18 01:23:05 +05:30
resource?
.request(editing ? .patch : .post, json: body)
.onSuccess { response in
if let modifiedPlaylist: Playlist = response.typedContent() {
playlist = modifiedPlaylist
}
2021-12-18 01:23:05 +05:30
playlists.load(force: true)
2021-08-30 03:06:18 +05:30
2021-12-18 01:23:05 +05:30
presentationMode.wrappedValue.dismiss()
}
.onFailure { error in
formError = "(\(error.httpStatusCode ?? -1)) \(error.userMessage)"
presentingErrorAlert = true
}
}
2021-10-21 03:51:50 +05:30
var resource: Resource? {
2021-11-08 02:21:22 +05:30
editing ? accounts.api.playlist(playlist.id) : accounts.api.playlists
}
var visibilityFormItem: some View {
#if os(macOS)
Picker("Visibility", selection: $visibility) {
ForEach(Playlist.Visibility.allCases) { visibility in
2021-11-28 20:07:55 +05:30
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 18:06:52 +05:30
#if os(tvOS)
Button("Cancel", role: .cancel) {}
#endif
}
#endif
}
var deletePlaylistButton: some View {
2021-11-28 20:07:55 +05:30
Button("Delete") {
2021-12-18 01:23:05 +05:30
presentingDeleteConfirmation = true
2021-11-28 20:07:55 +05:30
}
2021-12-18 01:23:05 +05:30
.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 21:59:35 +05:30
.foregroundColor(.red)
}
func deletePlaylistAndDismiss() {
2021-12-18 01:23:05 +05:30
accounts.api.playlist(playlist.id)?
.request(.delete)
.onSuccess { _ in
playlist = nil
playlists.load(force: true)
presentationMode.wrappedValue.dismiss()
}
.onFailure { error in
formError = "(\(error.httpStatusCode ?? -1)) \(error.localizedDescription)"
presentingErrorAlert = true
}
}
}
struct PlaylistFormView_Previews: PreviewProvider {
static var previews: some View {
PlaylistFormView(playlist: .constant(Playlist.fixture))
}
}