Skip to content

Commit

Permalink
Refactor in order to be able to do async stuff (#3)
Browse files Browse the repository at this point in the history
* Started on async

* progress on async, woop

* no idea what I am doing

* UI double imports resolved

* holy shit its almost async

* holy shit its almost async

* try some looping

* some cleanup

* some cleanup

* start on moving structs out

* start on moving structs out

* even more cleanup

* move xkcd

* move xkcd

* cleanup

* more cleanup
  • Loading branch information
sverrejb authored Jan 17, 2024
1 parent e369314 commit 6c6ed3d
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 281 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ slint = "1.3.2"
rust-embed = "6.8.1"
log = "0.4.20"
env_logger = "0.10.1"
tokio = "1.35.1"


# Vendored OpenSSL seems to be required for building on Linux on GHA, but not for Windows or Mac. Maybe this can be fixed some other way
[target.'cfg(target_os = "linux")'.dependencies]
Expand Down
75 changes: 15 additions & 60 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use rust_embed::RustEmbed;
slint::include_modules!();

extern crate chrono;

use std::rc::Rc;

use chrono::Local;
use log::{error, info};
use slint::{Timer, TimerMode};
use log::info;
use rust_embed::RustEmbed;
use slint::*;
use ui::*;

mod weather;
mod xkcd;

use crate::weather::*;
use crate::xkcd::*;
pub mod ui {
slint::include_modules!();
}

// we embed img folder into the compiled binary for simpler distribution
#[derive(RustEmbed)]
Expand All @@ -24,22 +22,21 @@ fn main() -> Result<(), slint::PlatformError> {
env_logger::init();
info!("Starting up...");

let ui = MainWindow::new()?;
let clock_timer = Timer::default();
let xkcd_timer = Timer::default();
let weather_timer = Timer::default();
let main_window = MainWindow::new().unwrap();

weather::setup(&main_window);
xkcd::setup(&main_window);

let clock_handle = ui.as_weak();
let xkcd_handle = ui.as_weak();
let weather_handle = ui.as_weak();
let clock_timer = Timer::default();
let clock_handle = main_window.as_weak();

clock_timer.start(
TimerMode::Repeated,
std::time::Duration::from_millis(1000),
move || {
let ui = clock_handle.unwrap();
let now = Local::now();
let datestring = format!("{}", now.format("%H:%M:%S"));
let datestring = std::format!("{}", now.format("%H:%M:%S"));
let date = now.format("%d").to_string().into();
let month = now.format("%b").to_string().to_uppercase().into();
ui.set_time(datestring.into());
Expand All @@ -48,47 +45,5 @@ fn main() -> Result<(), slint::PlatformError> {
},
);

match get_current_xkcd() {
Ok(xkcd) => {
ui.set_xkcd(xkcd);
info!("Initial xkcd set")
}
Err(e) => {
error!("Error setting initial XKCD: {}", e)
}
}

xkcd_timer.start(
TimerMode::Repeated,
std::time::Duration::from_secs(3600 * 2),
move || {
let ui = xkcd_handle.unwrap();
match get_current_xkcd() {
Ok(xkcd) => ui.set_xkcd(xkcd),
Err(e) => eprintln!("{}", e),
}
},
);

match get_forecast() {
Ok(forecasts) => {
ui.set_forecasts(Rc::new(forecasts).into());
info!("Initial weather set")
}
Err(e) => error!("Error setting initial forecast{}", e),
}

weather_timer.start(
TimerMode::Repeated,
std::time::Duration::from_secs(900),
move || {
let ui = weather_handle.unwrap();
match get_forecast() {
Ok(forecasts) => ui.set_forecasts(Rc::new(forecasts).into()),
Err(e) => eprintln!("{}", e),
}
},
);

ui.run()
main_window.run()
}
142 changes: 0 additions & 142 deletions src/weather.rs

This file was deleted.

Loading

0 comments on commit 6c6ed3d

Please sign in to comment.