From 548996bf6752e0931521890e40f4e723a6cf884e Mon Sep 17 00:00:00 2001 From: forntoh Date: Tue, 22 Oct 2024 14:46:10 +0200 Subject: [PATCH] Refactor increment and decrement methods for WidgetRange Simplify logic in the increment and decrement methods to improve readability and maintainability. Return false if value remains unchanged after operation. --- src/widget/WidgetRange.h | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/widget/WidgetRange.h b/src/widget/WidgetRange.h index ff2c1ea3..e2f68c73 100644 --- a/src/widget/WidgetRange.h +++ b/src/widget/WidgetRange.h @@ -51,13 +51,15 @@ class WidgetRange : public BaseWidgetValue { case UP: if (increment()) { BaseWidgetValue::handleChange(); + return true; } - return true; + return false; case DOWN: if (decrement()) { BaseWidgetValue::handleChange(); + return true; } - return true; + return false; default: return false; } @@ -70,13 +72,13 @@ class WidgetRange : public BaseWidgetValue { * @return true if incremented or reset (in case of cycle) */ bool increment() { - if (this->value + step > maxValue) { - this->value = cycle ? minValue : maxValue; - } else { - this->value += step; + T newValue = (this->value + step > maxValue) ? (cycle ? minValue : maxValue) : (this->value + step); + if (newValue != this->value) { + this->value = newValue; + printLog(F("WidgetRange::increment"), this->value); + return true; } - printLog(F("WidgetRange::increment"), this->value); - return true; + return false; } /** @@ -85,13 +87,13 @@ class WidgetRange : public BaseWidgetValue { * @return true if decremented or reset (in case of cycle) */ bool decrement() { - if (this->value - step < minValue) { - this->value = cycle ? maxValue : minValue; - } else { - this->value -= step; + T newValue = (this->value - step < minValue) ? (cycle ? maxValue : minValue) : (this->value - step); + if (newValue != this->value) { + this->value = newValue; + printLog(F("WidgetRange::decrement"), this->value); + return true; } - printLog(F("WidgetRange::decrement"), this->value); - return true; + return false; } };