From 85de0b02be2aeededb0c1b94b55c4d79ffa503f3 Mon Sep 17 00:00:00 2001 From: Jakub <106778416+MarekZegare4@users.noreply.github.com> Date: Thu, 14 May 2026 11:36:11 +0200 Subject: [PATCH] fix: guard savePrefs with dirty flag in BotScreen and DashboardConfigScreen BotScreen and DashboardConfigScreen were calling savePrefs() on every exit regardless of whether any setting changed. Add _dirty flag set only on actual value changes (toggle, channel select, keyboard confirm, field cycle), reset on enter(). Co-Authored-By: Claude Sonnet 4.6 --- examples/companion_radio/ui-new/BotScreen.h | 7 ++++++- examples/companion_radio/ui-new/DashboardConfigScreen.h | 8 +++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/companion_radio/ui-new/BotScreen.h b/examples/companion_radio/ui-new/BotScreen.h index 49ab0585..5a333af6 100644 --- a/examples/companion_radio/ui-new/BotScreen.h +++ b/examples/companion_radio/ui-new/BotScreen.h @@ -13,6 +13,7 @@ class BotScreen : public UIScreen { static const int VAL_X = 70; int _sel; + bool _dirty; // keyboard state (reused for trigger and reply fields) int _kb_field; // -1=off, 2=trigger, 3=reply DM, 4=reply Ch @@ -45,6 +46,7 @@ public: void enter() { _sel = 0; _kb_field = -1; + _dirty = false; refreshChannels(); } @@ -119,6 +121,7 @@ public: strncpy(_prefs->bot_reply_ch, _kb.buf, sizeof(_prefs->bot_reply_ch) - 1); _prefs->bot_reply_ch[sizeof(_prefs->bot_reply_ch) - 1] = '\0'; } + _dirty = true; _kb_field = -1; } else if (res == KeyboardWidget::CANCELLED) { _kb_field = -1; @@ -127,7 +130,7 @@ public: } if (cancel) { - the_mesh.savePrefs(); + if (_dirty) the_mesh.savePrefs(); _task->gotoToolsScreen(); return true; } @@ -136,6 +139,7 @@ public: if (_sel == 0 && (enter || left || right)) { _prefs->bot_enabled ^= 1; + _dirty = true; return true; } if (_sel == 1) { @@ -157,6 +161,7 @@ public: _prefs->bot_channel_enabled = 1; _prefs->bot_channel_idx = _channel_indices[idx]; } + _dirty = true; return true; } } diff --git a/examples/companion_radio/ui-new/DashboardConfigScreen.h b/examples/companion_radio/ui-new/DashboardConfigScreen.h index 1cbc007a..23fa962e 100644 --- a/examples/companion_radio/ui-new/DashboardConfigScreen.h +++ b/examples/companion_radio/ui-new/DashboardConfigScreen.h @@ -26,17 +26,19 @@ class DashboardConfigScreen : public UIScreen { static const char* OPTION_NAMES[DASH_COUNT]; - int _sel; + int _sel; + bool _dirty; void cycle(int slot, int dir) { uint8_t& f = _prefs->dashboard_fields[slot]; f = (uint8_t)((f + DASH_COUNT + dir) % DASH_COUNT); + _dirty = true; } public: DashboardConfigScreen(UITask* task, NodePrefs* prefs) : _task(task), _prefs(prefs) {} - void enter() { _sel = 0; } + void enter() { _sel = 0; _dirty = false; } int render(DisplayDriver& display) override { display.setTextSize(1); @@ -67,7 +69,7 @@ public: bool handleInput(char c) override { if (c == KEY_CANCEL || c == KEY_CONTEXT_MENU) { - the_mesh.savePrefs(); + if (_dirty) the_mesh.savePrefs(); _task->gotoHomeScreen(); return true; }