Start adding support for events

Signed-off-by: Fabrizio Iannetti <fabrizio.iannetti@gmail.com>
This commit is contained in:
Fabrizio Iannetti 2023-01-15 15:38:41 +01:00
parent 966752614a
commit 7085a2eed8
4 changed files with 73 additions and 8 deletions

View File

@ -1,6 +1,9 @@
//! Simple calendar applications.
mod ui;
mod model;
use ui::calendar;
use model::events::EventsCollection;
use chrono::{Datelike, NaiveDate, Months, Utc};
use calendar::{CalendarMonthView, CalendarYearView, CalendarParams };
@ -27,6 +30,7 @@ pub fn main() -> iced::Result {
struct CalendarApp {
month: NaiveDate,
controls: Controls,
events: EventsCollection,
}
#[derive(Debug, Clone, Copy)]
@ -119,6 +123,7 @@ impl Application for CalendarApp {
let month = Utc::today().naive_local();
(CalendarApp {
month,
events: EventsCollection::new(),
..CalendarApp::default()
}, Command::none())
}
@ -163,7 +168,7 @@ impl Application for CalendarApp {
;
match self.controls.mode {
Some(ViewMode::Year) => content = content.push(CalendarYearView::new(&CalendarParams::new(), self.month)),
Some(ViewMode::Year) => content = content.push(CalendarYearView::new(&CalendarParams::new(), self.month, &self.events)),
Some(ViewMode::Month) | None => content = content.push(CalendarMonthView::new(&CalendarParams::new(), self.month)),
};

1
src/model.rs Normal file
View File

@ -0,0 +1 @@
pub mod events;

51
src/model/events.rs Normal file
View File

@ -0,0 +1,51 @@
use chrono::NaiveDate;
use std::vec::Vec;
use std::default::Default;
pub struct Event {
begin: NaiveDate,
end: NaiveDate,
}
impl Event {
fn is_in_day(&self, day: NaiveDate) -> bool {
day >= self.begin && day <= self.end
}
}
pub struct EventsCollection {
events: Vec<Event>,
}
impl EventsCollection {
pub fn new() -> Self {
EventsCollection {
// TODO: hard-coded events
events: vec![
Event {
begin: NaiveDate::from_ymd_opt(2023, 01, 15).unwrap(),
end: NaiveDate::from_ymd_opt(2023, 01, 16).unwrap(),
},
Event {
begin: NaiveDate::from_ymd_opt(2023, 01, 16).unwrap(),
end: NaiveDate::from_ymd_opt(2023, 01, 17).unwrap(),
},
],
}
}
pub fn is_any_in_day(&self, day: NaiveDate) -> bool {
for ev_day in &self.events {
if ev_day.is_in_day(day) {
return true;
}
}
false
}
}
impl Default for EventsCollection {
fn default() -> EventsCollection {
EventsCollection::new()
}
}

View File

@ -16,6 +16,7 @@ use iced_native::alignment;
use iced_native::widget::Tree;
use chrono::{NaiveDate, Datelike, Duration, Local};
use super::basics::CellGrid;
use crate::model::events::{Event, EventsCollection};
const MONTH_NAMES: [&str;12] = [
"gen",
@ -46,6 +47,8 @@ pub struct CalendarParams {
day_weekend_bg: Color,
day_today_bg: Color,
day_text_margin: f32,
ev_bg: Color,
}
impl CalendarParams {
@ -59,6 +62,7 @@ impl CalendarParams {
day_other_month_fg: Color::from_rgb8(220, 220, 220),
day_weekend_bg: Color::from_rgb8(245, 245, 245),
day_text_margin: 5.0,
ev_bg: Color::from_rgb8(100, 100, 245)
}
}
}
@ -350,16 +354,17 @@ where
//-------------------------------------------------------------------------
pub struct CalendarYearView {
pub struct CalendarYearView<'a> {
first_day: NaiveDate,
first_day_in_view: NaiveDate,
params: CalendarParams,
month_column_font_size: f32,
margin: f32,
events: &'a EventsCollection,
}
impl CalendarYearView {
pub fn new(params: &CalendarParams, day: NaiveDate) -> Self {
impl<'a> CalendarYearView<'a> {
pub fn new(params: &CalendarParams, day: NaiveDate, events: &'a EventsCollection) -> Self {
// first day of the year
let first_day = NaiveDate::from_ymd(day.year(), 1, 1);
@ -374,7 +379,8 @@ impl CalendarYearView {
first_day_in_view,
params: params.clone(),
month_column_font_size: 24.0,
margin: 10.0
margin: 10.0,
events
}
}
@ -537,6 +543,8 @@ impl CalendarYearView {
self.params.day_today_bg
} else if weekday > 4 {
self.params.day_weekend_bg
} else if self.events.is_any_in_day(current_day) {
self.params.ev_bg
} else {
Color::TRANSPARENT
};
@ -580,7 +588,7 @@ impl CalendarYearView {
} // CalendarYearView
impl<Message, Renderer> Widget<Message, Renderer> for CalendarYearView
impl<Message, Renderer> Widget<Message, Renderer> for CalendarYearView<'_>
where
Renderer: text::Renderer,
{
@ -646,11 +654,11 @@ where
}
}
impl<'a, Message, Renderer> From<CalendarYearView> for Element<'a, Message, Renderer>
impl<'a, Message, Renderer> From<CalendarYearView<'a>> for Element<'a, Message, Renderer>
where
Renderer: text::Renderer,
{
fn from(year_view: CalendarYearView) -> Self {
fn from(year_view: CalendarYearView<'a>) -> Self {
Self::new(year_view)
}
}