From 359990b36a4a7fc3e98406ca54e3a474032574f9 Mon Sep 17 00:00:00 2001 From: MrDulfin Date: Tue, 25 Mar 2025 17:30:36 -0400 Subject: [PATCH] Updated Next Song Grabber to grab songs from playlists when playing songs from playlists --- dmp-core/src/music_controller/controller.rs | 5 + .../src/music_controller/library_command.rs | 9 ++ .../src/music_controller/player_command.rs | 108 ++++++++++++------ .../src/music_controller/player_monitor.rs | 2 - dmp-core/src/music_storage/playlist.rs | 18 +++ 5 files changed, 105 insertions(+), 37 deletions(-) diff --git a/dmp-core/src/music_controller/controller.rs b/dmp-core/src/music_controller/controller.rs index c227f7e..8c94836 100644 --- a/dmp-core/src/music_controller/controller.rs +++ b/dmp-core/src/music_controller/controller.rs @@ -109,6 +109,10 @@ pub enum LibraryCommand { AllSongs, GetLibrary, ExternalPlaylist(Uuid), + PlaylistSong{ + list_uuid: Uuid, + item_uuid: Uuid + }, Playlist(Uuid), ImportM3UPlayList(PathBuf), Save, @@ -122,6 +126,7 @@ pub enum LibraryResponse { AllSongs(Vec), Library(MusicLibrary), ExternalPlaylist(ExternalPlaylist), + PlaylistSong(Song, usize), Playlist(Playlist), ImportM3UPlayList(Uuid, String), Playlists(Vec<(Uuid, String)>), diff --git a/dmp-core/src/music_controller/library_command.rs b/dmp-core/src/music_controller/library_command.rs index 22031f4..463693b 100644 --- a/dmp-core/src/music_controller/library_command.rs +++ b/dmp-core/src/music_controller/library_command.rs @@ -89,6 +89,15 @@ impl Controller { .await .unwrap(); } + LibraryCommand::PlaylistSong { list_uuid, item_uuid } => { + let playlist= library.playlists.query_uuid(&list_uuid).unwrap(); + let Some((uuid, index)) = playlist.query_uuid(&item_uuid) else { todo!() }; + let Some((song, _)) = library.query_uuid(uuid) else { todo!() }; + res_rx + .send(LibraryResponse::PlaylistSong(song.clone(), index)) + .await + .unwrap(); + } _ => { todo!() } diff --git a/dmp-core/src/music_controller/player_command.rs b/dmp-core/src/music_controller/player_command.rs index 4ea18b9..eccf81f 100644 --- a/dmp-core/src/music_controller/player_command.rs +++ b/dmp-core/src/music_controller/player_command.rs @@ -91,42 +91,80 @@ impl Controller { panic!("This is temporary, handle queueItemTypes at some point") }; - let (command, tx) = - LibraryCommandInput::command(LibraryCommand::AllSongs); - // Append next song in library - lib_mail.send(command).await.unwrap(); - let LibraryResponse::AllSongs(songs) = tx.recv().await.unwrap() - else { - continue; - }; - - let (command, tx) = LibraryCommandInput::command( - LibraryCommand::Song(np_song.song.uuid), - ); - lib_mail.send(command).await.unwrap(); - let LibraryResponse::Song(_, i) = tx.recv().await.unwrap() else { - unreachable!() - }; - if let Some(song) = songs.get(i + 49) { - let (command, tx) = - QueueCommandInput::command(QueueCommand::Append( - QueueItem::from_item_type(QueueItemType::Single( - QueueSong { - song: song.clone(), - location: np_song.location, - }, - )), - false, - )); - queue_mail.send(command).await.unwrap(); - let QueueResponse::Empty(Ok(())) = tx.recv().await.unwrap() - else { - unreachable!() - }; - } else { - println!("Library Empty"); + match np_song.location { + PlayerLocation::Library => { + let (command, tx) = + LibraryCommandInput::command(LibraryCommand::AllSongs); + // Append next song in library + lib_mail.send(command).await.unwrap(); + let LibraryResponse::AllSongs(songs) = tx.recv().await.unwrap() + else { + continue; + }; + let (command, tx) = LibraryCommandInput::command( + LibraryCommand::Song(np_song.song.uuid), + ); + lib_mail.send(command).await.unwrap(); + let LibraryResponse::Song(_, i) = tx.recv().await.unwrap() else { + unreachable!() + }; + if let Some(song) = songs.get(i + 49) { + let (command, tx) = + QueueCommandInput::command(QueueCommand::Append( + QueueItem::from_item_type(QueueItemType::Single( + QueueSong { + song: song.clone(), + location: np_song.location, + }, + )), + false, + )); + queue_mail.send(command).await.unwrap(); + let QueueResponse::Empty(Ok(())) = tx.recv().await.unwrap() + else { + unreachable!() + }; + } else { + println!("Library Empty"); + } + } + PlayerLocation::Playlist(uuid) => { + let (command, tx) = LibraryCommandInput::command( + LibraryCommand::ExternalPlaylist(uuid), + ); + lib_mail.send(command).await.unwrap(); + let LibraryResponse::ExternalPlaylist(playlist) = tx.recv().await.unwrap() else { + unreachable!() + }; + let (command, tx) = LibraryCommandInput::command( + LibraryCommand::PlaylistSong { list_uuid: playlist.uuid, item_uuid: np_song.song.uuid } + ); + lib_mail.send(command).await.unwrap(); + let LibraryResponse::PlaylistSong(_, i) = tx.recv().await.unwrap() else { + unreachable!() + }; + if let Some(song) = playlist.tracks.get(i + 49) { + let (command, tx) = + QueueCommandInput::command(QueueCommand::Append( + QueueItem::from_item_type(QueueItemType::Single( + QueueSong { + song: song.clone(), + location: np_song.location, + }, + )), + false, + )); + queue_mail.send(command).await.unwrap(); + let QueueResponse::Empty(Ok(())) = tx.recv().await.unwrap() + else { + unreachable!() + }; + } else { + println!("Playlist Empty"); + } + } + _ => todo!() } - res_rx .send(PlayerResponse::NowPlaying(Ok(np_song.song.clone()))) .await diff --git a/dmp-core/src/music_controller/player_monitor.rs b/dmp-core/src/music_controller/player_monitor.rs index 9156784..90af789 100644 --- a/dmp-core/src/music_controller/player_monitor.rs +++ b/dmp-core/src/music_controller/player_monitor.rs @@ -55,7 +55,6 @@ impl Controller { notify_connections .send(ConnectionsNotification::AboutToFinish) .unwrap(); - println!("About to Finish"); } }) }); @@ -83,7 +82,6 @@ impl Controller { notify_connections .send(ConnectionsNotification::EOS) .unwrap(); - println!("End of song"); } }); }); diff --git a/dmp-core/src/music_storage/playlist.rs b/dmp-core/src/music_storage/playlist.rs index ff6b5f7..4155d7b 100644 --- a/dmp-core/src/music_storage/playlist.rs +++ b/dmp-core/src/music_storage/playlist.rs @@ -335,6 +335,24 @@ impl Playlist { (songs, invalid_uuids) } + + pub fn query_uuid(&self, uuid: &Uuid) -> Option<(&Uuid, usize)> { + let result = self + .tracks + .par_iter() + .enumerate() + .try_for_each(|(i, track)| { + if uuid == track { + return std::ops::ControlFlow::Break((track, i)); + } + std::ops::ControlFlow::Continue(()) + }); + + match result { + std::ops::ControlFlow::Break(song) => Some(song), + std::ops::ControlFlow::Continue(_) => None, + } + } } impl Default for Playlist {