add mlb pane

This commit is contained in:
death916 2026-03-12 05:25:25 -07:00
parent 67307043d6
commit e55e2240dd
2 changed files with 56 additions and 14 deletions

View file

@ -33,7 +33,9 @@ struct State {
news: Vec<String>, news: Vec<String>,
weather: Vec<u8>, weather: Vec<u8>,
location: String, location: String,
scores: Vec<Game>, nba_scores: Vec<Game>,
mlb_scores: Vec<Game>,
// nfl_scores: Vec<Game>,
panes: pane_grid::State<PaneType>, panes: pane_grid::State<PaneType>,
} }
impl State { impl State {
@ -53,7 +55,7 @@ impl State {
pane_grid(&state.panes, |_panes, pane_state, _is_maximized| { pane_grid(&state.panes, |_panes, pane_state, _is_maximized| {
let content: Element<'_, Message> = match pane_state { let content: Element<'_, Message> = match pane_state {
PaneType::NbaPane => { PaneType::NbaPane => {
let games = &state.scores; let games = &state.nba_scores;
column(games.iter().map(|game| { column(games.iter().map(|game| {
container( container(
column![ column![
@ -94,7 +96,47 @@ impl State {
.into() .into()
} }
PaneType::NflPane => text("NFL").into(), PaneType::NflPane => text("NFL").into(),
PaneType::MlbPane => text("MLB").into(), PaneType::MlbPane => {
let games = &state.mlb_scores;
column(games.iter().map(|game| {
container(
column![
row![
text(&game.team1).size(20).width(Fill),
text(&game.team2).size(20).width(Fill),
],
row![
text(&game.score1).size(20).width(Fill),
text(&game.score2).size(20).width(Fill),
],
text(format!("Period: {}", game.period)).size(14),
]
.padding(10),
)
.padding(5)
.width(Fill)
.style(|_| container::Style {
background: Some(iced::Background::Color(iced::Color::from_rgb(
0.2, 0.2, 0.2,
))),
border: Border {
width: 1.0,
color: iced::Color::WHITE,
radius: 0.0.into(),
},
text_color: Some(iced::Color::WHITE),
snap: true,
shadow: iced::Shadow {
color: iced::Color::BLACK,
offset: iced::Vector::new(0.0, 0.0),
blur_radius: 10.0,
},
})
.into()
}))
.padding(0)
.into()
}
PaneType::Main => text("Main").into(), PaneType::Main => text("Main").into(),
PaneType::Clock => text("clock").into(), PaneType::Clock => text("clock").into(),
PaneType::Weather => { PaneType::Weather => {
@ -123,7 +165,7 @@ impl State {
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
sports::update_mlb(); sports::update_mlb();
let text = ureq::get("https://v2.wttr.in/Sacramento.png?u0") let text = ureq::get("https://v2.wttr.in/Sacramento.png?u0")
.header("User-Agent", "deathclock-app/1.0") .header("User-Agent", "deathclock-app/1.0")
.call() .call()
@ -138,7 +180,8 @@ impl Default for State {
news: Vec::new(), news: Vec::new(),
weather: text, weather: text,
location: "Sacramento".to_string(), location: "Sacramento".to_string(),
scores: { sports::update_nba() }, nba_scores: { sports::update_nba() },
mlb_scores: { sports::update_mlb() },
panes: { panes: {
let (mut panes, nba) = pane_grid::State::new(PaneType::NbaPane); let (mut panes, nba) = pane_grid::State::new(PaneType::NbaPane);
let (weather, _) = panes let (weather, _) = panes
@ -151,7 +194,5 @@ impl Default for State {
panes panes
}, },
} }
} }
} }

View file

@ -41,7 +41,7 @@ impl Game {
} }
} }
pub fn update_mlb() { pub fn update_mlb() -> Vec<Game> {
let date = chrono::Local::now().format("%Y-%m-%d").to_string(); let date = chrono::Local::now().format("%Y-%m-%d").to_string();
let mlb_url = format!( let mlb_url = format!(
"https://statsapi.mlb.com/api/v1/schedule?sportId=1&date={}", "https://statsapi.mlb.com/api/v1/schedule?sportId=1&date={}",
@ -62,11 +62,13 @@ pub fn update_mlb() {
for game in games { for game in games {
let home_team = game["teams"]["away"]["team"]["name"].as_str().unwrap(); let home_team = game["teams"]["away"]["team"]["name"].as_str().unwrap();
println!("Home Team: {}", home_team);
let away_team = game["teams"]["home"]["team"]["name"].as_str().unwrap(); let away_team = game["teams"]["home"]["team"]["name"].as_str().unwrap();
println!("Away Team: {}", away_team); let home_score = game["teams"]["away"]["score"]
let home_score = game["teams"]["away"]["score"].as_str().unwrap_or_else(|| "0"); .as_str()
let away_score = game["teams"]["home"]["score"].as_str().unwrap_or_else(|| "0"); .unwrap_or_else(|| "0");
let away_score = game["teams"]["home"]["score"]
.as_str()
.unwrap_or_else(|| "0");
let period = game["status"]["period"] let period = game["status"]["period"]
.as_str() .as_str()
.unwrap_or_default() .unwrap_or_default()
@ -89,9 +91,8 @@ pub fn update_mlb() {
println!("Away Score: {}", away_score); println!("Away Score: {}", away_score);
println!("Period: {}", period); println!("Period: {}", period);
} }
// mlb_games_vec mlb_games_vec
} }
pub fn update_nba() -> Vec<Game> { pub fn update_nba() -> Vec<Game> {