From e06420480258cda94e20b482529c788efab83a2f Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Thu, 12 Nov 2020 13:38:07 +0100 Subject: [PATCH 04/16] alarm-item: Make sure that the time property is always set The time property for an AlarmItem isn't nullable therefore it needs to be set during construction. --- src/alarm-face.vala | 18 +++++++---- src/alarm-item.vala | 12 ++++---- src/alarm-setup-dialog.vala | 59 +++++++++++-------------------------- 3 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/alarm-face.vala b/src/alarm-face.vala index 0fe77cb..cf9a592 100644 --- a/src/alarm-face.vala +++ b/src/alarm-face.vala @@ -108,7 +108,7 @@ public class Face : Gtk.Stack, Clocks.Clock { } internal void edit (Item alarm) { - var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), alarm, alarms); + var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), alarm, alarms, true); // Disable alarm while editing it and remember the original active state. alarm.editing = true; @@ -116,7 +116,7 @@ public class Face : Gtk.Stack, Clocks.Clock { dialog.response.connect ((dialog, response) => { alarm.editing = false; if (response == Gtk.ResponseType.OK) { - ((SetupDialog) dialog).apply_to_alarm (alarm); + ((SetupDialog) dialog).apply_to_alarm (); save (); } else if (response == DELETE_ALARM) { alarms.delete_item (alarm); @@ -137,11 +137,19 @@ public class Face : Gtk.Stack, Clocks.Clock { } public void activate_new () { - var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), null, alarms); + var wc = Utils.WallClock.get_default (); + var alarm = new Item ({ wc.date_time.get_hour (), wc.date_time.get_minute () }, false); + var dialog = new SetupDialog ((Gtk.Window) get_toplevel (), alarm, alarms); + + // Disable alarm while editing it and remember the original active state. + alarm.editing = true; + dialog.response.connect ((dialog, response) => { + alarm.editing = false; + // Enable the newly created alarm + alarm.active = true; if (response == Gtk.ResponseType.OK) { - var alarm = new Item (); - ((SetupDialog) dialog).apply_to_alarm (alarm); + ((SetupDialog) dialog).apply_to_alarm (); alarms.add (alarm); save (); } diff --git a/src/alarm-item.vala b/src/alarm-item.vala index 6103342..26f655f 100644 --- a/src/alarm-item.vala +++ b/src/alarm-item.vala @@ -104,9 +104,12 @@ private class Item : Object, ContentItem { private Utils.Bell bell; private GLib.Notification notification; - public Item (string? id = null) { + public Item (AlarmTime time, bool active = true, Utils.Weekdays? days = null, string? id = null) { var guid = id != null ? (string) id : GLib.DBus.generate_guid (); - Object (id: guid); + Object (id: guid, + active: active, + time: time, + days: days); } private void setup_bell () { @@ -271,11 +274,8 @@ private class Item : Object, ContentItem { } if (hour >= 0 && minute >= 0) { - Item alarm = new Item (id); + Item alarm = new Item ({ hour, minute }, active, days, id); alarm.name = name; - alarm.active = active; - alarm.time = { hour, minute }; - alarm.days = days; alarm.ring_minutes = ring_minutes; alarm.snooze_minutes = snooze_minutes; alarm.reset (); diff --git a/src/alarm-setup-dialog.vala b/src/alarm-setup-dialog.vala index 97cde1f..d20ec61 100644 --- a/src/alarm-setup-dialog.vala +++ b/src/alarm-setup-dialog.vala @@ -75,6 +75,7 @@ private class DurationModel : ListModel, Object { [GtkTemplate (ui = "/org/gnome/clocks/ui/alarm-setup-dialog.ui")] private class SetupDialog : Gtk.Dialog { private Utils.WallClock.Format format; + public Item alarm { get; set; } [GtkChild] private unowned Gtk.Grid time_grid; [GtkChild] @@ -103,20 +104,21 @@ private class SetupDialog : Gtk.Dialog { typeof (DayPickerRow).ensure (); } - public SetupDialog (Gtk.Window parent, Item? alarm, ListModel all_alarms) { + public SetupDialog (Gtk.Window parent, Item alarm, ListModel all_alarms, bool edit_alarm = false) { Object (transient_for: parent, - title: alarm != null ? _("Edit Alarm") : _("New Alarm"), + alarm: alarm, + title: edit_alarm ? _("Edit Alarm") : _("New Alarm"), use_header_bar: 1); add_button (_("Cancel"), Gtk.ResponseType.CANCEL); - if (alarm != null) { + if (edit_alarm) { add_button (_("Done"), Gtk.ResponseType.OK); } else { add_button (_("Add"), Gtk.ResponseType.OK); } set_default_response (Gtk.ResponseType.OK); - delete_button.visible = alarm != null; + delete_button.visible = edit_alarm; other_alarms = new List (); var n = all_alarms.get_n_items (); @@ -158,39 +160,13 @@ private class SetupDialog : Gtk.Dialog { am_pm_stack.visible_child = am_pm_button; } - set_from_alarm (alarm); + set_from_alarm (); } // Sets up the dialog to show the values of alarm. - public void set_from_alarm (Item? alarm) { - string? name; - bool active; - int hour; - int minute; - int snooze_minutes; - int ring_minutes; - unowned Utils.Weekdays? days; - - if (alarm == null) { - var wc = Utils.WallClock.get_default (); - // Not great but we can't null it - name = ""; - hour = wc.date_time.get_hour (); - 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; - } - + public void set_from_alarm () { + var hour = alarm.time.hour; + var minute = alarm.time.minute; // Set the time. if (format == Utils.WallClock.Format.TWELVE) { if (hour < 12) { @@ -204,22 +180,22 @@ 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)); + ring_duration.set_selected_index (duration_model.find_by_duration (alarm.ring_minutes)); + snooze_duration.set_selected_index (duration_model.find_by_duration (alarm.snooze_minutes)); h_spinbutton.set_value (hour); m_spinbutton.set_value (minute); // Set the name. - name_entry.set_text ((string) name); + name_entry.set_text ((string) alarm.name); - if (days != null) { - repeats.load ((Utils.Weekdays) days); + if (alarm.days != null) { + repeats.load ((Utils.Weekdays) alarm.days); } } // Sets alarm according to the current dialog settings. - public void apply_to_alarm (Item alarm) { + public void apply_to_alarm () { var name = name_entry.get_text (); var hour = h_spinbutton.get_value_as_int (); var minute = m_spinbutton.get_value_as_int (); @@ -254,8 +230,7 @@ private class SetupDialog : Gtk.Dialog { } private void avoid_duplicate_alarm () { - var alarm = new Item (); - apply_to_alarm (alarm); + apply_to_alarm (); var duplicate = alarm.check_duplicate_alarm (other_alarms); this.set_response_sensitive (Gtk.ResponseType.OK, !duplicate); -- 2.34.1