From 5166efe517291f5c9fc6326f30651ef799d6db65 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Jan 2022 13:39:10 +0100 Subject: Add id in event model --- src/app/app.rs | 4 +++- src/app/calendar.rs | 23 +++++++++++++++++++---- src/app/form.rs | 5 +++-- src/app/update.rs | 29 ++++++----------------------- 4 files changed, 31 insertions(+), 30 deletions(-) (limited to 'src/app') diff --git a/src/app/app.rs b/src/app/app.rs index 45904a9..d93b544 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -17,6 +17,7 @@ pub struct App { pub events: Vec, pub today: NaiveDate, pub start_date: NaiveDate, + pub tx: Sender, } impl App { @@ -35,7 +36,7 @@ impl App { 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::grid(tx, &today, &start_date, &events); + let grid = calendar::create(tx.clone(), &today, &start_date, &events); window.set_child(Some(&grid)); @@ -52,6 +53,7 @@ impl App { events, today, start_date, + tx, } } } diff --git a/src/app/calendar.rs b/src/app/calendar.rs index 847ea71..250101f 100644 --- a/src/app/calendar.rs +++ b/src/app/calendar.rs @@ -5,14 +5,14 @@ use chrono::{Datelike, NaiveDate}; use gtk::glib; use gtk::prelude::*; -use crate::{app::update, app::update::Msg, model::event::Event}; +use crate::{app::update, app::update::Msg, app::App, model::event::Event}; static DAYS: [&str; 7] = ["LUN", "MAR", "MER", "JEU", "VEN", "SAM", "DIM"]; static MONTHES: [&str; 12] = [ "Jan", "Fév", "Mar", "Avr", "Mai", "Juin", "Juil", "Aoû", "Sep", "Oct", "Nov", "Déc", ]; -pub fn grid( +pub fn create( tx: Sender, today: &NaiveDate, start_date: &NaiveDate, @@ -24,12 +24,12 @@ pub fn grid( grid.attach(&day_title(col), col, 0, 1, 1); } - show_days(tx, &grid, &start_date, &today, &events); + attach_days(tx, &grid, &start_date, &today, &events); grid } -fn show_days( +fn attach_days( tx: Sender, grid: >k::Grid, start_date: &NaiveDate, @@ -45,6 +45,21 @@ fn show_days( } } +pub fn refresh_date(app: &App, date: NaiveDate) { + let d = date.signed_duration_since(app.start_date).num_days(); + + let col = (d % 7) as i32; + let row = 1 + (d / 7) as i32; + + app.grid.attach( + &day_entry(app.tx.clone(), &date, &app.today, &app.events), + col, + row, + 1, + 1, + ) +} + fn day_title(col: i32) -> gtk::Box { let vbox = gtk::Box::builder() .orientation(gtk::Orientation::Vertical) diff --git a/src/app/form.rs b/src/app/form.rs index fc3dc83..08a2af1 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -6,10 +6,11 @@ use gtk::glib; use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; +use uuid::Uuid; use crate::{app::update, app::update::Msg, db, model::event}; -pub async fn dialog( +pub async fn show( conn: Rc, tx: Sender, window: Rc, @@ -49,7 +50,7 @@ pub async fn dialog( let button = gtk::Button::with_label("Créer"); vbox.append(&button); button.connect_clicked(glib::clone!(@weak dialog => move |_| { - match event::validate(date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { + match event::validate(Uuid::new_v4(), date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text()) { Some(event) => { match db::insert(&conn, &event) { Ok(_) => { diff --git a/src/app/update.rs b/src/app/update.rs index 288ec51..e7bf7af 100644 --- a/src/app/update.rs +++ b/src/app/update.rs @@ -1,16 +1,12 @@ -use gtk4 as gtk; - use async_channel::{Receiver, Sender}; use chrono::NaiveDate; -use gtk::prelude::*; use rusqlite::Connection; use std::rc::Rc; -use crate::app::calendar; -use crate::app::form; -use crate::app::utils; -use crate::app::App; -use crate::model::event::Event; +use crate::{ + app::{calendar, form, utils, App}, + model::event::Event, +}; pub fn send(tx: Sender, msg: Msg) { utils::spawn(async move { @@ -27,25 +23,12 @@ pub async fn event_handler(conn: Rc, rx: Receiver, tx: Sender { - form::dialog(Rc::clone(&conn), tx.clone(), Rc::clone(&app.window), date).await; + form::show(Rc::clone(&conn), tx.clone(), Rc::clone(&app.window), date).await; } Msg::AddEvent { event } => { let date = event.date.clone(); - - let d = date.signed_duration_since(app.start_date).num_days(); - app.events.push(event); - - let col = (d % 7) as i32; - let row = 1 + (d / 7) as i32; - - app.grid.attach( - &calendar::day_entry(tx.clone(), &date, &app.today, &app.events), - col, - row, - 1, - 1, - ); + calendar::refresh_date(&app, date); } } } -- cgit v1.2.3