From 3e13cbcdaaeb9225128bb5d8e9cc71ba5f13b1ac Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 10 Aug 2023 10:13:57 -0400 Subject: [PATCH] Add the transit times to a card --- dashboard/src/app_window.rs | 8 ++- dashboard/src/components/mod.rs | 3 + dashboard/src/components/transit_card.rs | 73 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 dashboard/src/components/transit_card.rs diff --git a/dashboard/src/app_window.rs b/dashboard/src/app_window.rs index 7051687..0f877ab 100644 --- a/dashboard/src/app_window.rs +++ b/dashboard/src/app_window.rs @@ -1,5 +1,5 @@ use crate::{ - components::{Date, TransitClock}, + components::{Date, TransitCard, TransitClock}, types::State, }; use adw::prelude::AdwApplicationWindowExt; @@ -11,6 +11,7 @@ pub struct ApplicationWindow { pub window: adw::ApplicationWindow, pub date_label: Date, pub next_event: gtk::Label, + pub transit_card: TransitCard, pub transit_clock: TransitClock, } @@ -52,6 +53,9 @@ impl ApplicationWindow { next_event.add_css_class("activatable"); layout.append(&next_event); + let transit_card = TransitCard::new(); + layout.append(&transit_card); + let transit_clock = TransitClock::new(); layout.append(&transit_clock); @@ -61,6 +65,7 @@ impl ApplicationWindow { window, date_label, next_event, + transit_card, transit_clock, } } @@ -69,6 +74,7 @@ impl ApplicationWindow { self.date_label.update_date(state.date); self.next_event.set_text(&format!("{:?}", state.next_event)); if let Some(transit) = state.transit { + self.transit_card.update_transit(&transit); self.transit_clock.update_transit(transit); } } diff --git a/dashboard/src/components/mod.rs b/dashboard/src/components/mod.rs index a011020..2abc253 100644 --- a/dashboard/src/components/mod.rs +++ b/dashboard/src/components/mod.rs @@ -1,5 +1,8 @@ mod date; pub use date::Date; +mod transit_card; +pub use transit_card::TransitCard; + mod transit_clock; pub use transit_clock::TransitClock; diff --git a/dashboard/src/components/transit_card.rs b/dashboard/src/components/transit_card.rs new file mode 100644 index 0000000..565fd4c --- /dev/null +++ b/dashboard/src/components/transit_card.rs @@ -0,0 +1,73 @@ +use crate::soluna_client::SunMoon; +use glib::Object; +use gtk::{prelude::*, subclass::prelude::*}; +use std::{cell::RefCell, rc::Rc}; + +pub struct TransitCardPrivate { + sunrise: gtk::Label, + sunset: gtk::Label, + moonrise: gtk::Label, + moonset: gtk::Label, +} + +impl Default for TransitCardPrivate { + fn default() -> Self { + Self { + sunrise: gtk::Label::new(None), + sunset: gtk::Label::new(None), + moonrise: gtk::Label::new(None), + moonset: gtk::Label::new(None), + } + } +} + +#[glib::object_subclass] +impl ObjectSubclass for TransitCardPrivate { + const NAME: &'static str = "TransitCard"; + type Type = TransitCard; + type ParentType = gtk::Grid; +} + +impl ObjectImpl for TransitCardPrivate {} +impl WidgetImpl for TransitCardPrivate {} +impl GridImpl for TransitCardPrivate {} + +glib::wrapper! { + pub struct TransitCard(ObjectSubclass) @extends gtk::Grid, gtk::Widget; +} + +impl TransitCard { + pub fn new() -> Self { + let s: Self = Object::builder().build(); + s.add_css_class("card"); + s.set_column_homogeneous(true); + + s.attach(&s.imp().sunrise, 0, 0, 1, 1); + s.attach(&s.imp().sunset, 0, 1, 1, 1); + s.attach(&s.imp().moonrise, 1, 0, 1, 1); + s.attach(&s.imp().moonset, 1, 1, 1, 1); + + s + } + + pub fn update_transit(&self, transit_info: &SunMoon) { + self.imp() + .sunrise + .set_text(format!("{}", transit_info.sunrise.format("%H:%M")).as_ref()); + self.imp() + .sunset + .set_text(format!("{}", transit_info.sunset.format("%H:%M")).as_ref()); + self.imp().moonrise.set_text( + &transit_info + .moonrise + .map(|time| format!("{}", time.format("%H:%M"))) + .unwrap_or("".to_owned()), + ); + self.imp().moonset.set_text( + &transit_info + .moonset + .map(|time| format!("{}", time.format("%H:%M"))) + .unwrap_or("".to_owned()), + ); + } +}