Add the transit times to a card

This commit is contained in:
Savanni D'Gerinel 2023-08-10 10:13:57 -04:00
parent 4afdd16414
commit 962b507190
3 changed files with 83 additions and 1 deletions

View File

@ -1,5 +1,5 @@
use crate::{
components::{Date, TransitClock},
components::{Date, TransitCard, TransitClock},
types::State,
};
use adw::prelude::AdwApplicationWindowExt;
@ -10,6 +10,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,
}
@ -36,6 +37,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);
@ -45,6 +49,7 @@ impl ApplicationWindow {
window,
date_label,
next_event,
transit_card,
transit_clock,
}
}
@ -53,6 +58,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);
}
}

View File

@ -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;

View File

@ -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<TransitCardPrivate>) @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()),
);
}
}