Update SongData for frontend use

This commit is contained in:
Ethan Girouard 2024-07-23 23:31:48 -04:00
parent ffad799f72
commit f8bbe319bd
Signed by: eta357
GPG Key ID: 7BCDC36DFD11C146
3 changed files with 28 additions and 11 deletions

View File

@ -1,3 +1,4 @@
use crate::models::Artist;
use crate::playstatus::PlayStatus; use crate::playstatus::PlayStatus;
use leptos::ev::MouseEvent; use leptos::ev::MouseEvent;
use leptos::html::{Audio, Div}; use leptos::html::{Audio, Div};
@ -243,19 +244,20 @@ fn PlayDuration(elapsed_secs: MaybeSignal<i64>, total_secs: MaybeSignal<i64>) ->
fn MediaInfo(status: RwSignal<PlayStatus>) -> impl IntoView { fn MediaInfo(status: RwSignal<PlayStatus>) -> impl IntoView {
let name = Signal::derive(move || { let name = Signal::derive(move || {
status.with(|status| { 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 artist = Signal::derive(move || {
status.with(|status| { status.with(|status| {
status.queue.front().map_or("".into(), |song| song.artist.clone()) status.queue.front().map_or("".into(), |song| format!("{}", Artist::display_list(&song.artists)))
}) })
}); });
let album = Signal::derive(move || { let album = Signal::derive(move || {
status.with(|status| { status.with(|status| {
status.queue.front().map_or("".into(), |song| song.album.clone()) status.queue.front().map_or("".into(), |song|
song.album.as_ref().map_or("".into(), |album| album.title.clone()))
}) })
}); });
@ -400,7 +402,7 @@ pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
status.with_untracked(|status| { status.with_untracked(|status| {
// Start playing the first song in the queue, if available // Start playing the first song in the queue, if available
if let Some(song) = status.queue.front() { 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 // Don't use the set_play_src / set_playing helper function
// here because we already have access to the audio element // here because we already have access to the audio element
@ -453,7 +455,7 @@ pub fn PlayBar(status: RwSignal<PlayStatus>) -> impl IntoView {
let prev_song = status.queue.pop_front(); let prev_song = status.queue.pop_front();
if let Some(prev_song) = prev_song { 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); status.history.push_back(prev_song);
} else { } else {
log!("Queue empty, no previous song to add to history"); log!("Queue empty, no previous song to add to history");

View File

@ -1,3 +1,4 @@
use crate::models::Artist;
use crate::playstatus::PlayStatus; use crate::playstatus::PlayStatus;
use crate::song::Song; use crate::song::Song;
use leptos::ev::MouseEvent; use leptos::ev::MouseEvent;
@ -98,7 +99,7 @@ pub fn Queue(status: RwSignal<PlayStatus>) -> impl IntoView {
on:dragenter=move |e: DragEvent| on_drag_enter(e, index) on:dragenter=move |e: DragEvent| on_drag_enter(e, index)
on:dragover=on_drag_over on:dragover=on_drag_over
> >
<Song song_image_path=song.image_path.clone() song_title=song.name.clone() song_artist=song.artist.clone() /> <Song song_image_path=song.image_path.clone() song_title=song.title.clone() song_artist=Artist::display_list(&song.artists) />
<Show <Show
when=move || index != 0 when=move || index != 0
fallback=|| view!{ fallback=|| view!{

View File

@ -1,12 +1,26 @@
use crate::database;
use crate::models::{Album, Artist, Song};
use time::Date;
/// Holds information about a song /// Holds information about a song
#[derive(Debug, Clone)] ///
/// Intended to be used in the front-end, as it includes artist and album objects, rather than just their ids.
pub struct SongData { pub struct SongData {
/// Song id
pub id: i32,
/// Song name /// Song name
pub name: String, pub title: String,
/// Song artist /// Song artists
pub artist: String, pub artists: Vec<Artist>,
/// Song album /// Song album
pub album: String, pub album: Option<Album>,
/// The track number of the song on the album
pub track: Option<i32>,
/// The duration of the song in seconds
pub duration: i32,
/// The song's release date
pub release_date: Option<Date>,
/// Path to song file, relative to the root of the web server. /// Path to song file, relative to the root of the web server.
/// For example, `"/assets/audio/Song.mp3"` /// For example, `"/assets/audio/Song.mp3"`
pub song_path: String, pub song_path: String,