monorepo/dashboard/src/main.rs

144 lines
3.3 KiB
Rust
Raw Normal View History

2023-08-09 18:02:20 +00:00
use chrono::{Datelike, Local, Utc};
use geo_types::{Latitude, Longitude};
2023-08-09 17:15:32 +00:00
use glib::Sender;
2023-08-09 18:04:48 +00:00
use gtk::prelude::*;
2023-08-08 02:35:43 +00:00
use ifc::IFC;
use std::{
env,
sync::{Arc, RwLock},
};
2023-08-08 21:25:21 +00:00
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() {
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 = Arc::new(
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)),
};
let app_handle = 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 {
date: IFC::from(now.date_naive().with_year(12023).unwrap()),
next_event: EVENTS.next_event(now.with_timezone(&Utc)).unwrap(),
transit: Some(transit),
};
if let Some(ref gtk_tx) = *core.tx.read().unwrap() {
let _ = gtk_tx.send(Message::Refresh(state.clone()));
}
std::thread::sleep(std::time::Duration::from_secs(60));
}
}
});
app.connect_activate(move |app| {
let (gtk_tx, gtk_rx) =
gtk::glib::MainContext::channel::<Message>(gtk::glib::PRIORITY_DEFAULT);
*core.tx.write().unwrap() = Some(gtk_tx);
let window = ApplicationWindow::new(app);
window.window.present();
gtk_rx.attach(None, {
let window = window.clone();
move |msg| {
let Message::Refresh(state) = msg;
ApplicationWindow::update_state(&window, state);
Continue(true)
}
});
std::thread::spawn(move || {});
});
2023-08-08 21:25:21 +00:00
let args: Vec<String> = env::args().collect();
ApplicationExtManual::run_with_args(&app, &args);
2023-08-07 23:06:00 +00:00
let _ = runtime.block_on(async { app_handle.await });
}