Skip to content

Commit

Permalink
Add album to queue action (#429)
Browse files Browse the repository at this point in the history
- support adding album to queue
- add `AlbumAction::AddToQueue`

Resolves #407

---------

Co-authored-by: Thang Pham <[email protected]>
  • Loading branch information
nikosavola and aome510 authored Apr 26, 2024
1 parent 1926026 commit ba133a8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
10 changes: 10 additions & 0 deletions spotify_player/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ impl Client {
self.add_track_to_playlist(state, playlist_id, track_id)
.await?;
}
ClientRequest::AddAlbumToQueue(album_id) => {
let album_context = self.album_context(album_id).await?;

if let Context::Album { album: _, tracks } = album_context {
for track in tracks {
self.add_item_to_queue(PlayableId::Track(track.id), None)
.await?;
}
}
}
ClientRequest::DeleteTrackFromPlaylist(playlist_id, track_id) => {
self.delete_track_from_playlist(state, playlist_id, track_id)
.await?;
Expand Down
1 change: 1 addition & 0 deletions spotify_player/src/client/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum ClientRequest {
},
Search(String),
AddTrackToQueue(TrackId<'static>),
AddAlbumToQueue(AlbumId<'static>),
AddTrackToPlaylist(PlaylistId<'static>, TrackId<'static>),
DeleteTrackFromPlaylist(PlaylistId<'static>, TrackId<'static>),
ReorderPlaylistItems {
Expand Down
2 changes: 2 additions & 0 deletions spotify_player/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub enum AlbumAction {
AddToLibrary,
DeleteFromLibrary,
CopyAlbumLink,
AddToQueue,
}

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -149,6 +150,7 @@ pub fn construct_album_actions(album: &Album, data: &DataReadGuard) -> Vec<Album
AlbumAction::GoToAlbumRadio,
AlbumAction::ShowActionsOnArtist,
AlbumAction::CopyAlbumLink,
AlbumAction::AddToQueue,
];
if data.user_data.saved_albums.iter().any(|a| a.id == album.id) {
actions.push(AlbumAction::DeleteFromLibrary);
Expand Down
6 changes: 4 additions & 2 deletions spotify_player/src/event/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn handle_key_sequence_for_page(

match page_type {
PageType::Search => anyhow::bail!("page search type should already be handled!"),
PageType::Library => handle_command_for_library_page(command, ui, state),
PageType::Library => handle_command_for_library_page(command, client_pub, ui, state),
PageType::Context => handle_command_for_context_page(command, client_pub, ui, state),
PageType::Browse => handle_command_for_browse_page(command, client_pub, ui, state),
#[cfg(feature = "lyric-finder")]
Expand All @@ -37,6 +37,7 @@ pub fn handle_key_sequence_for_page(

fn handle_command_for_library_page(
command: Command,
client_pub: &flume::Sender<ClientRequest>,
ui: &mut UIStateGuard,
state: &SharedState,
) -> Result<bool> {
Expand All @@ -63,6 +64,7 @@ fn handle_command_for_library_page(
ui.search_filtered_items(&data.user_data.saved_albums),
&data,
ui,
client_pub,
),
LibraryFocusState::FollowedArtists => {
window::handle_command_for_artist_list_window(
Expand Down Expand Up @@ -140,7 +142,7 @@ fn handle_key_sequence_for_search_page(
let albums = search_results
.map(|s| s.albums.iter().collect())
.unwrap_or_default();
window::handle_command_for_album_list_window(command, albums, &data, ui)
window::handle_command_for_album_list_window(command, albums, &data, ui, client_pub)
}
SearchFocusState::Playlists => {
let playlists = search_results
Expand Down
4 changes: 4 additions & 0 deletions spotify_player/src/event/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ fn handle_item_action(
client_pub.send(ClientRequest::DeleteFromLibrary(ItemId::Album(album.id)))?;
ui.popup = None;
}
AlbumAction::AddToQueue => {
client_pub.send(ClientRequest::AddAlbumToQueue(album.id))?;
ui.popup = None;
}
},
ActionListItem::Artist(artist, actions) => match actions[n] {
ArtistAction::Follow => {
Expand Down
5 changes: 5 additions & 0 deletions spotify_player/src/event/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub fn handle_command_for_focused_context_window(
ui.search_filtered_items(albums),
&data,
ui,
client_pub,
),
ArtistFocusState::RelatedArtists => handle_command_for_artist_list_window(
command,
Expand Down Expand Up @@ -327,6 +328,7 @@ pub fn handle_command_for_album_list_window(
albums: Vec<&Album>,
data: &DataReadGuard,
ui: &mut UIStateGuard,
client_pub: &flume::Sender<ClientRequest>,
) -> Result<bool> {
let id = ui.current_page_mut().selected().unwrap_or_default();
if id >= albums.len() {
Expand All @@ -352,6 +354,9 @@ pub fn handle_command_for_album_list_window(
new_list_state(),
));
}
Command::AddSelectedItemToQueue => {
client_pub.send(ClientRequest::AddAlbumToQueue(albums[id].id.clone()))?;
}
_ => return Ok(false),
}
Ok(true)
Expand Down

0 comments on commit ba133a8

Please sign in to comment.