aboutsummaryrefslogtreecommitdiff
path: root/src/app/app.rs
blob: d93b5448d2bc65e6318d97d65389253f1b6fd3b2 (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
use gtk4 as gtk;

use async_channel::Sender;
use chrono::{Datelike, NaiveDate, Weekday};
use gtk::glib::signal::Inhibit;
use gtk::prelude::*;
use rusqlite::Connection;
use std::rc::Rc;

use crate::app::calendar;
use crate::app::update::Msg;
use crate::{db, model::event::Event};

pub struct App {
    pub window: Rc<gtk::ApplicationWindow>,
    pub grid: gtk::Grid,
    pub events: Vec<Event>,
    pub today: NaiveDate,
    pub start_date: NaiveDate,
    pub tx: Sender<Msg>,
}

impl App {
    pub fn new(conn: Rc<Connection>, app: &gtk::Application, tx: Sender<Msg>) -> Self {
        let window = Rc::new(
            gtk::ApplicationWindow::builder()
                .application(app)
                .title("Calendar")
                .default_width(800)
                .default_height(600)
                .visible(true)
                .build(),
        );

        let today = chrono::offset::Local::today().naive_utc();
        let start_date =
            NaiveDate::from_isoywd(today.year(), today.iso_week().week(), Weekday::Mon);
        let events = db::list(&conn).unwrap_or(vec![]);
        let grid = calendar::create(tx.clone(), &today, &start_date, &events);

        window.set_child(Some(&grid));

        window.connect_close_request(move |window| {
            if let Some(application) = window.application() {
                application.remove_window(window);
            }
            Inhibit(false)
        });

        Self {
            window,
            grid,
            events,
            today,
            start_date,
            tx,
        }
    }
}