about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRicky Kresslein <rk@lakoliu.com>2023-06-04 17:58:41 +0200
committerRicky Kresslein <rk@lakoliu.com>2023-06-04 17:58:41 +0200
commitc7d283c483a24bccc501a8a66cfd2a60b43ffb0f (patch)
treef683013bdecd9e509d2b1ef7401fa31f4183936c
parent3cbbabd4aa35342b36dce327794149f0c5704508 (diff)
downloadFurtherance-c7d283c483a24bccc501a8a66cfd2a60b43ffb0f.tar.zst
#97: Add 'This Week' and 'Last Week' to reports. Add start of week picker to settings.
-rw-r--r--data/com.lakoliu.Furtherance.gschema.xml3
-rw-r--r--src/gtk/preferences_window.ui21
-rw-r--r--src/gtk/report.ui4
-rw-r--r--src/ui/preferences_window.rs5
-rw-r--r--src/ui/report.rs36
5 files changed, 64 insertions, 5 deletions
diff --git a/data/com.lakoliu.Furtherance.gschema.xml b/data/com.lakoliu.Furtherance.gschema.xml
index fd96c00..a321365 100644
--- a/data/com.lakoliu.Furtherance.gschema.xml
+++ b/data/com.lakoliu.Furtherance.gschema.xml
@@ -43,6 +43,9 @@
 	  <key name="inclusive-total" type="b">
       <default>false</default>
     </key>
+	  <key name="week-starts" type="i">
+      <default>0</default>
+    </key>
 	  <key name="database-loc" type="s">
       <default>"default"</default>
     </key>
diff --git a/src/gtk/preferences_window.ui b/src/gtk/preferences_window.ui
index 3b862c0..e916883 100644
--- a/src/gtk/preferences_window.ui
+++ b/src/gtk/preferences_window.ui
@@ -218,7 +218,26 @@
                 </child>
               </object>
             </child>
-
+          </object>
+        </child>
+        <child>
+          <object class="AdwPreferencesGroup" id="reports_group">
+            <property name="title" translatable="yes">Reports</property>
+            <property name="visible">True</property>
+            <child>
+              <object class="AdwComboRow" id="week_start_combo">
+                <property name="title" translatable="yes">Week starts on</property>
+                <property name="use_underline">True</property>
+                <property name="model">
+                  <object class="GtkStringList">
+                    <items>
+                      <item translatable="yes">Monday</item>
+                      <item translatable="yes">Sunday</item>
+                    </items>
+                  </object>
+                </property>
+              </object>
+            </child>
           </object>
         </child>
         <child>
diff --git a/src/gtk/report.ui b/src/gtk/report.ui
index 92ae072..db4b651 100644
--- a/src/gtk/report.ui
+++ b/src/gtk/report.ui
@@ -30,7 +30,9 @@
             <object class="GtkComboBoxText" id="range_combo">
               <property name="halign">center</property>
                 <items>
-                  <item translatable="yes" id="week_item">Past week</item>
+                  <item translatable="yes" id="this_week_item">This Week</item>
+                  <item translatable="yes" id="last_week_item">Last Week</item>
+                  <item translatable="yes" id="week_item">Past 7 days</item>
                   <item translatable="yes" id="month_item">This month</item>
                   <item translatable="yes" id="30_days_item">Past 30 days</item>
                   <item translatable="yes" id="six_months_item">Past 180 days</item>
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;