mirror of
https://github.com/Death916/deathclock.git
synced 2026-04-10 03:04:40 -07:00
make weather async and add boot task to main iced app
This commit is contained in:
parent
535d1f4110
commit
2402edb209
3 changed files with 36 additions and 14 deletions
|
|
@ -20,7 +20,7 @@ const WEATHER_UPDATE_TIME_MINS: u64 = 30;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application(
|
iced::application(
|
||||||
|| (RustClock::default(), Task::none()), // Wrap it in a closure
|
|| (RustClock::default(), Task::perform(weather::get_weather(), Message::UpdateWeatherImg)), // Wrap it in a closure
|
||||||
RustClock::update,
|
RustClock::update,
|
||||||
RustClock::view,
|
RustClock::view,
|
||||||
)
|
)
|
||||||
|
|
@ -45,7 +45,8 @@ enum Message {
|
||||||
PaneResized(pane_grid::ResizeEvent),
|
PaneResized(pane_grid::ResizeEvent),
|
||||||
RunSportsUpdate,
|
RunSportsUpdate,
|
||||||
UpdateTime,
|
UpdateTime,
|
||||||
UpdateWeather,
|
RunWeatherUpdate,
|
||||||
|
UpdateWeatherImg(Handle),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -62,23 +63,32 @@ struct RustClock {
|
||||||
weather_handle: Option<Handle>,
|
weather_handle: Option<Handle>,
|
||||||
}
|
}
|
||||||
impl RustClock {
|
impl RustClock {
|
||||||
fn update(&mut self, message: Message) {
|
fn update(&mut self, message: Message) -> iced::Task<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::PaneDragged(pane_grid::DragEvent::Dropped { pane, target }) => {
|
Message::PaneDragged(pane_grid::DragEvent::Dropped { pane, target }) => {
|
||||||
self.panes.drop(pane, target);
|
self.panes.drop(pane, target);
|
||||||
|
Task::none()
|
||||||
}
|
}
|
||||||
Message::PaneDragged(_) => {}
|
Message::PaneDragged(_) => Task::none(),
|
||||||
|
|
||||||
Message::PaneResized(pane_grid::ResizeEvent { split, ratio }) => {
|
Message::PaneResized(pane_grid::ResizeEvent { split, ratio }) => {
|
||||||
self.panes.resize(split, ratio);
|
self.panes.resize(split, ratio);
|
||||||
|
Task::none()
|
||||||
}
|
}
|
||||||
Message::RunSportsUpdate => {
|
Message::RunSportsUpdate => {
|
||||||
self.nba_scores = sports::update_nba();
|
self.nba_scores = sports::update_nba();
|
||||||
self.mlb_scores = sports::update_mlb();
|
self.mlb_scores = sports::update_mlb();
|
||||||
|
Task::none()
|
||||||
}
|
}
|
||||||
Message::UpdateTime => {
|
Message::UpdateTime => {
|
||||||
self.current_time = Local::now();
|
self.current_time = Local::now();
|
||||||
|
Task::none()
|
||||||
|
}
|
||||||
|
Message::RunWeatherUpdate => Task::perform(weather::get_weather(), Message::UpdateWeatherImg),
|
||||||
|
Message::UpdateWeatherImg(handle) => {
|
||||||
|
self.weather_handle = Some(handle);
|
||||||
|
Task::none()
|
||||||
}
|
}
|
||||||
Message::UpdateWeather => self.weather_handle = weather::get_weather(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,7 +99,7 @@ impl RustClock {
|
||||||
iced::time::every(Duration::from_mins(UPDATE_SPORTS_TIME_MINS))
|
iced::time::every(Duration::from_mins(UPDATE_SPORTS_TIME_MINS))
|
||||||
.map(|_| Message::RunSportsUpdate),
|
.map(|_| Message::RunSportsUpdate),
|
||||||
iced::time::every(Duration::from_mins(WEATHER_UPDATE_TIME_MINS))
|
iced::time::every(Duration::from_mins(WEATHER_UPDATE_TIME_MINS))
|
||||||
.map(|_| Message::UpdateWeather),
|
.map(|_| Message::RunWeatherUpdate),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -127,6 +137,7 @@ impl Default for RustClock {
|
||||||
.map(|(k, v)| (k, Handle::from_bytes(v)))
|
.map(|(k, v)| (k, Handle::from_bytes(v)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
||||||
RustClock {
|
RustClock {
|
||||||
current_time: Local::now(),
|
current_time: Local::now(),
|
||||||
next_alarm: None,
|
next_alarm: None,
|
||||||
|
|
@ -136,7 +147,7 @@ impl Default for RustClock {
|
||||||
mlb_scores: { sports::update_mlb() },
|
mlb_scores: { sports::update_mlb() },
|
||||||
mlb_logos,
|
mlb_logos,
|
||||||
nba_logos,
|
nba_logos,
|
||||||
weather_handle: weather::get_weather(),
|
weather_handle: None,
|
||||||
panes: {
|
panes: {
|
||||||
let config = Configuration::Split {
|
let config = Configuration::Split {
|
||||||
axis: pane_grid::Axis::Horizontal,
|
axis: pane_grid::Axis::Horizontal,
|
||||||
|
|
|
||||||
|
|
@ -155,3 +155,4 @@ pub fn render_weather_pane<'a>(
|
||||||
.center_y(Fill)
|
.center_y(Fill)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,26 @@
|
||||||
use iced::widget::image::Handle;
|
use iced::widget::image::Handle;
|
||||||
use ureq;
|
use reqwest;
|
||||||
|
|
||||||
pub fn get_weather() -> Option<Handle> {
|
pub async fn get_weather() -> Handle {
|
||||||
let image = ureq::get("https://v2.wttr.in/Sacramento.png?u0")
|
let image = reqwest::get("https://v2.wttr.in/Sacramento.png?u0")
|
||||||
.header("User-Agent", "deathclock-app/1.0")
|
.await
|
||||||
.call()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.into_body()
|
.bytes()
|
||||||
.read_to_vec()
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let handle = Some(Handle::from_bytes(image));
|
let handle = Some(Handle::from_bytes(image));
|
||||||
|
let handle = handle.unwrap();
|
||||||
|
//TODO better error handling
|
||||||
dbg!("updating weather");
|
dbg!("updating weather");
|
||||||
handle
|
handle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
|
||||||
|
async fn test_get_weather() {
|
||||||
|
let handle = get_weather().await;
|
||||||
|
let handle_type: Handle = handle.clone();
|
||||||
|
assert_eq!(handle_type, handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue