From 80fdf874ac74efea64c7a21937ee790a4d871163 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 18 Dec 2022 20:02:42 +0100 Subject: Fix changing week After changing 1 week, we could not move anymore afterward. I didn’t find the source of the bug, but after removing one row from the grid, we lost the key event handler, albeit the application still working fine otherwise. --- src/gui/update.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/gui/update.rs b/src/gui/update.rs index 7b3625c..a4b36f3 100644 --- a/src/gui/update.rs +++ b/src/gui/update.rs @@ -1,6 +1,5 @@ use async_channel::{Receiver, Sender}; use chrono::{Duration, NaiveDate}; -use gtk4::prelude::GridExt; use std::collections::HashSet; use std::iter::FromIterator; use uuid::Uuid; @@ -166,8 +165,6 @@ pub async fn event_handler(rx: Receiver, mut app: App) { refresh(&app, &HashSet::from([date])) } Msg::SelectPreviousWeek => { - app.calendar.remove_row(4); - app.calendar.insert_row(1); app.start_date -= Duration::days(7); app.end_date -= Duration::days(7); @@ -179,11 +176,9 @@ pub async fn event_handler(rx: Receiver, mut app: App) { Ok(events) => app.events = events, Err(err) => eprintln!("{}", err), }; - refresh(&app, &HashSet::from_iter(week_from(app.start_date))); + refresh(&app, &HashSet::from_iter(visible_dates(&app))); } Msg::SelectNextWeek => { - app.calendar.remove_row(1); - app.calendar.insert_row(4); app.start_date += Duration::days(7); app.end_date += Duration::days(7); @@ -195,10 +190,7 @@ pub async fn event_handler(rx: Receiver, mut app: App) { Ok(events) => app.events = events, Err(err) => eprintln!("{}", err), }; - refresh( - &app, - &HashSet::from_iter(week_from(app.end_date - Duration::days(6))), - ); + refresh(&app, &HashSet::from_iter(visible_dates(&app))); } } } @@ -283,16 +275,24 @@ fn refresh(app: &App, dates: &HashSet) { for date in dates { if date >= &app.start_date && date <= &app.end_date { - calendar::refresh_date(app, *date, &repetitions, &app.categories, &app.default_color) + calendar::refresh_date( + app, + *date, + &repetitions, + &app.categories, + &app.default_color, + ) } } } -/// Seven days vector from the given date. -fn week_from(date: NaiveDate) -> Vec { +/// List visible dates in calendar. +fn visible_dates(app: &App) -> Vec { + let mut date = app.start_date; let mut res = vec![date]; - for i in 1..=6 { - res.push(date + Duration::days(i)) + while date <= app.end_date { + date += Duration::days(1); + res.push(date); } res } -- cgit v1.2.3