mirror of
https://github.com/yattee/yattee.git
synced 2024-12-22 21:43:41 +00:00
Show errors when handling playlists
This commit is contained in:
parent
923f0c0356
commit
201e91a3cc
@ -52,14 +52,22 @@ final class PlaylistsModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func addVideo(playlistID: Playlist.ID, videoID: Video.ID, onSuccess: @escaping () -> Void = {}) {
|
func addVideo(
|
||||||
|
playlistID: Playlist.ID,
|
||||||
|
videoID: Video.ID,
|
||||||
|
onSuccess: @escaping () -> Void = {},
|
||||||
|
onFailure: @escaping (RequestError) -> Void = { _ in }
|
||||||
|
) {
|
||||||
let resource = accounts.api.playlistVideos(playlistID)
|
let resource = accounts.api.playlistVideos(playlistID)
|
||||||
let body = ["videoId": videoID]
|
let body = ["videoId": videoID]
|
||||||
|
|
||||||
resource?.request(.post, json: body).onSuccess { _ in
|
resource?
|
||||||
self.load(force: true)
|
.request(.post, json: body)
|
||||||
onSuccess()
|
.onSuccess { _ in
|
||||||
}
|
self.load(force: true)
|
||||||
|
onSuccess()
|
||||||
|
}
|
||||||
|
.onFailure(onFailure)
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeVideo(videoIndexID: String, playlistID: Playlist.ID, onSuccess: @escaping () -> Void = {}) {
|
func removeVideo(videoIndexID: String, playlistID: Playlist.ID, onSuccess: @escaping () -> Void = {}) {
|
||||||
|
@ -7,8 +7,12 @@ struct AddToPlaylistView: View {
|
|||||||
|
|
||||||
@State private var selectedPlaylistID: Playlist.ID = ""
|
@State private var selectedPlaylistID: Playlist.ID = ""
|
||||||
|
|
||||||
|
@State private var error = ""
|
||||||
|
@State private var presentingErrorAlert = false
|
||||||
|
|
||||||
@Environment(\.colorScheme) private var colorScheme
|
@Environment(\.colorScheme) private var colorScheme
|
||||||
@Environment(\.presentationMode) private var presentationMode
|
@Environment(\.presentationMode) private var presentationMode
|
||||||
|
|
||||||
@EnvironmentObject<PlaylistsModel> private var model
|
@EnvironmentObject<PlaylistsModel> private var model
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@ -120,6 +124,12 @@ struct AddToPlaylistView: View {
|
|||||||
Button("Add to Playlist", action: addToPlaylist)
|
Button("Add to Playlist", action: addToPlaylist)
|
||||||
.disabled(selectedPlaylist.isNil)
|
.disabled(selectedPlaylist.isNil)
|
||||||
.padding(.top, 30)
|
.padding(.top, 30)
|
||||||
|
.alert(isPresented: $presentingErrorAlert) {
|
||||||
|
Alert(
|
||||||
|
title: Text("Error when accessing playlist"),
|
||||||
|
message: Text(error)
|
||||||
|
)
|
||||||
|
}
|
||||||
#if !os(tvOS)
|
#if !os(tvOS)
|
||||||
.keyboardShortcut(.defaultAction)
|
.keyboardShortcut(.defaultAction)
|
||||||
#endif
|
#endif
|
||||||
@ -155,9 +165,17 @@ struct AddToPlaylistView: View {
|
|||||||
|
|
||||||
Defaults[.lastUsedPlaylistID] = id
|
Defaults[.lastUsedPlaylistID] = id
|
||||||
|
|
||||||
model.addVideo(playlistID: id, videoID: video.videoID) {
|
model.addVideo(
|
||||||
presentationMode.wrappedValue.dismiss()
|
playlistID: id,
|
||||||
}
|
videoID: video.videoID,
|
||||||
|
onSuccess: {
|
||||||
|
presentationMode.wrappedValue.dismiss()
|
||||||
|
},
|
||||||
|
onFailure: { requestError in
|
||||||
|
error = "(\(requestError.httpStatusCode ?? -1)) \(requestError.userMessage)"
|
||||||
|
presentingErrorAlert = true
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var selectedPlaylist: Playlist? {
|
private var selectedPlaylist: Playlist? {
|
||||||
|
@ -8,7 +8,10 @@ struct PlaylistFormView: View {
|
|||||||
@State private var visibility = Playlist.Visibility.public
|
@State private var visibility = Playlist.Visibility.public
|
||||||
|
|
||||||
@State private var valid = false
|
@State private var valid = false
|
||||||
@State private var showingDeleteConfirmation = false
|
@State private var presentingDeleteConfirmation = false
|
||||||
|
|
||||||
|
@State private var formError = ""
|
||||||
|
@State private var presentingErrorAlert = false
|
||||||
|
|
||||||
@Environment(\.colorScheme) private var colorScheme
|
@Environment(\.colorScheme) private var colorScheme
|
||||||
@Environment(\.presentationMode) private var presentationMode
|
@Environment(\.presentationMode) private var presentationMode
|
||||||
@ -57,6 +60,12 @@ struct PlaylistFormView: View {
|
|||||||
|
|
||||||
Button("Save", action: submitForm)
|
Button("Save", action: submitForm)
|
||||||
.disabled(!valid)
|
.disabled(!valid)
|
||||||
|
.alert(isPresented: $presentingErrorAlert) {
|
||||||
|
Alert(
|
||||||
|
title: Text("Error when accessing playlist"),
|
||||||
|
message: Text(formError)
|
||||||
|
)
|
||||||
|
}
|
||||||
.keyboardShortcut(.defaultAction)
|
.keyboardShortcut(.defaultAction)
|
||||||
}
|
}
|
||||||
.frame(minHeight: 35)
|
.frame(minHeight: 35)
|
||||||
@ -165,15 +174,21 @@ struct PlaylistFormView: View {
|
|||||||
|
|
||||||
let body = ["title": name, "privacy": visibility.rawValue]
|
let body = ["title": name, "privacy": visibility.rawValue]
|
||||||
|
|
||||||
resource?.request(editing ? .patch : .post, json: body).onSuccess { response in
|
resource?
|
||||||
if let modifiedPlaylist: Playlist = response.typedContent() {
|
.request(editing ? .patch : .post, json: body)
|
||||||
playlist = modifiedPlaylist
|
.onSuccess { response in
|
||||||
|
if let modifiedPlaylist: Playlist = response.typedContent() {
|
||||||
|
playlist = modifiedPlaylist
|
||||||
|
}
|
||||||
|
|
||||||
|
playlists.load(force: true)
|
||||||
|
|
||||||
|
presentationMode.wrappedValue.dismiss()
|
||||||
|
}
|
||||||
|
.onFailure { error in
|
||||||
|
formError = "(\(error.httpStatusCode ?? -1)) \(error.userMessage)"
|
||||||
|
presentingErrorAlert = true
|
||||||
}
|
}
|
||||||
|
|
||||||
playlists.load(force: true)
|
|
||||||
|
|
||||||
presentationMode.wrappedValue.dismiss()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var resource: Resource? {
|
var resource: Resource? {
|
||||||
@ -207,9 +222,9 @@ struct PlaylistFormView: View {
|
|||||||
|
|
||||||
var deletePlaylistButton: some View {
|
var deletePlaylistButton: some View {
|
||||||
Button("Delete") {
|
Button("Delete") {
|
||||||
showingDeleteConfirmation = true
|
presentingDeleteConfirmation = true
|
||||||
}
|
}
|
||||||
.alert(isPresented: $showingDeleteConfirmation) {
|
.alert(isPresented: $presentingDeleteConfirmation) {
|
||||||
Alert(
|
Alert(
|
||||||
title: Text("Are you sure you want to delete playlist?"),
|
title: Text("Are you sure you want to delete playlist?"),
|
||||||
message: Text("Playlist \"\(playlist.title)\" will be deleted.\nIt cannot be undone."),
|
message: Text("Playlist \"\(playlist.title)\" will be deleted.\nIt cannot be undone."),
|
||||||
@ -221,11 +236,17 @@ struct PlaylistFormView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func deletePlaylistAndDismiss() {
|
func deletePlaylistAndDismiss() {
|
||||||
accounts.api.playlist(playlist.id)?.request(.delete).onSuccess { _ in
|
accounts.api.playlist(playlist.id)?
|
||||||
playlist = nil
|
.request(.delete)
|
||||||
playlists.load(force: true)
|
.onSuccess { _ in
|
||||||
presentationMode.wrappedValue.dismiss()
|
playlist = nil
|
||||||
}
|
playlists.load(force: true)
|
||||||
|
presentationMode.wrappedValue.dismiss()
|
||||||
|
}
|
||||||
|
.onFailure { error in
|
||||||
|
formError = "(\(error.httpStatusCode ?? -1)) \(error.localizedDescription)"
|
||||||
|
presentingErrorAlert = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user