Updated Next Song Grabber to grab songs from playlists when playing songs from playlists

This commit is contained in:
MrDulfin 2025-03-25 17:30:36 -04:00
parent fce2927986
commit 359990b36a
5 changed files with 105 additions and 37 deletions

View file

@ -109,6 +109,10 @@ pub enum LibraryCommand {
AllSongs, AllSongs,
GetLibrary, GetLibrary,
ExternalPlaylist(Uuid), ExternalPlaylist(Uuid),
PlaylistSong{
list_uuid: Uuid,
item_uuid: Uuid
},
Playlist(Uuid), Playlist(Uuid),
ImportM3UPlayList(PathBuf), ImportM3UPlayList(PathBuf),
Save, Save,
@ -122,6 +126,7 @@ pub enum LibraryResponse {
AllSongs(Vec<Song>), AllSongs(Vec<Song>),
Library(MusicLibrary), Library(MusicLibrary),
ExternalPlaylist(ExternalPlaylist), ExternalPlaylist(ExternalPlaylist),
PlaylistSong(Song, usize),
Playlist(Playlist), Playlist(Playlist),
ImportM3UPlayList(Uuid, String), ImportM3UPlayList(Uuid, String),
Playlists(Vec<(Uuid, String)>), Playlists(Vec<(Uuid, String)>),

View file

@ -89,6 +89,15 @@ impl Controller {
.await .await
.unwrap(); .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!() todo!()
} }

View file

@ -91,6 +91,8 @@ impl Controller {
panic!("This is temporary, handle queueItemTypes at some point") panic!("This is temporary, handle queueItemTypes at some point")
}; };
match np_song.location {
PlayerLocation::Library => {
let (command, tx) = let (command, tx) =
LibraryCommandInput::command(LibraryCommand::AllSongs); LibraryCommandInput::command(LibraryCommand::AllSongs);
// Append next song in library // Append next song in library
@ -99,7 +101,6 @@ impl Controller {
else { else {
continue; continue;
}; };
let (command, tx) = LibraryCommandInput::command( let (command, tx) = LibraryCommandInput::command(
LibraryCommand::Song(np_song.song.uuid), LibraryCommand::Song(np_song.song.uuid),
); );
@ -126,7 +127,44 @@ impl Controller {
} else { } else {
println!("Library Empty"); 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 res_rx
.send(PlayerResponse::NowPlaying(Ok(np_song.song.clone()))) .send(PlayerResponse::NowPlaying(Ok(np_song.song.clone())))
.await .await

View file

@ -55,7 +55,6 @@ impl Controller {
notify_connections notify_connections
.send(ConnectionsNotification::AboutToFinish) .send(ConnectionsNotification::AboutToFinish)
.unwrap(); .unwrap();
println!("About to Finish");
} }
}) })
}); });
@ -83,7 +82,6 @@ impl Controller {
notify_connections notify_connections
.send(ConnectionsNotification::EOS) .send(ConnectionsNotification::EOS)
.unwrap(); .unwrap();
println!("End of song");
} }
}); });
}); });

View file

@ -335,6 +335,24 @@ impl Playlist {
(songs, invalid_uuids) (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 { impl Default for Playlist {