aboutsummaryrefslogtreecommitdiff
path: root/karlgui/src/views/calendar.rs
blob: c332b8066a3a343d668f591e9dfa9f13effa7324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::ops::Range;

use crate::{helper::weekday_to_str, Globals};
use chrono::{Duration, NaiveDateTime};
use egui::Ui;
use egui_extras::TableBuilder;

#[derive(Default)]
pub struct Calendar {
    // offset: NaiveDate,
}

impl Calendar {
    pub fn ui(&mut self, ui: &mut Ui, g: &mut Globals) {
        let start_date = chrono::Utc::now().date_naive(); //self.offset;
        let end_date = start_date + chrono::Duration::days(7);
        let start_dt = start_date.and_hms(0, 0, 0);
        let end_dt = end_date.and_hms(0, 0, 0);

        let instances = if let Some(instances) = g.get_all_instances_range(start_dt, end_dt) {
            instances
        } else {
            ui.horizontal(|ui| {
                ui.spinner();
                ui.label("Loading…");
            });
            return;
        };

        TableBuilder::new(ui)
            .column(egui_extras::Size::exact(50.0))
            .columns(egui_extras::Size::remainder(), 7)
            .header(25.0, |mut ui| {
                ui.col(|_| {});
                for d in 0..7 {
                    ui.col(|ui| {
                        ui.heading(weekday_to_str(d));
                    });
                }
            })
            .body(|mut ui| {
                let mut cols: [Vec<u64>; 7] =
                    [vec![], vec![], vec![], vec![], vec![], vec![], vec![]];
                for h in 0..24 {
                    ui.row(50.0, |mut ui| {
                        ui.col(|ui| {
                            ui.heading(&format!("{h:02}:00"));
                        });
                        for d in 0..7 {
                            let _col = &mut cols[d];
                            let time = start_dt + Duration::days(d as i64) + Duration::hours(h);
                            let instances_here =
                                instances.iter().filter(|(_, r)| r.includes(&time));

                            ui.col(|ui| {
                                for (id, _) in instances_here {
                                    g.tasks.get(id).unwrap();
                                    ui.label(&format!("{id}"));
                                }
                            });
                        }
                    })
                }
            })
    }
}

trait Includes<T> {
    fn includes(&self, p: &T) -> bool;
}
impl Includes<NaiveDateTime> for Range<Option<NaiveDateTime>> {
    fn includes(&self, p: &NaiveDateTime) -> bool {
        match (&self.start, &self.end) {
            (None, None) => false,
            (None, Some(e)) => p < e,
            (Some(s), None) => s <= p,
            (Some(s), Some(e)) => s <= p && p < e,
        }
    }
}