diff --git a/rustclock/src/main.rs b/rustclock/src/main.rs index b695ba5..5f1b97d 100644 --- a/rustclock/src/main.rs +++ b/rustclock/src/main.rs @@ -9,6 +9,7 @@ use iced::Fill; use iced::widget::pane_grid::Configuration; use iced::widget::{column, container, image, pane_grid, row, scrollable, text}; use sports::Game; +use std::collections::HashMap; pub fn main() -> iced::Result { iced::run(State::update, State::view) @@ -28,6 +29,7 @@ enum PaneType { enum Message { PaneDragged(pane_grid::DragEvent), PaneResized(pane_grid::ResizeEvent), + RunSportsUpdate, } #[derive(Debug)] @@ -41,6 +43,8 @@ struct State { mlb_scores: Vec, // nfl_scores: Vec, panes: pane_grid::State, + nba_logos: HashMap>, + mlb_logos: HashMap>, } impl State { fn update(&mut self, message: Message) { @@ -52,6 +56,10 @@ impl State { Message::PaneResized(pane_grid::ResizeEvent { split, ratio }) => { self.panes.resize(split, ratio); } + Message::RunSportsUpdate => { + self.nba_scores = sports::update_nba(); + self.mlb_scores = sports::update_mlb(); + } } } @@ -60,11 +68,23 @@ impl State { let content: Element<'_, Message> = match pane_state { PaneType::NbaPane => { let games = &state.nba_scores; + column(games.iter().map(|game| { + let Some(team1_logo) = state.nba_logos.get(&game.team1) else { + return text(format!("Error: Team 1 logo not found for {}", game.team1)).into(); + }; + let team1_handle = image::Handle::from_bytes(team1_logo.clone()); + let Some(team2_logo) = state.nba_logos.get(&game.team2) else { + return text("Error: Team 2 logo not found").into(); + }; + let team2_handle = image::Handle::from_bytes(team2_logo.clone()); + container( column![ row![ + image(team1_handle).width(30).height(30), text(&game.team1).size(20).width(Fill), + image(team2_handle).width(30).height(30), text(&game.team2).size(20).width(Fill), ], row![ @@ -104,13 +124,13 @@ impl State { PaneType::News => text("News").into(), PaneType::MlbPane => { let games = &state.mlb_scores; - let logos = sports::get_mlb_logos(); // TODO: MOVE TO INITIALIZATION AND CACHE + // TODO: MOVE TO INITIALIZATION AND CACHE scrollable(column(games.iter().map(|game| { - let Some(team1_logo) = logos.get(&game.team1) else { - return text("Error: Team 1 logo not found").into(); + let Some(team1_logo) = state.mlb_logos.get(&game.team1) else { + return text(format!("Error: Team 1 logo not found for {}", game.team1)).into(); }; let team1_handle = image::Handle::from_bytes(team1_logo.clone()); - let Some(team2_logo) = logos.get(&game.team2) else { + let Some(team2_logo) = state.mlb_logos.get(&game.team2) else { return text("Error: Team 2 logo not found").into(); }; let team2_handle = image::Handle::from_bytes(team2_logo.clone()); @@ -196,6 +216,9 @@ impl Default for State { .read_to_vec() .unwrap(); + let mlb_logos = sports::get_mlb_logos(); + let nba_logos = sports::get_nba_logos(); + State { current_time: Local::now(), next_alarm: None, @@ -204,6 +227,8 @@ impl Default for State { location: "Sacramento".to_string(), nba_scores: { sports::update_nba() }, mlb_scores: { sports::update_mlb() }, + mlb_logos, + nba_logos, panes: { let config = Configuration::Split { axis: pane_grid::Axis::Horizontal, diff --git a/rustclock/src/sports.rs b/rustclock/src/sports.rs index ef29944..5dacd6b 100644 --- a/rustclock/src/sports.rs +++ b/rustclock/src/sports.rs @@ -161,3 +161,29 @@ pub fn get_mlb_logos() -> HashMap> { } logos_svg_map } + +pub fn get_nba_logos() -> HashMap> { + let json = std::fs::read_to_string("src/files/nba_logos.json").unwrap(); + let parsed_json: serde_json::Value = serde_json::from_str(&json).unwrap(); + let teams = parsed_json.as_array().unwrap(); + + let mut logos_map = std::collections::HashMap::new(); + for team in teams { + let team_name = team["name"].as_str().unwrap(); + let logo_url = team["logo"].as_str().unwrap(); + logos_map.insert(team_name.to_string(), logo_url.to_string()); + } + let mut nba_svg_map = std::collections::HashMap::new(); + for (team_name, logo_url) in logos_map.iter() { + let response = ureq::get(logo_url) + .header("User-Agent", "deathclock-app/0.1") + .call() + .unwrap(); + println!("Response {}", response.status()); + + let image_data = response.into_body().read_to_vec().unwrap(); + nba_svg_map.insert(team_name.to_string(), image_data); + println!("Downloaded logo for {}", team_name); + } + nba_svg_map +}