use chrono::{Datelike, Local}; use geo_types::{Latitude, Longitude}; use glib::Sender; use gtk::{prelude::*, subclass::prelude::*, Orientation}; use ifc::IFC; use std::{ env, sync::{Arc, RwLock}, }; mod components; use components::{Date, TransitClock}; mod drawing; mod soluna_client; use soluna_client::{SolunaClient, SunMoon}; /* mod solstices; use solstices::EVENTS; */ 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, Debug)] pub struct State { date: IFC, transit: Option, } #[derive(Clone)] pub struct Core { tx: Arc>>>, } pub fn main() { let app = gtk::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 = Arc::new( tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap(), ); 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(); let transit = soluna_client .request(latitude, longitude, Local::now()) .await; let state = State { date: IFC::from(Local::now().date_naive().with_year(12023).unwrap()), transit: Some(transit), }; loop { 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(300)); } } }); app.connect_activate(move |app| { let (gtk_tx, gtk_rx) = gtk::glib::MainContext::channel::(gtk::glib::PRIORITY_DEFAULT); *core.tx.write().unwrap() = Some(gtk_tx); let window = gtk::ApplicationWindow::new(app); window.present(); let layout = gtk::Box::builder() .orientation(Orientation::Vertical) .hexpand(true) .vexpand(true) .build(); let date_label = Date::new(); layout.append(&date_label); let transit_clock = TransitClock::new(); layout.append(&transit_clock); window.set_child(Some(&layout)); gtk_rx.attach(None, move |msg| { let Message::Refresh(state) = msg; println!("new state: {:?}", state); date_label.update_date(state.date); if let Some(transit) = state.transit { transit_clock.update_transit(transit); } Continue(true) }); std::thread::spawn(move || {}); }); let args: Vec = env::args().collect(); ApplicationExtManual::run_with_args(&app, &args); let _ = runtime.block_on(async { app_handle.await }); /* let now = Local::now(); let ifc = ifc::IFC::from(now.date_naive().with_year(12023).unwrap()); let next_event = EVENTS.next_event(now.with_timezone(&Utc)).unwrap(); println!( "{:?}, {:?} {}, {}", ifc.weekday(), ifc.month(), ifc.day(), ifc.year() ); println!("{:?}", next_event); let latitude = Latitude::from(41.78); let longitude = Longitude::from(-71.41); let soluna_client = SolunaClient::new(); let sun_moon = soluna_client .request(latitude, longitude, Local::now()) .await; println!( "Solar Transit: {} -> {}", sun_moon.sunrise.format("%H:%M").to_string(), sun_moon.sunset.format("%H:%M").to_string() ); println!( "Lunar Transit: {} -> {} [{:?}]", sun_moon .moonrise .map(|time| time.format("%H:%M").to_string()) .unwrap_or(" -- ".to_owned()), sun_moon .moonset .map(|time| time.format("%H:%M").to_string()) .unwrap_or(" -- ".to_owned()), sun_moon.moon_phase ); */ }