[gnome-clocks/bilelmoussaoui/snooze-ring-duration] alarm: allow to set ring/snooze duration




commit cf5fb025e6ae53b8607cf9b5f5092abca53d9085
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Wed Aug 5 23:59:26 2020 +0200

    alarm: allow to set ring/snooze duration
    
    fixes #46 #47

 data/ui/alarm-setup-dialog.ui | 16 +++++++++++
 src/alarm-item.vala           | 21 ++++++++++----
 src/alarm-setup-dialog.vala   | 64 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+), 5 deletions(-)
---
diff --git a/data/ui/alarm-setup-dialog.ui b/data/ui/alarm-setup-dialog.ui
index dc7e93f..0e353cc 100644
--- a/data/ui/alarm-setup-dialog.ui
+++ b/data/ui/alarm-setup-dialog.ui
@@ -153,6 +153,22 @@
                         </child>
                       </object>
                     </child>
+                    <child>
+                      <object class="HdyComboRow" id="ring_duration">
+                        <property name="visible">True</property>
+                        <property name="activatable">False</property>
+                        <property name="selectable">False</property>
+                        <property name="title" translatable="yes">Ring Duration</property>
+                      </object>
+                    </child>
+                    <child>
+                      <object class="HdyComboRow" id="snooze_duration">
+                        <property name="visible">True</property>
+                        <property name="activatable">False</property>
+                        <property name="selectable">False</property>
+                        <property name="title" translatable="yes">Snooze Duration</property>
+                      </object>
+                    </child>
                     <style>
                       <class name="content"/>
                       <class name="clocks-list"/>
diff --git a/src/alarm-item.vala b/src/alarm-item.vala
index 8849f38..8c86f78 100644
--- a/src/alarm-item.vala
+++ b/src/alarm-item.vala
@@ -26,9 +26,6 @@ private struct AlarmTime {
 }
 
 private class Item : Object, ContentItem {
-    const int SNOOZE_MINUTES = 9;
-    const int RING_MINUTES = 3;
-
     // FIXME: should we add a "MISSED" state where the alarm stopped
     // ringing but we keep showing the ringing panel?
     public enum State {
@@ -41,6 +38,10 @@ private class Item : Object, ContentItem {
 
     public string id { get; construct set; }
 
+    public int snooze_minutes { get; set; default = 10; }
+
+    public int ring_minutes { get; set; default = 5; }
+
     public string? name {
         get {
             return _name;
@@ -151,7 +152,7 @@ private class Item : Object, ContentItem {
     }
 
     private void update_snooze_time (GLib.DateTime start_time) {
-        snooze_time = start_time.add_minutes (SNOOZE_MINUTES);
+        snooze_time = start_time.add_minutes (snooze_minutes);
     }
 
     public virtual signal void ring () {
@@ -162,7 +163,7 @@ private class Item : Object, ContentItem {
 
     private void start_ringing (GLib.DateTime now) {
         update_snooze_time (now);
-        ring_end_time = now.add_minutes (RING_MINUTES);
+        ring_end_time = now.add_minutes (ring_minutes);
         state = State.RINGING;
         ring ();
     }
@@ -230,6 +231,8 @@ private class Item : Object, ContentItem {
         builder.add ("{sv}", "hour", new GLib.Variant.int32 (time.hour));
         builder.add ("{sv}", "minute", new GLib.Variant.int32 (time.minute));
         builder.add ("{sv}", "days", ((Utils.Weekdays) days).serialize ());
+        builder.add ("{sv}", "snooze_minutes", new GLib.Variant.int32 (snooze_minutes));
+        builder.add ("{sv}", "ring_minutes", new GLib.Variant.int32 (ring_minutes));
         builder.close ();
     }
 
@@ -241,6 +244,8 @@ private class Item : Object, ContentItem {
         bool active = true;
         int hour = -1;
         int minute = -1;
+        int snooze_minutes = 10;
+        int ring_minutes = 5;
         Utils.Weekdays? days = null;
 
         var iter = alarm_variant.iterator ();
@@ -257,6 +262,10 @@ private class Item : Object, ContentItem {
                 minute = (int32) val;
             } else if (key == "days") {
                 days = Utils.Weekdays.deserialize (val);
+            } else if (key == "snooze_minutes") {
+                snooze_minutes = (int32) val;
+            } else if (key == "ring_minutes") {
+                ring_minutes = (int32) val;
             }
         }
 
@@ -266,6 +275,8 @@ private class Item : Object, ContentItem {
             alarm.active = active;
             alarm.time = { hour, minute };
             alarm.days = days;
+            alarm.ring_minutes = ring_minutes;
+            alarm.snooze_minutes = snooze_minutes;
             alarm.reset ();
             return alarm;
         } else {
diff --git a/src/alarm-setup-dialog.vala b/src/alarm-setup-dialog.vala
index adee495..cb13467 100644
--- a/src/alarm-setup-dialog.vala
+++ b/src/alarm-setup-dialog.vala
@@ -16,6 +16,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
+using Hdy;
 
 namespace Clocks {
 namespace Alarm {
@@ -23,6 +24,40 @@ namespace Alarm {
 // Response used for the "Delete Alarm" button in the edit dialogue
 const int DELETE_ALARM = 2;
 
+private class Duration : Object {
+    public int minutes { get; set ; default = 0; }
+    public string label { get; set; }
+
+    public Duration (int minutes, string label) {
+        this.minutes = minutes;
+        this.label = label;
+    }
+}
+
+private class DurationModel : Object {
+    public ListStore store;
+
+    construct {
+        store = new ListStore (typeof (Duration));
+        store.append (new Duration (1, _("1 minute")));
+        store.append (new Duration (5, _("5 minutes")));
+        store.append (new Duration (10, _("10 minutes")));
+        store.append (new Duration (15, _("15 minutes")));
+        store.append (new Duration (20, _("20 minutes")));
+        store.append (new Duration (30, _("30 minutes")));
+    }
+
+    public int find_by_duration (int minutes) {
+        for (var i = 0; i < store.get_n_items (); i++) {
+            var d = (Duration) store.get_item (i);
+            if (d.minutes == minutes) {
+                return i;
+            }
+        }
+        return -1;
+    }
+}
+
 [GtkTemplate (ui = "/org/gnome/clocks/ui/alarm-setup-dialog.ui")]
 private class SetupDialog : Gtk.Dialog {
     private Utils.WallClock.Format format;
@@ -34,6 +69,10 @@ private class SetupDialog : Gtk.Dialog {
     private Gtk.SpinButton m_spinbutton;
     [GtkChild]
     private Gtk.Entry name_entry;
+    [GtkChild]
+    private Hdy.ComboRow snooze_duration;
+    [GtkChild]
+    private Hdy.ComboRow ring_duration;
     private AmPmToggleButton am_pm_button;
     [GtkChild]
     private DayPickerRow repeats;
@@ -44,6 +83,7 @@ private class SetupDialog : Gtk.Dialog {
     [GtkChild]
     private Gtk.Button delete_button;
     private List<Item> other_alarms;
+    private DurationModel duration_model;
 
     static construct {
         typeof (DayPickerRow).ensure ();
@@ -73,6 +113,16 @@ private class SetupDialog : Gtk.Dialog {
             }
         }
 
+        duration_model = new DurationModel ();
+
+        ring_duration.bind_name_model (duration_model.store, (item) => {
+            return ((Duration) item).label;
+        });
+
+        snooze_duration.bind_name_model (duration_model.store, (item) => {
+            return ((Duration) item).label;
+        });
+
         // Force LTR since we do not want to reverse [hh] : [mm]
         time_grid.set_direction (Gtk.TextDirection.LTR);
 
@@ -103,6 +153,8 @@ private class SetupDialog : Gtk.Dialog {
         bool active;
         int hour;
         int minute;
+        int snooze_minutes;
+        int ring_minutes;
         unowned Utils.Weekdays? days;
 
         if (alarm == null) {
@@ -113,12 +165,16 @@ private class SetupDialog : Gtk.Dialog {
             minute = wc.date_time.get_minute ();
             days = null;
             active = true;
+            ring_minutes = 5;
+            snooze_minutes = 10;
         } else {
             name = ((Item) alarm).name;
             hour = ((Item) alarm).time.hour;
             minute = ((Item) alarm).time.minute;
             days = ((Item) alarm).days;
             active = ((Item) alarm).active;
+            ring_minutes = ((Item) alarm).ring_minutes;
+            snooze_minutes = ((Item) alarm).snooze_minutes;
         }
 
         // Set the time.
@@ -134,6 +190,9 @@ private class SetupDialog : Gtk.Dialog {
                 hour = 12;
             }
         }
+        ring_duration.set_selected_index (duration_model.find_by_duration (ring_minutes));
+        snooze_duration.set_selected_index (duration_model.find_by_duration (snooze_minutes));
+
         h_spinbutton.set_value (hour);
         m_spinbutton.set_value (minute);
 
@@ -150,6 +209,9 @@ private class SetupDialog : Gtk.Dialog {
         var name = name_entry.get_text ();
         var hour = h_spinbutton.get_value_as_int ();
         var minute = m_spinbutton.get_value_as_int ();
+        var snooze_item = (Duration) duration_model.store.get_item (snooze_duration.get_selected_index ());
+        var ring_item = (Duration) duration_model.store.get_item (ring_duration.get_selected_index ());
+
         if (format == Utils.WallClock.Format.TWELVE) {
             var choice = am_pm_button.choice;
             if (choice == AmPmToggleButton.AmPm.AM && hour == 12) {
@@ -168,6 +230,8 @@ private class SetupDialog : Gtk.Dialog {
         alarm.name = name;
         alarm.time = time;
         alarm.days = days;
+        alarm.snooze_minutes = snooze_item.minutes;
+        alarm.ring_minutes = ring_item.minutes;
 
         // Force update of alarm_time before notifying the changes
         alarm.reset ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]