diff --git a/dmp-core/src/music_controller/controller_handle.rs b/dmp-core/src/music_controller/controller_handle.rs index 525d107..378c981 100644 --- a/dmp-core/src/music_controller/controller_handle.rs +++ b/dmp-core/src/music_controller/controller_handle.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use async_channel::{Receiver, Sender}; -use rcue::cue::Command; +use discord_presence::models::Command; use uuid::Uuid; use crate::music_storage::{ @@ -192,6 +192,15 @@ impl ControllerHandle { res } + pub async fn enqueue(&self, index: usize) -> Result { + let (command, tx) = PlayerCommandInput::command(PlayerCommand::Enqueue(index)); + self.player_mail_rx.send(command).await.unwrap(); + let PlayerResponse::NowPlaying(song) = tx.recv().await.unwrap() else { + unreachable!() + }; + song + } + pub async fn play(&self) -> Result<(), PlayerError> { let (command, tx) = PlayerCommandInput::command(PlayerCommand::Play); self.player_mail_rx.send(command).await.unwrap(); diff --git a/dmp-core/src/music_controller/player_command.rs b/dmp-core/src/music_controller/player_command.rs index eccf81f..b978fc6 100644 --- a/dmp-core/src/music_controller/player_command.rs +++ b/dmp-core/src/music_controller/player_command.rs @@ -97,7 +97,8 @@ impl Controller { LibraryCommandInput::command(LibraryCommand::AllSongs); // Append next song in library lib_mail.send(command).await.unwrap(); - let LibraryResponse::AllSongs(songs) = tx.recv().await.unwrap() + let LibraryResponse::AllSongs(songs) = + tx.recv().await.unwrap() else { continue; }; @@ -105,22 +106,24 @@ impl Controller { LibraryCommand::Song(np_song.song.uuid), ); lib_mail.send(command).await.unwrap(); - let LibraryResponse::Song(_, i) = tx.recv().await.unwrap() else { + 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 { + 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() + let QueueResponse::Empty(Ok(())) = + tx.recv().await.unwrap() else { unreachable!() }; @@ -133,29 +136,37 @@ impl Controller { LibraryCommand::ExternalPlaylist(uuid), ); lib_mail.send(command).await.unwrap(); - let LibraryResponse::ExternalPlaylist(playlist) = tx.recv().await.unwrap() else { + 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 } + 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 { + 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 { + 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() + let QueueResponse::Empty(Ok(())) = + tx.recv().await.unwrap() else { unreachable!() }; @@ -163,7 +174,7 @@ impl Controller { println!("Playlist Empty"); } } - _ => todo!() + _ => todo!(), } res_rx .send(PlayerResponse::NowPlaying(Ok(np_song.song.clone()))) @@ -234,7 +245,7 @@ impl Controller { queue_mail.send(command).await.unwrap(); match tx.recv().await.unwrap() { QueueResponse::Item(Ok(item)) => { - match item.item { + let mut song = match item.item { QueueItemType::Single(np_song) => { let prism_uri = prismriver::utils::path_to_uri( &np_song @@ -251,17 +262,24 @@ impl Controller { state.now_playing = np_song.song.uuid; _ = state.write_file(); + notify_connections_ - .send(ConnectionsNotification::SongChange(np_song.song)) + .send(ConnectionsNotification::SongChange( + np_song.song.clone(), + )) .unwrap(); + np_song.song } _ => unimplemented!(), - } - res_rx.send(PlayerResponse::Empty(Ok(()))).await.unwrap(); + }; + res_rx + .send(PlayerResponse::NowPlaying(Ok(song))) + .await + .unwrap(); } QueueResponse::Item(Err(e)) => { res_rx - .send(PlayerResponse::Empty(Err(e.into()))) + .send(PlayerResponse::NowPlaying(Err(e.into()))) .await .unwrap(); } diff --git a/src-tauri/src/wrappers.rs b/src-tauri/src/wrappers.rs index 91fe03a..90f159a 100644 --- a/src-tauri/src/wrappers.rs +++ b/src-tauri/src/wrappers.rs @@ -322,11 +322,22 @@ pub async fn clear_queue( #[tauri::command] pub async fn queue_move_to( + app: AppHandle, ctrl_handle: State<'_, ControllerHandle>, index: usize, ) -> Result<(), String> { ctrl_handle .queue_move_to(index) .await - .map_err(|e| e.to_string()) + .map_err(|e| e.to_string())?; + + match ctrl_handle.enqueue(0).await.map_err(|e| e.to_string()) { + Ok(song) => { + app.emit("queue_updated", ()).unwrap(); + app.emit("now_playing_change", _Song::from(&song)).unwrap(); + app.emit("playing", true).unwrap(); + Ok(()) + } + Err(e) => Err(e), + } } diff --git a/src/App.tsx b/src/App.tsx index 7a74fa0..51dd310 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -496,27 +496,27 @@ interface selectedQueueSong { function Queue({ songs, selectedSong, }: QueueProps) { const removeFromQueue = () => { - invoke('remove_from_queue', { index: selectedSong.current.index }).then(() => {}) - } - const playNow = () => { - invoke('play_now', { uuid: selectedSong.current.uuid, location: selectedSong.current.location }).then(() => {}) - } - const playNext = () => invoke('play_next_queue', { uuid: selectedSong.current.uuid, location: selectedSong.current.location }).then(() => {}); - const clearQueue = () => invoke('clear_queue').then(); + invoke('remove_from_queue', { index: selectedSong.current.index }).then(() => {}); + } + const playNow = () => { + invoke('queue_move_to', { index: selectedSong.current.index }).then(() => {}); + } + const playNext = () => invoke('play_next_queue', { uuid: selectedSong.current.uuid, location: selectedSong.current.location }).then(() => {}); + const clearQueue = () => invoke('clear_queue').then(); - async function menuHandler(event: React.MouseEvent) { - event.preventDefault(); + async function menuHandler(event: React.MouseEvent) { + event.preventDefault(); - const menu = await Menu.new({ - items: [ - { id: "play_now" + selectedSong.current.index, text: "Play Now", action: playNow }, - { id: "play_next_" + selectedSong.current.uuid + selectedSong.current.index, text: "Play Next in Queue", action: playNext }, - { id: "remove_queue" + selectedSong.current.uuid + selectedSong.current.index, text: "Remove from Queue", action: removeFromQueue }, - { id: "clear_queue", text: "Clear Queue", action: clearQueue }, - ] - }) - menu.popup(await contextMenuPosition(event)); - } + const menu = await Menu.new({ + items: [ + { id: "play_now" + selectedSong.current.index, text: "Play Now", action: playNow }, + { id: "play_next_" + selectedSong.current.uuid + selectedSong.current.index, text: "Play Next in Queue", action: playNext }, + { id: "remove_queue" + selectedSong.current.uuid + selectedSong.current.index, text: "Remove from Queue", action: removeFromQueue }, + { id: "clear_queue", text: "Clear Queue", action: clearQueue }, + ] + }) + menu.popup(await contextMenuPosition(event)); + } return (