monorepo/dashboard/src/main.rs

144 lines
3.4 KiB
Rust
Raw Normal View History

use std::{
env,
sync::{Arc, RwLock},
};
2023-08-08 21:25:21 +00:00
2024-09-24 02:44:25 +00:00
use async_std::channel::Sender;
use chrono::{Datelike, Local, Utc};
use geo_types::{Latitude, Longitude};
use gtk::prelude::*;
mod app_window;
use app_window::ApplicationWindow;
2023-08-09 17:15:32 +00:00
mod components;
2023-08-07 23:06:00 +00:00
2023-08-09 17:15:32 +00:00
mod drawing;
mod soluna_client;
2023-08-09 18:04:48 +00:00
use soluna_client::SolunaClient;
mod solstices;
2023-08-07 23:06:00 +00:00
use solstices::EVENTS;
mod types;
use types::State;
/*
const EO_TEXT: &'static str = "
day = {$day ->
*[Sunday] Dimanĉo
[Monday] Lundo
[Tuesday] Mardo
[Wednesday] Merkredo
[Thursday] Ĵaŭdo
[Friday] Vendredo
[Saturday] Sabato
[LeapDay] Leap Day
[YearDay] Year Day
}
month = {$month ->
*[January] Januaro
[February] Februaro
[March] Marto
[April] Aprilo
[May] Mayo
[June] Junio
[Sol] Solo
[July] Julio
[August] Aŭgusto
[September] Septembro
[October] Oktobro
[November] Novembro
[December] Decembro
}
spring_equinox = Printempa Ekvinokso
summer_solstice = Somera Solstico
autumn_equinox = Aŭtuna Ekvinokso
winter_solstice = Vintra Solstico
";
*/
#[derive(Clone, Debug)]
pub enum Message {
Refresh(State),
}
#[derive(Clone)]
pub struct Core {
tx: Arc<RwLock<Option<Sender<Message>>>>,
}
2023-08-07 23:06:00 +00:00
pub fn main() {
2023-08-10 15:07:35 +00:00
gio::resources_register_include!("com.luminescent-dreams.dashboard.gresource")
.expect("Failed to register resources");
let app = adw::Application::builder()
2023-08-07 23:06:00 +00:00
.application_id("com.luminescent-dreams.dashboard")
2023-08-08 21:25:21 +00:00
.resource_base_path("/com/luminescent-dreams/dashboard")
2023-08-07 23:06:00 +00:00
.build();
let latitude = Latitude::from(41.78);
let longitude = Longitude::from(-71.41);
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
2023-08-07 23:06:00 +00:00
let core = Core {
tx: Arc::new(RwLock::new(None)),
};
runtime.spawn({
let core = core.clone();
async move {
let soluna_client = SolunaClient::new();
loop {
let transit = soluna_client
.request(latitude.clone(), longitude.clone(), Local::now())
.await;
let now = Local::now();
let state = State {
2024-09-24 02:44:25 +00:00
date: now.date_naive(),
next_event: EVENTS.next_event(now.with_timezone(&Utc)).unwrap(),
2023-08-10 16:35:48 +00:00
events: EVENTS.yearly_events(now.year()).unwrap(),
transit: Some(transit),
};
2024-09-24 02:44:25 +00:00
let gtk_tx = core.tx.read().unwrap().clone();
if let Some(gtk_tx) = gtk_tx {
let state = state.clone();
let _ = gtk_tx.send(Message::Refresh(state)).await;
std::thread::sleep(std::time::Duration::from_secs(60));
} else {
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
}
});
app.connect_activate(move |app| {
2024-09-24 02:44:25 +00:00
let (gtk_tx, gtk_rx) = async_std::channel::unbounded();
*core.tx.write().unwrap() = Some(gtk_tx);
let window = ApplicationWindow::new(app);
window.window.present();
2024-09-24 02:44:25 +00:00
glib::spawn_future_local(async move {
loop {
let Message::Refresh(state) = gtk_rx.recv().await.unwrap();
window.update_state(state);
}
});
});
2023-08-08 21:25:21 +00:00
let args: Vec<String> = env::args().collect();
ApplicationExtManual::run_with_args(&app, &args);
runtime.shutdown_background();
}