Use menu for add to playlists

This commit is contained in:
Arkadiusz Fal 2022-12-16 19:34:12 +01:00
parent 6b30e804c0
commit b621eba236
3 changed files with 34 additions and 13 deletions

View File

@ -21,6 +21,10 @@ final class PlaylistsModel: ObservableObject {
playlists.sorted { $0.title.lowercased() < $1.title.lowercased() } playlists.sorted { $0.title.lowercased() < $1.title.lowercased() }
} }
var editable: [Playlist] {
all.filter(\.editable)
}
var lastUsed: Playlist? { var lastUsed: Playlist? {
find(id: Defaults[.lastUsedPlaylistID]) find(id: Defaults[.lastUsedPlaylistID])
} }
@ -100,6 +104,7 @@ final class PlaylistsModel: ObservableObject {
} }
) { ) {
self.load(force: true) { self.load(force: true) {
Defaults[.lastUsedPlaylistID] = playlistID
self.reloadPlaylists.toggle() self.reloadPlaylists.toggle()
onSuccess() onSuccess()
} }

View File

@ -95,7 +95,7 @@ struct AddToPlaylistView: View {
Text("Playlist") Text("Playlist")
Menu { Menu {
Picker("Playlist", selection: $selectedPlaylistID) { Picker("Playlist", selection: $selectedPlaylistID) {
ForEach(editablePlaylists) { playlist in ForEach(model.editable) { playlist in
Text(playlist.title).tag(playlist.id) Text(playlist.title).tag(playlist.id)
} }
} }
@ -114,10 +114,6 @@ struct AddToPlaylistView: View {
.padding(.horizontal) .padding(.horizontal)
} }
var editablePlaylists: [Playlist] {
model.all.filter(\.editable)
}
private var formAlignment: HorizontalAlignment { private var formAlignment: HorizontalAlignment {
#if os(tvOS) #if os(tvOS)
.trailing .trailing
@ -146,10 +142,10 @@ struct AddToPlaylistView: View {
return // swiftlint:disable:this implicit_return return // swiftlint:disable:this implicit_return
} }
selectedPlaylistID = editablePlaylists.next(after: selectedPlaylist!)!.id selectedPlaylistID = model.editable.next(after: selectedPlaylist!)!.id
} }
.contextMenu { .contextMenu {
ForEach(editablePlaylists) { playlist in ForEach(model.editable) { playlist in
Button(playlist.title) { Button(playlist.title) {
selectedPlaylistID = playlist.id selectedPlaylistID = playlist.id
} }
@ -161,11 +157,7 @@ struct AddToPlaylistView: View {
#endif #endif
private func addToPlaylist() { private func addToPlaylist() {
guard let id = selectedPlaylist?.id else { guard let id = selectedPlaylist?.id else { return }
return
}
Defaults[.lastUsedPlaylistID] = id
model.addVideo(playlistID: id, videoID: video.videoID) model.addVideo(playlistID: id, videoID: video.videoID)

View File

@ -72,7 +72,11 @@ struct VideoContextMenuView: View {
if accounts.app.supportsUserPlaylists, accounts.signedIn, !video.isLocal { if accounts.app.supportsUserPlaylists, accounts.signedIn, !video.isLocal {
Section { Section {
addToPlaylistButton #if os(tvOS)
addToPlaylistButton
#else
addToPlaylistMenu
#endif
addToLastPlaylistButton addToLastPlaylistButton
if let id = navigation.tabSelection?.playlistID ?? playlistID { if let id = navigation.tabSelection?.playlistID ?? playlistID {
@ -309,6 +313,26 @@ struct VideoContextMenuView: View {
} }
} }
#if !os(tvOS)
@ViewBuilder private var addToPlaylistMenu: some View {
if playlists.playlists.isEmpty {
Text("No Playlists")
} else {
Menu {
ForEach(playlists.editable) { playlist in
Button {
playlists.addVideo(playlistID: playlist.id, videoID: video.videoID)
} label: {
Text(playlist.title).tag(playlist.id)
}
}
} label: {
Label("Add to Playlist...", systemImage: "text.badge.plus")
}
}
}
#endif
func removeFromPlaylistButton(playlistID: String) -> some View { func removeFromPlaylistButton(playlistID: String) -> some View {
Button { Button {
playlists.removeVideo(index: video.indexID!, playlistID: playlistID) playlists.removeVideo(index: video.indexID!, playlistID: playlistID)