From 633000062c754f5ca570eecbfae781ce2940ba42 Mon Sep 17 00:00:00 2001 From: ecco257 <72117210+ecco257@users.noreply.github.com> Date: Fri, 5 Apr 2024 23:48:24 -0400 Subject: [PATCH] Modify queue, playbar, and playstatus to work with Song model struct instead of old SongData --- src/playbar.rs | 60 +++++++++++++++++++++++++++++++++++++---------- src/playstatus.rs | 6 ++--- src/queue.rs | 2 +- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/playbar.rs b/src/playbar.rs index 11700aa..f9ee41e 100644 --- a/src/playbar.rs +++ b/src/playbar.rs @@ -1,4 +1,6 @@ use crate::playstatus::PlayStatus; +use crate::api::songs::get_artists; +use crate::api::albums::get_album; use leptos::ev::MouseEvent; use leptos::html::{Audio, Div}; use leptos::leptos_dom::*; @@ -152,7 +154,7 @@ fn PlayControls(status: RwSignal) -> impl IntoView { if let Some(last_played_song) = last_played_song { // Push the popped song to the front of the queue, and play it - let next_src = last_played_song.song_path.clone(); + let next_src = last_played_song.storage_path.clone(); status.update(|status| status.queue.push_front(last_played_song)); set_play_src(status, next_src); set_playing(status, true); @@ -245,26 +247,39 @@ fn PlayDuration(elapsed_secs: MaybeSignal, total_secs: MaybeSignal) -> fn MediaInfo(status: RwSignal) -> impl IntoView { let name = Signal::derive(move || { status.with(|status| { - status.queue.front().map_or("No media playing".into(), |song| song.name.clone()) + status.queue.front().map_or("No media playing".into(), |song| song.title.clone()) }) }); - let artist = Signal::derive(move || { + let song_id = Signal::derive(move || { status.with(|status| { - status.queue.front().map_or("".into(), |song| song.artist.clone()) + status.queue.front().map_or(None, |song| song.id) }) }); - let album = Signal::derive(move || { + let song_artists_resource = create_resource(song_id, move |song_id| async move { + let artists_vec = get_artists(song_id).await.unwrap_or(Vec::new()); + // convert the vec of artists to a string of artists separated by commas + let artists_string = artists_vec.iter().map(|artist| artist.name.clone()).collect::>().join(", "); + artists_string + }); + + let album_id = Signal::derive(move || { status.with(|status| { - status.queue.front().map_or("".into(), |song| song.album.clone()) + status.queue.front().map_or(None, |song| song.album_id) }) }); + let album_resource = create_resource(album_id, move |album_id| async move { + // get the album name attribute or return "Unknown Album" + let album_name = get_album(album_id).await.map_or("".to_string(), |album| album.title); + album_name + }); + let image = Signal::derive(move || { status.with(|status| { // TODO Use some default / unknown image? - status.queue.front().map_or("".into(), |song| song.image_path.clone()) + status.queue.front().map_or("".into(), |song| song.image_path.clone().unwrap_or("".into())) }) }); @@ -274,7 +289,28 @@ fn MediaInfo(status: RwSignal) -> impl IntoView {
{name}
- {artist} - {album} + + {move || { + song_artists_resource.get().map(|artists_string| view! { +

{artists_string}

+ }) + }} +
+ + {move || { + album_resource.get().map(|album_name| view! { +

{album_name}

+ }) + }} +
} @@ -402,11 +438,11 @@ pub fn PlayBar(status: RwSignal) -> impl IntoView { status.with_untracked(|status| { // Start playing the first song in the queue, if available if let Some(song) = status.queue.front() { - log!("Starting playing with song: {}", song.name); + log!("Starting playing with song: {}", song.title); // Don't use the set_play_src / set_playing helper function // here because we already have access to the audio element - audio.set_src(&song.song_path); + audio.set_src(&song.storage_path); if let Err(e) = audio.play() { error!("Error playing audio on load: {:?}", e); @@ -455,7 +491,7 @@ pub fn PlayBar(status: RwSignal) -> impl IntoView { let prev_song = status.queue.pop_front(); if let Some(prev_song) = prev_song { - log!("Adding song to history: {}", prev_song.name); + log!("Adding song to history: {}", prev_song.title); status.history.push_back(prev_song); } else { log!("Queue empty, no previous song to add to history"); @@ -464,7 +500,7 @@ pub fn PlayBar(status: RwSignal) -> impl IntoView { // Get the next song to play, if available let next_src = status.with_untracked(|status| { - status.queue.front().map(|song| song.song_path.clone()) + status.queue.front().map(|song| song.storage_path.clone()) }); if let Some(audio) = audio_ref.get() { diff --git a/src/playstatus.rs b/src/playstatus.rs index 5952ee9..a21db45 100644 --- a/src/playstatus.rs +++ b/src/playstatus.rs @@ -3,7 +3,7 @@ use leptos::NodeRef; use leptos::html::Audio; use std::collections::VecDeque; -use crate::songdata::SongData; +use crate::models::Song; /// Represents the global state of the audio player feature of LibreTunes pub struct PlayStatus { @@ -14,9 +14,9 @@ pub struct PlayStatus { /// A reference to the HTML audio element pub audio_player: Option>, /// A queue of songs that have been played, ordered from oldest to newest - pub history: VecDeque, + pub history: VecDeque, /// A queue of songs that have yet to be played, ordered from next up to last - pub queue: VecDeque, + pub queue: VecDeque, } impl PlayStatus { diff --git a/src/queue.rs b/src/queue.rs index fc786b9..07468e5 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -99,7 +99,7 @@ pub fn Queue(status: RwSignal) -> impl IntoView { on:dragenter=move |e: DragEvent| on_drag_enter(e, index) on:dragover=on_drag_over > - +