Commit d93251a9 authored by Julian Sparber's avatar Julian Sparber
Browse files

debian: add 'timers: Make timers persisted' patches

parent e0d8a488
From c0a0f14bc39982fe670fe6fa4677fd87326abdd0 Mon Sep 17 00:00:00 2001
From: Julian Sparber <julian@sparber.net>
Date: Thu, 26 Nov 2020 16:45:49 +0100
Subject: [PATCH] timers: persist running timers
Persist timer state between app starts by storing the remaining
time and the timestamp of when it was started.
---
src/timer-face.vala | 2 ++
src/timer-item.vala | 69 +++++++++++++++++++++++++++++++++++++--------
src/timer-row.vala | 15 ++++++++--
3 files changed, 72 insertions(+), 14 deletions(-)
diff --git a/src/timer-face.vala b/src/timer-face.vala
index 95aac80..581042f 100644
--- a/src/timer-face.vala
+++ b/src/timer-face.vala
@@ -56,6 +56,8 @@ public class Face : Gtk.Stack, Clocks.Clock {
((Item)timer).ring.connect (() => ring ());
((Item)timer).notify["state"].connect (() => {
this.is_running = this.get_total_active_timers () != 0;
+ /* We need to save state changes */
+ save ();
});
return row;
});
diff --git a/src/timer-item.vala b/src/timer-item.vala
index b226d83..e2a55a5 100644
--- a/src/timer-item.vala
+++ b/src/timer-item.vala
@@ -41,6 +41,7 @@ public class Item : Object, ContentItem {
private int stored_minute;
private int stored_second;
+ private GLib.DateTime? start_time;
public signal void ring ();
public signal void countdown_updated (int hours, int minutes, int seconds);
@@ -52,6 +53,12 @@ public class Item : Object, ContentItem {
public void serialize (GLib.VariantBuilder builder) {
builder.open (new GLib.VariantType ("a{sv}"));
builder.add ("{sv}", "duration", new GLib.Variant.int32 (get_total_seconds ()));
+ if (span > 0) {
+ builder.add ("{sv}", "time_left", new GLib.Variant.int32 ((int32) Math.ceil (span)));
+ }
+ if (start_time != null) {
+ builder.add ("{sv}", "start_time", new GLib.Variant.int64 (((!) start_time).to_unix ()));
+ }
if (name != null) {
builder.add ("{sv}", "name", new GLib.Variant.string ((string) name));
}
@@ -63,10 +70,18 @@ public class Item : Object, ContentItem {
Variant val;
int duration = 0;
string? name = null;
+ int span = 0;
+ GLib.DateTime? start_time = null;
var iter = time_variant.iterator ();
while (iter.next ("{sv}", out key, out val)) {
switch (key) {
+ case "time_left":
+ span = (int32) val;
+ break;
+ case "start_time":
+ start_time = new GLib.DateTime.from_unix_local ((int64) val);
+ break;
case "duration":
duration = (int32) val;
break;
@@ -76,33 +91,63 @@ public class Item : Object, ContentItem {
}
}
- return duration != 0 ? (Item?) new Item.from_seconds (duration, name) : null;
+ return duration != 0 ? (Item?) new Item.from_seconds (duration, name, start_time, (double) span) : null;
}
- public Item.from_seconds (int seconds, string? name) {
-
+ public Item.from_seconds (int seconds,
+ string? name,
+ GLib.DateTime? start_time = null,
+ double time_left = 0) {
int rest = 0;
int h = seconds / 3600;
rest = seconds - h * 3600;
int m = rest / 60;
int s = rest - m * 60;
- this (h, m, s, name);
+ this (h, m, s, name, start_time, time_left);
}
- public Item (int h, int m, int s, string? name) {
+ public Item (int h,
+ int m,
+ int s,
+ string? name,
+ GLib.DateTime? start_time = null,
+ double time_left = 0) {
Object (name: name);
hours = h;
minutes = m;
seconds = s;
- span = get_total_seconds ();
timer = new GLib.Timer ();
- timeout_id = 0;
+ if (start_time != null) {
+ this.start_time = start_time;
+ start ();
+ } else if (time_left > 0) {
+ this.span = time_left;
+ state = State.PAUSED;
+ }
+ }
+
+ public void update_countdown () {
+ int h, m, s;
+ if (state == State.STOPPED) {
+ countdown_updated (hours, minutes, seconds);
+ }else {
+ Utils.time_to_hms (span, out h, out m, out s, null);
+ countdown_updated (h, m, s);
+ }
}
public virtual signal void start () {
+ if (start_time == null) {
+ start_time = new GLib.DateTime.now ();
+ if (span == 0)
+ span = get_total_seconds ();
+ } else {
+ span = get_total_seconds () - new GLib.DateTime.now ().difference ((!) start_time) / TimeSpan.SECOND;
+ }
+
state = State.RUNNING;
timeout_id = GLib.Timeout.add (100, () => {
var e = timer.elapsed ();
@@ -134,16 +179,18 @@ public class Item : Object, ContentItem {
}
public virtual signal void pause () {
- state = State.PAUSED;
+ start_time = null;
span -= timer.elapsed ();
timer.stop ();
+ state = State.PAUSED;
}
public virtual signal void reset () {
- state = State.STOPPED;
- span = get_total_seconds ();
+ start_time = null;
+ span = 0;
timer.reset ();
- timeout_id = 0;
+ state = State.STOPPED;
+ update_countdown ();
}
}
diff --git a/src/timer-row.vala b/src/timer-row.vala
index d26ebce..b6d6847 100644
--- a/src/timer-row.vala
+++ b/src/timer-row.vala
@@ -75,7 +75,18 @@ public class Row : Gtk.ListBoxRow {
item.reset.connect (() => this.reset ());
delete_button.clicked.connect (() => deleted ());
- reset ();
+ item.update_countdown ();
+ switch (item.state) {
+ case STOPPED:
+ reset ();
+ break;
+ case RUNNING:
+ this.start ();
+ break;
+ case PAUSED:
+ this.pause ();
+ break;
+ }
}
[GtkCallback]
@@ -102,8 +113,6 @@ public class Row : Gtk.ListBoxRow {
countdown_label.get_style_context ().remove_class ("timer-running");
start_stack.visible_child_name = "start";
name_stack.visible_child_name = "edit";
-
- update_countdown (item.hours, item.minutes, item.seconds);
}
private void start () {
--
2.29.2
......@@ -21,3 +21,5 @@
#replace GSound with Libfeedback: https://gitlab.gnome.org/GNOME/gnome-clocks/-/merge_requests/151
0001-timer-Use-name-that-can-be-added-to-the-event-naming.patch
0001-Use-libfeedback-in-instead-of-GSound.patch
# timers: Make timers persisted: https://gitlab.gnome.org/GNOME/gnome-clocks/-/merge_requests/148
0001-timers-persist-running-timers.patch
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment