aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2023-07-13 16:04:55 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2023-07-13 16:04:55 -0500
commit8633fe4774a83f90969e965c02b35e531d2ac6df (patch)
tree47ab02f5cf9885d0a856fcdf4a92ae1d5e180891 /src
parent0c39f286f6125ecc98e9216443d98a011b17a8c1 (diff)
downloadfreqtimer-web-8633fe4774a83f90969e965c02b35e531d2ac6df.tar.xz
freqtimer-web-8633fe4774a83f90969e965c02b35e531d2ac6df.zip
Refactor, move label-making components into the model class, create a single update function for all labels
Diffstat (limited to 'src')
-rw-r--r--src/controllers/timer_view.cpp195
-rw-r--r--src/models/counter.cpp18
-rw-r--r--src/models/counter.hpp1
-rw-r--r--src/models/timer.cpp20
-rw-r--r--src/models/timer.hpp2
-rw-r--r--src/view/index.html28
6 files changed, 148 insertions, 116 deletions
diff --git a/src/controllers/timer_view.cpp b/src/controllers/timer_view.cpp
index 841d7f7..03e8bb5 100644
--- a/src/controllers/timer_view.cpp
+++ b/src/controllers/timer_view.cpp
@@ -19,11 +19,6 @@ class TimerView {
public:
- TimerView()
- : _counters { FreqCounter(_timer), FreqCounter(_timer) }
- , _alarm_sound_triggered(false)
- { }
-
/*
* Timer
*/
@@ -44,20 +39,76 @@ class TimerView {
set_inner_text_by_id("clear-reset-btn", text);
}
+ /*
+ * Counter
+ */
+
+ void counter_display_text(uint32_t counter_num, std::string const & counter_text, std::string const & rate_text) {
+ std::string counter_id = "counter-";
+ counter_id.append(std::to_string(counter_num));
+
+ std::string rate_id = "rate-";
+ rate_id.append(std::to_string(counter_num));
+
+ set_inner_text_by_id(counter_id, counter_text);
+ set_inner_text_by_id(rate_id, rate_text);
+ }
+
+
+ void set_clear_reset_button_disabled(bool disabled) {
+ set_button_disabled("clear-reset-btn", disabled);
+ }
+
+ void play_alarm() {
+
+ EM_ASM({
+ const handle = document.getElementById("sound-handle");
+
+ const duration = Math.round(handle.duration * 1000.0);
+
+ handle.currentTime = 0;
+ handle.play().then(() => {
+ setTimeout(() => {
+ handle.pause();
+ }, duration);
+ });
+ });
+
+ }
+
+ private:
+
+ static void set_inner_text_by_id(std::string const & id, std::string const & text) {
+ emval timer_display = document.call<emval>("getElementById", emval(id.c_str()));
+ timer_display.set("innerText", text.c_str());
+ }
+
+ static void set_button_disabled(std::string const & id, bool disabled) {
+ emval timer_display = document.call<emval>("getElementById", emval(id.c_str()));
+ timer_display.set("disabled", disabled);
+ }
+
+};
+
+class TimerController {
+
+ public:
+
+ TimerController()
+ : _counters { FreqCounter(_timer), FreqCounter(_timer) }
+ , _alarm_sound_triggered(false)
+ { }
+
void handle_inc_time(uint32_t amount) {
_timer.increment_preset_ms(amount * 1000);
- _update_start_stop_label();
- _update_clear_reset_btn_label();
- _update_clear_reset_btn();
+ _update_labels();
}
void handle_start_stop() {
_timer.toggleStart();
- _update_start_stop_label();
- _update_clear_reset_btn_label();
- _update_clear_reset_btn();
+ _update_labels();
}
void handle_clear_reset() {
@@ -67,58 +118,32 @@ class TimerView {
reset();
}
- _update_clear_reset_btn_label();
- _update_clear_reset_btn();
+ _update_labels();
}
/*
* Counter
*/
- void counter_display_text(uint32_t counter_num, std::string const & counter_text, std::string const & rate_text) {
- std::string counter_id = "counter-";
- counter_id.append(std::to_string(counter_num));
-
- std::string rate_id = "rate-";
- rate_id.append(std::to_string(counter_num));
-
- set_inner_text_by_id(counter_id, counter_text);
- set_inner_text_by_id(rate_id, rate_text);
- }
-
void handle_inc_counter(uint32_t counter_num) {
_counters[counter_num].inc();
+ // Effects
+ _update_labels();
_update_counter(counter_num);
-
- _update_clear_reset_btn_label();
}
void handle_timer_update() {
- std::string value = _timer.counter_display_value();
-
- timer_display_text(value);
-
- double record_floor = _timer.record_floor();
-
- std::string rec_floor_text;
- format_rate(rec_floor_text, record_floor);
-
- if(_timer.overtime()) {
- std::string rec_floor_overtime;
- format_rate(rec_floor_overtime, _timer.overtime_record_floor());
- rec_floor_text.append(", ");
- rec_floor_text.append(rec_floor_overtime);
- }
+ _view.timer_display_text(_timer.counter_display_value());
- timer_record_floor(rec_floor_text);
+ _view.timer_record_floor(_timer.record_floor_display());
for(size_t i = 0; i < _counters.size(); i++)
_update_counter(i);
if(_timer.overtime() && !_alarm_sound_triggered) {
- _play_alarm();
+ _view.play_alarm();
_alarm_sound_triggered = true;
}
}
@@ -126,77 +151,43 @@ class TimerView {
private:
FreqTimer _timer;
+ TimerView _view;
std::array<FreqCounter, 2> _counters;
bool _alarm_sound_triggered;
- static void set_inner_text_by_id(std::string const & id, std::string const & text) {
- emval timer_display = document.call<emval>("getElementById", emval(id.c_str()));
- timer_display.set("innerText", text.c_str());
- }
-
- static void set_button_disabled(std::string const & id, bool disabled) {
- emval timer_display = document.call<emval>("getElementById", emval(id.c_str()));
- timer_display.set("disabled", disabled);
- }
-
void _update_counter(uint32_t counter_num) {
FreqCounter & counter = _counters[counter_num];
- std::string text = counter.display_value();
- double rate = counter.rate();
-
- std::string rate_val;
- format_rate(rate_val, rate);
+ std::string count = counter.display_value();
+ std::string rate = counter.rate_display();
- if(_timer.overtime()) {
- std::string rate_overtime;
- format_rate(rate_overtime, counter.overtime_rate());
- rate_val.append(", ");
- rate_val.append(rate_overtime);
- }
-
- counter_display_text(counter_num, text, rate_val);
+ _view.counter_display_text(counter_num, count, rate);
}
- static void _play_alarm() {
-
- EM_ASM({
- const handle = document.getElementById("sound-handle");
-
- const duration = Math.round(handle.duration * 1000.0);
-
- handle.currentTime = 0;
- handle.play().then(() => {
- setTimeout(() => {
- handle.pause();
- }, duration);
- });
- });
-
+ bool _clear_reset_btn_clears() const {
+ return (
+ _timer.has_preset() and !_timer.started() and
+ (_counters[0].cnt() == 0 && _counters[1].cnt() == 0)
+ );
}
- void _update_start_stop_label() {
+ void _update_labels() {
+ // Start / Stop Label
if(_timer.running()) {
- timer_start_stop_button_label("Stop");
+ _view.timer_start_stop_button_label("Stop");
} else {
- timer_start_stop_button_label("Start");
+ _view.timer_start_stop_button_label("Start");
}
- }
-
- bool _clear_reset_btn_clears() const {
- return _timer.has_preset() and !_timer.started() and (_counters[0].cnt() == 0 && _counters[1].cnt() == 0);
- }
- void _update_clear_reset_btn_label() {
+ // Clear / Reset Button Label
if(_clear_reset_btn_clears()) {
- timer_clear_reset_button_label("Clear");
+ _view.timer_clear_reset_button_label("Clear");
} else {
- timer_clear_reset_button_label("Reset");
+ _view.timer_clear_reset_button_label("Reset");
}
- }
- void _update_clear_reset_btn() {
- set_button_disabled("clear-reset-btn", _timer.running());
+ // Disable the clear / reset button when the timer is running
+ _view.set_clear_reset_button_disabled(_timer.running());
}
void clear() {
@@ -220,22 +211,22 @@ class TimerView {
};
-EMSCRIPTEN_BINDINGS(timer_view) {
- emscripten::class_<TimerView>("TimerView")
+EMSCRIPTEN_BINDINGS(timer_controller) {
+ emscripten::class_<TimerController>("TimerController")
.constructor()
.function("handle_start_stop",
- &TimerView::handle_start_stop
+ &TimerController::handle_start_stop
)
.function("handle_clear_reset",
- &TimerView::handle_clear_reset
+ &TimerController::handle_clear_reset
)
.function("handle_inc_counter",
- &TimerView::handle_inc_counter
+ &TimerController::handle_inc_counter
)
.function("handle_inc_time",
- &TimerView::handle_inc_time
+ &TimerController::handle_inc_time
)
.function("handle_timer_update",
- &TimerView::handle_timer_update
+ &TimerController::handle_timer_update
);
}
diff --git a/src/models/counter.cpp b/src/models/counter.cpp
index 1b6315d..a80515e 100644
--- a/src/models/counter.cpp
+++ b/src/models/counter.cpp
@@ -1,4 +1,5 @@
#include "models/counter.hpp"
+#include "utils/utils.hpp"
double FreqCounter::rate() const {
uint64_t ms = _timer->total_duration_ms();
@@ -23,6 +24,23 @@ std::string FreqCounter::display_value() const {
return std::to_string(_overtime_cnt);
}
+std::string FreqCounter::rate_display() const {
+ double rate = this->rate();
+
+ std::string rate_display_val;
+ format_rate(rate_display_val, rate);
+
+ if(_timer->overtime()) {
+ std::string rate_overtime;
+ format_rate(rate_overtime, overtime_rate());
+
+ rate_display_val.append(", ");
+ rate_display_val.append(rate_overtime);
+ }
+
+ return rate_display_val;
+}
+
void FreqCounter::inc() {
if(_overtime_cnt != UINT32_MAX)
diff --git a/src/models/counter.hpp b/src/models/counter.hpp
index fa9e1c5..74f4e46 100644
--- a/src/models/counter.hpp
+++ b/src/models/counter.hpp
@@ -26,6 +26,7 @@ class FreqCounter {
[[nodiscard]] std::string display_value() const;
+ [[nodiscard]] std::string rate_display() const;
private:
diff --git a/src/models/timer.cpp b/src/models/timer.cpp
index a05e8c1..8ab3c23 100644
--- a/src/models/timer.cpp
+++ b/src/models/timer.cpp
@@ -1,6 +1,9 @@
#include "timer.hpp"
+
#include <cinttypes>
+#include "utils/utils.hpp"
+
#ifdef EMSCRIPTEN
#include <emscripten/html5.h>
#endif
@@ -73,6 +76,23 @@ std::string FreqTimer::counter_display_value() const {
return display_val;
}
+std::string FreqTimer::record_floor_display() const {
+ double record_floor = this->record_floor();
+
+ std::string record_floor_label;
+ format_rate(record_floor_label, record_floor);
+
+ if(overtime()) {
+ std::string rec_floor_overtime;
+ format_rate(rec_floor_overtime, overtime_record_floor());
+
+ record_floor_label.append(", ");
+ record_floor_label.append(rec_floor_overtime);
+ }
+
+ return record_floor_label;
+}
+
void FreqTimer::increment_preset_ms(uint64_t amount_ms) {
if(_state != State::SETTING) {
clear();
diff --git a/src/models/timer.hpp b/src/models/timer.hpp
index 0b0a978..fcaec1a 100644
--- a/src/models/timer.hpp
+++ b/src/models/timer.hpp
@@ -47,6 +47,8 @@ class FreqTimer {
[[nodiscard]] std::string counter_display_value() const;
+ [[nodiscard]] std::string record_floor_display() const;
+
/*
* Lowest frequency which can be measured
* in the interval (as measured in events/min
diff --git a/src/view/index.html b/src/view/index.html
index b92cc6d..32dd74e 100644
--- a/src/view/index.html
+++ b/src/view/index.html
@@ -33,12 +33,12 @@
<script src="/freqtimer/js/bundle.min.js"></script>
<!-- Controller Bindings -->
<script>
- var timer_view = null;
+ var timer_controller = null;
var sound_handle = null;
var Module = {
onRuntimeInitialized: function() {
- timer_view = new Module.TimerView;
+ timer_controller = new Module.TimerController;
}
};
@@ -62,8 +62,8 @@
}
setInterval(() => {
- if (timer_view !== null) {
- timer_view.handle_timer_update();
+ if (timer_controller !== null) {
+ timer_controller.handle_timer_update();
}
}, 10);
</script>
@@ -100,37 +100,37 @@
<div class="col-6 p-1">
<button
class="btn btn-outline-primary w-100"
- onclick="timer_view.handle_inc_time(1);"
+ onclick="timer_controller.handle_inc_time(1);"
>1s</button>
</div>
<div class="col-6 p-1">
<button
class="btn btn-outline-primary w-100"
- onclick="timer_view.handle_inc_time(6);"
+ onclick="timer_controller.handle_inc_time(6);"
>6s</button>
</div>
<div class="col-6 p-1">
<button
class="btn btn-outline-primary w-100"
- onclick="timer_view.handle_inc_time(10);"
+ onclick="timer_controller.handle_inc_time(10);"
>10s</button>
</div>
<div class="col-6 p-1">
<button
class="btn btn-outline-primary w-100"
- onclick="timer_view.handle_inc_time(15);"
+ onclick="timer_controller.handle_inc_time(15);"
>15s</button>
</div>
<div class="col-6 p-1">
<button
class="btn btn-outline-primary w-100"
- onclick="timer_view.handle_inc_time(30);"
+ onclick="timer_controller.handle_inc_time(30);"
>30s</button>
</div>
<div class="col-6 p-1">
<button
class="btn btn-outline-primary w-100"
- onclick="timer_view.handle_inc_time(60);"
+ onclick="timer_controller.handle_inc_time(60);"
>60s</button>
</div>
</div>
@@ -139,12 +139,12 @@
<button
class="btn btn-danger btn-llg w-100"
id="clear-reset-btn"
- onclick="timer_view.handle_clear_reset();"
+ onclick="timer_controller.handle_clear_reset();"
>Reset</button>
<button
class="btn btn-success btn-llg w-100"
id="start-stop-btn"
- onclick="timer_view.handle_start_stop(); attach_ios_audio();"
+ onclick="timer_controller.handle_start_stop(); attach_ios_audio();"
>Start</button>
</div>
@@ -153,14 +153,14 @@
<button
class="btn btn-secondary btn-plus w-100"
id="increment-counter-1-btn"
- onclick="timer_view.handle_inc_counter(0);"
+ onclick="timer_controller.handle_inc_counter(0);"
>+1</button>
</div>
<div class="col-6 p-1">
<button
class="btn btn-secondary btn-plus w-100"
id="increment-counter-2-btn"
- onclick="timer_view.handle_inc_counter(1);"
+ onclick="timer_controller.handle_inc_counter(1);"
>+1</button>
</div>
</div>