use std::{ env, sync::{Arc, RwLock}, }; 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; mod components; mod drawing; mod soluna_client; use soluna_client::SolunaClient; mod solstices; 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>>>, } pub fn main() { gio::resources_register_include!("com.luminescent-dreams.dashboard.gresource") .expect("Failed to register resources"); let app = adw::Application::builder() .application_id("com.luminescent-dreams.dashboard") .resource_base_path("/com/luminescent-dreams/dashboard") .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(); 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 { date: now.date_naive(), next_event: EVENTS.next_event(now.with_timezone(&Utc)).unwrap(), events: EVENTS.yearly_events(now.year()).unwrap(), transit: Some(transit), }; 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| { let (gtk_tx, gtk_rx) = async_std::channel::unbounded(); *core.tx.write().unwrap() = Some(gtk_tx); let window = ApplicationWindow::new(app); window.window.present(); glib::spawn_future_local(async move { loop { let Message::Refresh(state) = gtk_rx.recv().await.unwrap(); window.update_state(state); } }); }); let args: Vec = env::args().collect(); ApplicationExtManual::run_with_args(&app, &args); runtime.shutdown_background(); }