diff options
author | Ricky Kresslein <rk@lakoliu.com> | 2023-06-04 17:58:41 +0200 |
---|---|---|
committer | Ricky Kresslein <rk@lakoliu.com> | 2023-06-04 17:58:41 +0200 |
commit | c7d283c483a24bccc501a8a66cfd2a60b43ffb0f (patch) | |
tree | f683013bdecd9e509d2b1ef7401fa31f4183936c /src/ui | |
parent | 3cbbabd4aa35342b36dce327794149f0c5704508 (diff) | |
download | Furtherance-c7d283c483a24bccc501a8a66cfd2a60b43ffb0f.tar.zst |
#97: Add 'This Week' and 'Last Week' to reports. Add start of week picker to settings.
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/preferences_window.rs | 5 | ||||
-rw-r--r-- | src/ui/report.rs | 36 |
2 files changed, 38 insertions, 3 deletions
diff --git a/src/ui/preferences_window.rs b/src/ui/preferences_window.rs index bd135de..301250b 100644 --- a/src/ui/preferences_window.rs +++ b/src/ui/preferences_window.rs @@ -76,6 +76,9 @@ mod imp { pub inclusive_total_switch: TemplateChild<gtk::Switch>, #[template_child] + pub week_start_combo: TemplateChild<adw::ComboRow>, + + #[template_child] pub database_loc_row: TemplateChild<adw::ActionRow>, #[template_child] pub database_browse_btn: TemplateChild<gtk::Button>, @@ -186,6 +189,8 @@ impl FurPreferencesWindow { settings_manager::bind_property("inclusive-total", &*imp.inclusive_total_switch, "active"); + settings_manager::bind_property("week-starts", &*imp.week_start_combo, "selected"); + imp.dark_theme_switch.connect_active_notify(move |_| { let app = FurtheranceApplication::default(); app.update_light_dark(); diff --git a/src/ui/report.rs b/src/ui/report.rs index a830b05..6bc35d0 100644 --- a/src/ui/report.rs +++ b/src/ui/report.rs @@ -15,13 +15,14 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. use adw::subclass::prelude::*; -use chrono::{offset::TimeZone, Date, DateTime, Datelike, Duration, Local, NaiveDate}; +use chrono::{offset::TimeZone, Date, DateTime, Datelike, Duration, Local, NaiveDate, Weekday}; use gettextrs::*; use glib::clone; use gtk::{glib, prelude::*, CompositeTemplate}; use itertools::Itertools; use crate::database::{self, SortOrder, TaskSort}; +use crate::settings_manager; use crate::ui::FurtheranceWindow; use crate::FurtheranceApplication; @@ -115,7 +116,7 @@ impl FurReport { pub fn setup_widgets(&self) { let imp = imp::FurReport::from_obj(self); - imp.range_combo.set_active_id(Some("week_item")); + imp.range_combo.set_active_id(Some("this_week_item")); imp.filter_combo.set_active_id(Some("tasks_item")); imp.range_combo @@ -185,7 +186,13 @@ impl FurReport { let today = Local::today(); let range_start_date: Date<Local>; let mut range_end_date = today; - if active_range == "week_item" { + if active_range == "this_week_item" { + let current_week = today.iso_week().week(); + (range_start_date, range_end_date) = FurReport::week_bounds(today.weekday(), current_week); + } else if active_range == "last_week_item" { + let last_week = today.iso_week().week() - 1; + (range_start_date, range_end_date) = FurReport::week_bounds(today.weekday(), last_week); + } else if active_range == "week_item" { range_start_date = today - Duration::days(6); } else if active_range == "month_item" { let days_ago = today.day() - 1; @@ -468,6 +475,29 @@ impl FurReport { imp.results_tree.expand_row(&all_tasks_path, false); } + fn week_bounds(weekday: Weekday, mut week: u32) -> (Date<Local>, Date<Local>) { + let mut week_starts_on = Weekday::Mon; + let mut week_ends_on = Weekday::Sun; + // If user set week to start Sunday + if settings_manager::get_int("week-starts") == 1 { + week_starts_on = Weekday::Sun; + week_ends_on = Weekday::Sat; + } + + let current_year = chrono::offset::Local::now().year(); + let week_start = NaiveDate::from_isoywd(current_year, week, week_starts_on); + + // If weekday start Sunday, add 1 to the week number only after getting the week start date + if settings_manager::get_int("week-starts") == 1 && weekday == Weekday::Sun { + week += 1; + } + + let week_end = NaiveDate::from_isoywd(current_year, week, week_ends_on); + let week_start_local = Local.from_local_date(&week_start).unwrap(); + let week_end_local = Local.from_local_date(&week_end).unwrap(); + (week_start_local, week_end_local) + } + fn format_duration(total_time: i64) -> String { // Format total time to readable string let h = total_time / 3600; |