aboutsummaryrefslogtreecommitdiff
path: root/src/gui/app.rs
blob: ab3f9c02b9d85f0e8e0dde7cabde54f2402e96d2 (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
81
82
83
84
85
86
87
88
89
use gtk4 as gtk;

use anyhow::Result;
use async_channel::Sender;
use chrono::{Datelike, Duration, NaiveDate, Weekday};
use rusqlite::Connection;
use std::rc::Rc;

use crate::gui::calendar;
use crate::gui::update::Msg;
use crate::{db, model::category::Category, model::event::Event};

pub struct App {
    pub conn: Rc<Connection>,
    pub window: Rc<gtk::ApplicationWindow>,
    pub tx: Sender<Msg>,
    // Calendar
    pub calendar: gtk::Grid,
    pub events: Vec<Event>, // TODO: use Hashmap to have fast access to events by id ?
    pub recurring_events: Vec<Event>, // TODO: use Hashmap to have fast access to events by id ?
    pub today: NaiveDate,
    pub start_date: NaiveDate,
    pub end_date: NaiveDate,
    // Categories
    // pub categories: gtk::Box,
    pub categories: Vec<Category>,
    pub default_color: String,
}

impl App {
    pub fn new(conn: Rc<Connection>, app: &gtk::Application, tx: Sender<Msg>) -> Result<Self> {
        let today = chrono::offset::Local::now().naive_local().date();
        // TODO: error handling
        let start_date =
            NaiveDate::from_isoywd_opt(today.year(), today.iso_week().week(), Weekday::Mon)
                .unwrap();
        let end_date = start_date + Duration::days(7 * 4 - 1);

        let events = db::events::list_non_recurring_between(&conn, start_date, end_date)?;
        let recurring_events = db::events::list_recurring(&conn)?;
        let categories = db::categories::list(&conn)?;
        let default_color = db::event_color::get_default_color(&conn)?;

        let calendar = calendar::create(
            tx.clone(),
            today,
            start_date,
            end_date,
            &events,
            &recurring_events,
            &categories,
            &default_color,
        );

        // let categories = gtk::Box::builder()
        //     .orientation(gtk::Orientation::Vertical)
        //     .build();

        // let notebook = gtk::Notebook::builder().build();
        // notebook.append_page(&calendar, Some(&gtk::Label::new(Some("Calendrier"))));
        // notebook.append_page(&categories, Some(&gtk::Label::new(Some("Catégories"))));
        // window.set_child(Some(&notebook));

        let window = Rc::new(
            gtk::ApplicationWindow::builder()
                .application(app)
                .title("Calendrier")
                .default_width(800)
                .default_height(600)
                .visible(true)
                .child(&calendar)
                .build(),
        );

        Ok(Self {
            conn,
            window,
            tx,
            calendar,
            events,
            recurring_events,
            today,
            start_date,
            end_date,
            categories,
            default_color,
        })
    }
}