From 553e24800b84b56d5acf6709e10e397b95225017 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sun, 6 Oct 2024 15:35:18 -0400 Subject: [PATCH] Refactor DashboardTile into trait --- src/components/dashboard_row.rs | 8 ++-- src/components/dashboard_tile.rs | 70 +++++++------------------------- 2 files changed, 17 insertions(+), 61 deletions(-) diff --git a/src/components/dashboard_row.rs b/src/components/dashboard_row.rs index 1690d6d..7abd258 100644 --- a/src/components/dashboard_row.rs +++ b/src/components/dashboard_row.rs @@ -2,19 +2,17 @@ use leptos::html::Ul; use leptos::leptos_dom::*; use leptos::*; use leptos_use::{use_element_size, UseElementSizeReturn, use_scroll, UseScrollReturn}; -use serde::{Deserialize, Serialize}; use crate::components::dashboard_tile::DashboardTile; use leptos_icons::*; /// A row of dashboard tiles, with a title -#[derive(Serialize, Deserialize)] pub struct DashboardRow { pub title: String, - pub tiles: Vec, + pub tiles: Vec>, } impl DashboardRow { - pub fn new(title: String, tiles: Vec) -> Self { + pub fn new(title: String, tiles: Vec>) -> Self { Self { title, tiles, @@ -109,7 +107,7 @@ impl IntoView for DashboardRow { {self.tiles.into_iter().map(|tile_info| { view! {
  • - { tile_info } + { tile_info.into_view() }
  • } }).collect::>()} diff --git a/src/components/dashboard_tile.rs b/src/components/dashboard_tile.rs index 899981f..354aad5 100644 --- a/src/components/dashboard_tile.rs +++ b/src/components/dashboard_tile.rs @@ -1,68 +1,26 @@ use leptos::leptos_dom::*; use leptos::*; -use serde::{Deserialize, Serialize}; -use crate::media_type::MediaType; -/// Info representing what will be displayed in a dashboard tile -#[derive(Serialize, Deserialize)] -pub struct DashboardTile { - pub image_path: String, - pub title: String, - pub media_type: Option, - pub artist: Option, +pub trait DashboardTile { + fn image_path(&self) -> String; + fn title(&self) -> String; + fn link(&self) -> String; + fn description(&self) -> Option { None } } -impl DashboardTile { - pub fn new(image_path: String, title: String, media_type: Option, artist: Option) -> Self { - Self { - image_path, - title, - media_type, - artist: artist.map(|artist| artist.to_string()), - } - } - - /// Get the description of the dashboard tile - /// Will display the media type, and the artist if it is available and relevant - pub fn description(&self) -> String { - match self.media_type { - Some(MediaType::Song) => { - if let Some(artist) = &self.artist { - format!("{} • {}", MediaType::Song.to_string(), artist) - } else { - MediaType::Song.to_string() - } - }, - Some(MediaType::Album) => { - if let Some(artist) = &self.artist { - format!("{} • {}", MediaType::Album.to_string(), artist) - } else { - MediaType::Album.to_string() - } - }, - Some(MediaType::Artist) => { - MediaType::Artist.to_string() - }, - None => { - if let Some(artist) = &self.artist { - artist.to_string() - } else { - "".to_string() - } - } - } - } -} - -impl IntoView for DashboardTile { +impl IntoView for &dyn DashboardTile { fn into_view(self) -> View { - let description = self.description(); + let link = self.link(); view! { }.into_view() }