Skip to content

Commit f23b955

Browse files
committed
refactor: Reset action (Vala)
1 parent b3c206c commit f23b955

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

data/xyz.safeworlds.hiit.gschema.xml.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
<default>"[]"</default>
2222
<summary>List of training setups, in JSON notation</summary>
2323
</key>
24-
<key name="warmup-s" type="u">
24+
<key name="countdown-sec" type="u">
2525
<default>5</default>
26-
<summary>The duration of the warmup, in seconds</summary>
26+
<summary>The duration of the audio countdown, in seconds</summary>
2727
</key>
2828
<key name="beep-volume" type="d">
2929
<default>1</default>

src/Application.vala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ namespace ExerciseTimer {
1212
{ "about", this.on_about_action },
1313
{ "close", this.on_close_action },
1414
{ "shortcuts", this.on_shortcuts_action },
15+
{ "restart", this.on_restart_action },
1516
{ "quit", this.quit },
1617
};
1718
this.add_action_entries (action_entries, this);
1819
this.set_accels_for_action ("app.close", { "<primary>w" });
1920
this.set_accels_for_action ("app.shortcuts", { "<primary>question" });
21+
this.set_accels_for_action ("app.restart", { "<primary>r" });
2022
this.set_accels_for_action ("app.quit", { "<primary>q" });
2123
}
2224

@@ -69,5 +71,10 @@ namespace ExerciseTimer {
6971
var dialog = builder.get_object ("shortcuts_dialog") as Adw.ShortcutsDialog;
7072
dialog.present (this.get_active_window ());
7173
}
74+
75+
private void on_restart_action () {
76+
var window = this.active_window as ExerciseTimer.Window;
77+
window?.on_restart_action ();
78+
}
7279
}
7380
}

src/ITimerPageActionNotifier.vala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace ExerciseTimer {
2+
public interface ITimerPageActionNotifier : Object {
3+
public signal void restart_action_called ();
4+
}
5+
}

src/TimerPage.vala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
namespace ExerciseTimer {
22
[GtkTemplate(ui = "/xyz/safeworlds/hiit/ui/timer_page.ui")]
33
public class TimerPage : Adw.NavigationPage, ITimerNotifier {
4-
public TimerPage(TrainingSetup setup) {
4+
public TimerPage(TrainingSetup setup, ITimerPageActionNotifier action_notifier) {
55
Setup = setup;
6+
this.action_notifier = action_notifier;
67
updateCssClass();
78
timer_label_box.set_direction(Gtk.TextDirection.LTR);
89
volume_button.get_first_child().css_classes = new string[] { "circular", "toggle", "large-button" };
910

1011
var settings = new GLib.Settings(Config.AppId);
1112
settings.bind("beep-volume", volume_adjustment, "value", GLib.SettingsBindFlags.DEFAULT);
13+
settings.bind("countdown-sec", this, "CountdownSec", GLib.SettingsBindFlags.DEFAULT);
1214

1315
this.shown.connect(() => {
1416
restart();
1517
});
1618
this.hidden.connect((_) => {
1719
Running = false;
20+
this.action_notifier.restart_action_called.disconnect(restart);
1821
});
22+
this.action_notifier.restart_action_called.connect(restart);
1923
}
2024

2125
public TrainingSetup Setup { get; private set; }
@@ -108,6 +112,8 @@ namespace ExerciseTimer {
108112
}
109113
}
110114

115+
public int CountdownSec { get; set; }
116+
111117
[GtkCallback]
112118
public void restart() {
113119
remaining_sets = Setup.Sets;
@@ -136,7 +142,7 @@ namespace ExerciseTimer {
136142
if (remaining_sec > 1) {
137143
--remaining_sec;
138144
retval = true;
139-
if (remaining_sec <= countdown_threshold) {
145+
if (remaining_sec <= CountdownSec) {
140146
countdown(remaining_sec);
141147
}
142148
} else {
@@ -229,6 +235,6 @@ namespace ExerciseTimer {
229235
private int remaining_sets;
230236
private uint? timer_id;
231237
private bool running;
232-
private const int countdown_threshold = 5;
238+
ITimerPageActionNotifier action_notifier;
233239
}
234240
}

src/Window.vala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace ExerciseTimer {
22

33
[GtkTemplate(ui = "/xyz/safeworlds/hiit/ui/window.ui")]
4-
public class Window : Adw.ApplicationWindow {
4+
public class Window : Adw.ApplicationWindow, ITimerPageActionNotifier {
55
public Window(Gtk.Application app) {
66
Object(application: app);
77

@@ -13,6 +13,10 @@ namespace ExerciseTimer {
1313
training_list_stack.set_visible_child(training_list_status);
1414
}
1515

16+
public void on_restart_action () {
17+
restart_action_called();
18+
}
19+
1620
[GtkCallback]
1721
private void on_add_training() {
1822
var editor_dialog = new TrainingEditor(default_setup);
@@ -38,7 +42,7 @@ namespace ExerciseTimer {
3842
private void on_training_activated(Gtk.ListBoxRow row) {
3943
var training_list_row = row as TrainingListRow;
4044
var setup = training_list_row.Setup;
41-
var timer_page = new TimerPage(setup);
45+
timer_page = new TimerPage(setup, this);
4246
navigation_view.push(timer_page);
4347
voices = new Voices(timer_page);
4448
}
@@ -55,6 +59,7 @@ namespace ExerciseTimer {
5559
private unowned Gtk.ListBox training_listbox;
5660

5761
private Voices voices;
62+
private TimerPage timer_page;
5863

5964
private static TrainingSetup default_setup = new TrainingSetup(){ Title = "Exercise", WarmupSec = 5, ExerciseSec = 30, RestSec = 10, Sets = 4 };
6065
}

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ config_source = configure_file(
2727
sources = [
2828
'Application.vala',
2929
'ITimerNotifier.vala',
30+
'ITimerPageActionNotifier.vala',
3031
'TimerPage.vala',
3132
'TrainingEditor.vala',
3233
'TrainingListRow.vala',

0 commit comments

Comments
 (0)