From b621eba236f88227f6a6a54304fcfd661b338ef5 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Fri, 16 Dec 2022 19:34:12 +0100 Subject: [PATCH] Use menu for add to playlists --- Model/PlaylistsModel.swift | 5 +++++ Shared/Playlists/AddToPlaylistView.swift | 16 ++++----------- Shared/Views/VideoContextMenuView.swift | 26 +++++++++++++++++++++++- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Model/PlaylistsModel.swift b/Model/PlaylistsModel.swift index 24027e45..96633a51 100644 --- a/Model/PlaylistsModel.swift +++ b/Model/PlaylistsModel.swift @@ -21,6 +21,10 @@ final class PlaylistsModel: ObservableObject { playlists.sorted { $0.title.lowercased() < $1.title.lowercased() } } + var editable: [Playlist] { + all.filter(\.editable) + } + var lastUsed: Playlist? { find(id: Defaults[.lastUsedPlaylistID]) } @@ -100,6 +104,7 @@ final class PlaylistsModel: ObservableObject { } ) { self.load(force: true) { + Defaults[.lastUsedPlaylistID] = playlistID self.reloadPlaylists.toggle() onSuccess() } diff --git a/Shared/Playlists/AddToPlaylistView.swift b/Shared/Playlists/AddToPlaylistView.swift index 9394a719..d991f432 100644 --- a/Shared/Playlists/AddToPlaylistView.swift +++ b/Shared/Playlists/AddToPlaylistView.swift @@ -95,7 +95,7 @@ struct AddToPlaylistView: View { Text("Playlist") Menu { Picker("Playlist", selection: $selectedPlaylistID) { - ForEach(editablePlaylists) { playlist in + ForEach(model.editable) { playlist in Text(playlist.title).tag(playlist.id) } } @@ -114,10 +114,6 @@ struct AddToPlaylistView: View { .padding(.horizontal) } - var editablePlaylists: [Playlist] { - model.all.filter(\.editable) - } - private var formAlignment: HorizontalAlignment { #if os(tvOS) .trailing @@ -146,10 +142,10 @@ struct AddToPlaylistView: View { return // swiftlint:disable:this implicit_return } - selectedPlaylistID = editablePlaylists.next(after: selectedPlaylist!)!.id + selectedPlaylistID = model.editable.next(after: selectedPlaylist!)!.id } .contextMenu { - ForEach(editablePlaylists) { playlist in + ForEach(model.editable) { playlist in Button(playlist.title) { selectedPlaylistID = playlist.id } @@ -161,11 +157,7 @@ struct AddToPlaylistView: View { #endif private func addToPlaylist() { - guard let id = selectedPlaylist?.id else { - return - } - - Defaults[.lastUsedPlaylistID] = id + guard let id = selectedPlaylist?.id else { return } model.addVideo(playlistID: id, videoID: video.videoID) diff --git a/Shared/Views/VideoContextMenuView.swift b/Shared/Views/VideoContextMenuView.swift index 26c91f61..2fff6963 100644 --- a/Shared/Views/VideoContextMenuView.swift +++ b/Shared/Views/VideoContextMenuView.swift @@ -72,7 +72,11 @@ struct VideoContextMenuView: View { if accounts.app.supportsUserPlaylists, accounts.signedIn, !video.isLocal { Section { - addToPlaylistButton + #if os(tvOS) + addToPlaylistButton + #else + addToPlaylistMenu + #endif addToLastPlaylistButton 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 { Button { playlists.removeVideo(index: video.indexID!, playlistID: playlistID)