aboutsummaryrefslogtreecommitdiff
path: root/src/gui/form/mod.rs
blob: 0b63dea88a5f424284e7d9dc796275e5ba884355 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
mod repetition;
mod utils;

use gtk4 as gtk;

use anyhow::Result;
use chrono::{Duration, NaiveDate};
use gtk::glib;
use gtk::prelude::*;
use rusqlite::Connection;
use std::collections::HashSet;
use thiserror::Error;
use uuid::Uuid;

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

pub async fn repetition_dialog(app: &App, date: NaiveDate, event: &Event) {
    let dialog = gtk::Dialog::builder()
        .transient_for(&*app.window)
        .modal(true)
        .title("Modifier")
        .css_classes(vec!["g-Dialog".to_string()])
        .build();

    let content_area = dialog.content_area();

    let lines = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .build();
    content_area.append(&lines);

    let button = gtk::Button::builder()
        .label("Cette occurence")
        .margin_bottom(10)
        .build();
    lines.append(&button);
    let tx = app.tx.clone();
    button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| {
        update::send(tx.clone(), Msg::ShowUpdateRepetitionForm { date, event_id: event.id });
        dialog.close()
    }));

    let button = gtk::Button::builder()
        .label("Toutes les occurences")
        .margin_bottom(10)
        .build();
    lines.append(&button);
    let tx = app.tx.clone();
    button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| {
        update::send(tx.clone(), Msg::ShowUpdateForm { event_id: event.id });
        dialog.close()
    }));

    let button = gtk::Button::builder()
        .label("À partir de cette occurence")
        .build();
    lines.append(&button);
    let tx = app.tx.clone();
    button.connect_clicked(glib::clone!(@weak dialog, @strong event => move |_| {
        update::send(tx.clone(), Msg::ShowUpdateFromOccurence { date, event_id: event.id });
        dialog.close()
    }));

    dialog.run_future().await;
}

#[derive(Clone)]
pub enum Target {
    New { date: NaiveDate },
    Update { event: Event },
    UpdateRepetition { event: Event, date: NaiveDate },
    UpdateFromOccurence { event: Event, date: NaiveDate },
}

pub async fn show(app: &App, target: Target) {
    let event = match target {
        Target::New { .. } => None,
        Target::Update { ref event } => Some(event.clone()),
        Target::UpdateRepetition { ref event, .. } => Some(event.clone()),
        Target::UpdateFromOccurence { ref event, .. } => Some(event.clone()),
    };

    let title = if event.is_some() {
        "Modifier"
    } else {
        "Ajouter"
    };

    let dialog = gtk::Dialog::builder()
        .transient_for(&*app.window)
        .modal(true)
        .title(title)
        .css_classes(vec!["g-Dialog".to_string()])
        .build();

    let content_area = dialog.content_area();

    let lines = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .build();
    content_area.append(&lines);

    let columns = gtk::Box::builder()
        .orientation(gtk::Orientation::Horizontal)
        .spacing(10)
        .build();
    lines.append(&columns);

    // First column

    let column1 = gtk::Box::builder()
        .orientation(gtk::Orientation::Vertical)
        .build();
    columns.append(&column1);

    let name = event
        .as_ref()
        .map(|e| utils::entry(&e.name))
        .unwrap_or_default();
    column1.append(&utils::label("Événement"));
    column1.append(&name);

    let date = match target {
        Target::New { date } => date,
        Target::Update { ref event } => event.date,
        Target::UpdateRepetition { date, .. } => date,
        Target::UpdateFromOccurence { date, .. } => date,
    };
    let date = utils::entry(&date.format(event::DATE_FORMAT).to_string());
    column1.append(&utils::label("Jour"));
    column1.append(&date);

    let start = event
        .as_ref()
        .map(|e| utils::time_entry(e.start))
        .unwrap_or_else(|| utils::entry(""));
    column1.append(&utils::label("Début"));
    column1.append(&start);

    let end = event
        .as_ref()
        .map(|e| utils::time_entry(e.end))
        .unwrap_or_else(|| utils::entry(""));
    column1.append(&utils::label("Fin"));
    column1.append(&end);

    let dropdown_categories = get_dropdown_categories(&app.categories);
    let category_dropdown = gtk::DropDown::from_strings(
        &dropdown_categories
            .iter()
            .map(|s| s.as_str())
            .collect::<Vec<&str>>(),
    );
    category_dropdown.set_margin_bottom(10);
    let selected = get_selected_category(&event, &app.categories).unwrap_or_else(|| "".to_string());
    category_dropdown.set_selected(
        dropdown_categories
            .iter()
            .position(|d| d == &selected)
            .unwrap_or(0) as u32,
    );
    column1.append(&utils::label("Catégorie"));
    column1.append(&category_dropdown);

    // Second column

    let repetition = match target {
        Target::Update { ref event } => event.repetition.as_ref(),
        Target::UpdateFromOccurence { ref event, .. } => event.repetition.as_ref(),
        _ => None,
    };
    let repetition_model = repetition::view(repetition);
    columns.append(&repetition_model.view);

    // Buttons

    let button_title = match target {
        Target::New { .. } => "Créer",
        Target::Update { .. } => "Modifier",
        Target::UpdateRepetition { .. } => "Modifier l’occurence",
        Target::UpdateFromOccurence { .. } => "Modifier à partir de l’occurence",
    };

    let button = gtk::Button::builder()
        .label(button_title)
        .margin_bottom(10)
        .build();
    lines.append(&button);
    let conn = app.conn.clone();
    let tx = app.tx.clone();
    let categories = app.categories.clone();
    button.connect_clicked(glib::clone!(@weak dialog, @strong target, @strong event, @strong categories => move |_| {
        let removed_occurences = match &target {
            Target::Update { event, .. } => {
                event.repetition.as_ref().map(|r| r.removed_occurences.clone()).unwrap_or_default()
            },
            _ => HashSet::new(),
        };
        match repetition::validate(&repetition_model, removed_occurences) {
            Ok(repetition) => {
                let id = match &target {
                    Target::Update {event} => event.id,
                    _ => Uuid::new_v4(),
                };

                // Find category id from selected id
                let category = categories.iter().find(|c| c.name == dropdown_categories[category_dropdown.selected() as usize]).map(|c| c.id);

                match event::validate(id, date.buffer().text(), name.buffer().text(), start.buffer().text(), end.buffer().text(), repetition, category) {
                    Some(new) => {
                        match &target {
                            Target::New {..} => {
                                match db::events::insert(&conn, &new) {
                                    Ok(_) => {
                                        update::send(tx.clone(), Msg::AddEvent { new });
                                        dialog.close()
                                    },
                                    Err(err) => eprintln!("Error when inserting event: {}", err)
                                }
                            }
                            Target::Update {event} => {
                                match db::events::update(&conn, &new) {
                                    Ok(_) => {
                                        update::send(tx.clone(), Msg::UpdateEvent { old: event.clone(), new });
                                        dialog.close()
                                    },
                                    Err(err) => eprintln!("Error when updating event: {}", err)
                                }
                            }
                            Target::UpdateRepetition { event, date } => {
                                // TODO: improve intermediate error state
                                match delete_repetition_occurence(&conn, event, *date) {
                                    Ok(occurence) => {
                                        match db::events::insert(&conn, &new) {
                                            Ok(_) => {
                                                update::send(tx.clone(), Msg::UpdateEventOccurence {
                                                    event: event.clone(),
                                                    occurence,
                                                    date: *date,
                                                    new
                                                })
                                            }
                                            Err(err) => eprintln!("Error when updating repetition: {}", err)
                                        };
                                        dialog.close()
                                    },
                                    Err(err) => eprintln!("Error when updating repetition: {}", err)
                                }
                            }
                            Target::UpdateFromOccurence { date, event } => {
                                match update_repetition_until(&conn, *date - Duration::days(1), event) {
                                    Ok(updated) => {
                                        match db::events::insert(&conn, &new) {
                                            Ok(_) => {
                                                update::send(tx.clone(), Msg::UpdateRepeatedFrom {
                                                    old: event.clone(),
                                                    updated,
                                                    new
                                                });
                                                dialog.close()
                                            },
                                            Err(err) => eprintln!("Error when inserting event: {}", err)
                                        }
                                    },
                                    Err(err) => eprintln!("Error when updating event: {}", err)
                                }
                            }
                        }
                    }
                    None => eprintln!("Event is not valid.")
                }
            },
            Err(message) => eprintln!("{}", message)
        }
    }));

    if let Some(event) = event {
        let label = match target {
            Target::Update { .. } => "Supprimer",
            Target::UpdateFromOccurence { .. } => "Supprimer à partir de l’occurence",
            _ => "Supprimer l’occurence",
        };
        let button = gtk::Button::builder().label(label).build();
        lines.append(&button);
        let conn = app.conn.clone();
        let tx = app.tx.clone();
        button.connect_clicked(glib::clone!(@weak dialog => move |_| {
            match target {
                Target::UpdateRepetition { date, .. } => {
                    match delete_repetition_occurence(&conn, &event, date) {
                        Ok(occurence) => {
                            update::send(tx.clone(), Msg::DeleteOccurence { event: event.clone(), date, occurence });
                            dialog.close()
                        }
                        Err(err) => {
                            eprintln!("{:?}", err);
                        }
                    }
                }
                Target::UpdateFromOccurence { date, .. } => {
                    match update_repetition_until(&conn, date - Duration::days(1), &event) {
                        Ok(updated) => {
                            update::send(tx.clone(), Msg::UpdateEvent { old: event.clone(), new: updated });
                            dialog.close()
                        },
                        Err(err) => eprintln!("Error when updating event: {}", err)
                    }
                }
                _ => {
                    let operation = db::events::delete(&conn, &event.id);
                    if operation.is_ok() {
                        update::send(tx.clone(), Msg::DeleteEvent { event: event.clone() });
                        dialog.close()
                    }
                }
            }
        }));
    }

    dialog.run_future().await;
}

fn get_dropdown_categories(categories: &[Category]) -> Vec<String> {
    let mut xs = categories
        .iter()
        .map(|c| c.name.clone())
        .collect::<Vec<String>>();
    xs.push("".to_string());
    xs.sort();
    xs
}

fn get_selected_category(event: &Option<Event>, categories: &[Category]) -> Option<String> {
    let id = event.as_ref()?.category?;
    let category = categories.iter().find(|c| c.id == id)?;
    Some(category.name.clone())
}

#[derive(Error, Debug)]
enum DeleteError {
    #[error("Repetition not found")]
    RepetitionNotFound,
    #[error("Occurence not found")]
    OccurenceNotFound,
}

fn delete_repetition_occurence(
    conn: &Connection,
    event: &Event,
    occurence_date: NaiveDate,
) -> Result<usize> {
    if let Some(ref repetition) = event.repetition {
        if let Some(occurence) = repetition.occurence_index(event.date, occurence_date) {
            let mut event = event.clone();
            let mut repetition = repetition.clone();
            repetition.removed_occurences.insert(occurence);
            event.repetition = Some(repetition);
            db::events::update(conn, &event).map(|_| occurence)
        } else {
            Err(anyhow::Error::new(DeleteError::OccurenceNotFound))
        }
    } else {
        Err(anyhow::Error::new(DeleteError::RepetitionNotFound))
    }
}

fn update_repetition_until(conn: &Connection, date: NaiveDate, event: &Event) -> Result<Event> {
    let mut with_repetition_until = event.clone();
    with_repetition_until.repetition = event.repetition.as_ref().map(|r| Repetition {
        frequency: r.frequency.clone(),
        removed_occurences: r.removed_occurences.clone(),
        until: Some(date),
    });
    db::events::update(conn, &with_repetition_until)?;
    Ok(with_repetition_until)
}