diff --git a/main/displays/menus/manageprofilesmenu.cpp b/main/displays/menus/manageprofilesmenu.cpp index 7dbce98..7fa56ef 100644 --- a/main/displays/menus/manageprofilesmenu.cpp +++ b/main/displays/menus/manageprofilesmenu.cpp @@ -10,6 +10,7 @@ #include "globals.h" #include "mainmenu.h" #include "presets.h" +#include "settingsutils.h" #include "utils.h" constexpr const char * const TAG = "ProfileManager"; @@ -28,27 +29,101 @@ public: ManageProfileMenuItem(ManageProfilesMenu &menu, const uint8_t profileIndex) : m_menu{menu}, m_profileIndex{profileIndex} {} void copy() { + if (m_mode == CONFIRM_COPY) + { + const auto currProfile = settingsPersister.currentlyOpenProfileIndex(); + if (!currProfile) + return; + settingsutils::switchProfile(m_menu.m_firstIndex); + + if (!settingsPersister.openProfile(m_profileIndex)) // just switch nvs namespace + { + BobbyErrorHandler{}.errorOccured(fmt::format("openProfile({}) failed", m_profileIndex)); + return; + } + saveProfileSettings(); + settingsutils::switchProfile(*currProfile); + m_mode = _DEFAULT; + m_menu.m_firstIndex = -1; + } + else if (m_menu.m_firstIndex == m_profileIndex && m_mode == SELECT_OTHER) + { + m_mode = _DEFAULT; + m_menu.m_firstIndex = -1; + } + else if (m_menu.m_firstIndex == -1 && m_mode == _DEFAULT) + { + m_mode = SELECT_OTHER; + m_menu.m_firstIndex = m_profileIndex; + } + else if (m_menu.m_firstIndex != -1 && m_menu.m_firstIndex != m_profileIndex) + { + BobbyErrorHandler{}.errorOccured(fmt::format("Press CONFIRM to COPY from Profile {} to Profile {}", m_menu.m_firstIndex, m_profileIndex)); + m_mode = CONFIRM_COPY; + } } void swap() { + if (m_mode == CONFIRM_SWAP) + { + const auto currProfile = settingsPersister.currentlyOpenProfileIndex(); + if (!currProfile) + return; + settingsutils::switchProfile(m_menu.m_firstIndex); + const ProfileSettings tmp = profileSettings; + settingsutils::switchProfile(m_profileIndex); + const ProfileSettings tmp2 = profileSettings; + + profileSettings = tmp; + saveProfileSettings(); + settingsutils::switchProfile(m_menu.m_firstIndex); + profileSettings = tmp2; + saveProfileSettings(); + + m_mode = _DEFAULT; + m_menu.m_firstIndex = -1; + settingsutils::switchProfile(*currProfile); + } + else if (m_menu.m_firstIndex == m_profileIndex && m_mode == SELECT_OTHER) + { + m_mode = _DEFAULT; + m_menu.m_firstIndex = -1; + } + else if (m_menu.m_firstIndex == -1 && m_mode == _DEFAULT) + { + m_mode = SELECT_OTHER; + m_menu.m_firstIndex = m_profileIndex; + } + else if (m_menu.m_firstIndex != -1 && m_menu.m_firstIndex != m_profileIndex) + { + BobbyErrorHandler{}.errorOccured(fmt::format("Press CONFIRM to SWAP Profile {} with Profile {}", m_menu.m_firstIndex, m_profileIndex)); + m_mode = CONFIRM_SWAP; + } } void clear() { - if (m_mode == CONFIRMCLEAR) + if (m_mode == CONFIRM_CLEAR) { + const auto currProfile = settingsPersister.currentlyOpenProfileIndex(); + if (!currProfile) + return; + m_menu.unlock(); ESP_LOGI(TAG, "Reseting profile %i...", m_profileIndex); + settingsutils::switchProfile(m_profileIndex); profileSettings = presets::defaultProfileSettings; + saveProfileSettings(); m_mode = _DEFAULT; + settingsutils::switchProfile(*currProfile); } - else + else if(m_mode == _DEFAULT) { m_menu.lock(); - m_mode = CONFIRMCLEAR; + m_mode = CONFIRM_CLEAR; BobbyErrorHandler{}.errorOccured("Press CONFIRM to reset Profile or BACK to cancel."); } } @@ -59,16 +134,22 @@ public: { case _DEFAULT: return fmt::format("Profile {}", m_profileIndex); - case CONFIRMCLEAR: + case CONFIRM_CLEAR: return fmt::format("&1Profile {}", m_profileIndex); + default: + break; } return fmt::format("Profile {}", m_profileIndex); } int color() const override { - if (m_mode == CONFIRMCLEAR) + if (m_mode == CONFIRM_CLEAR || m_mode == CONFIRM_COPY || m_mode == CONFIRM_SWAP) return TFT_RED; + else if (m_menu.m_firstIndex == m_profileIndex) + return TFT_GREEN; + else if (m_menu.m_firstIndex != -1) + return TFT_GREY; else if (m_menu.m_locked) return TFT_DARKGREY; return TFT_WHITE; @@ -95,13 +176,16 @@ public: void update() override { Base::update(); - if (m_mode != _DEFAULT && !m_menu.m_locked) + if (m_mode != _DEFAULT && !m_menu.m_locked && m_menu.m_firstIndex == -1) m_mode = _DEFAULT; } private: enum Mode : uint8_t { _DEFAULT, - CONFIRMCLEAR + CONFIRM_CLEAR, + SELECT_OTHER, + CONFIRM_COPY, + CONFIRM_SWAP } m_mode; ManageProfilesMenu &m_menu; const uint8_t m_profileIndex; @@ -122,6 +206,8 @@ public: { m_menu.m_action = Actions(0); } + m_menu.m_firstIndex = -1; + m_menu.unlock(); } private: ManageProfilesMenu &m_menu; @@ -158,10 +244,18 @@ void ManageProfilesMenu::stop() void ManageProfilesMenu::back() { - if (!m_locked) + if (!m_locked && m_firstIndex == -1) + { switchScreen(); - else + return; + } + + if (m_locked) m_locked = false; + + if (m_firstIndex != -1) + m_firstIndex = -1; + } std::string ManageProfilesMenu::text() const diff --git a/main/displays/menus/manageprofilesmenu.h b/main/displays/menus/manageprofilesmenu.h index 3b3c448..500645d 100644 --- a/main/displays/menus/manageprofilesmenu.h +++ b/main/displays/menus/manageprofilesmenu.h @@ -35,4 +35,5 @@ private: protected: bool m_locked{false}; Actions m_action{Actions::Clear}; + int8_t m_firstIndex{-1}; }; diff --git a/main/webserver.cpp b/main/webserver.cpp index 8798732..9812683 100644 --- a/main/webserver.cpp +++ b/main/webserver.cpp @@ -174,7 +174,7 @@ esp_err_t webserver_status_handler(httpd_req_t *req) CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); } #endif - + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); std::string wants_json_query; if (auto result = esphttpdutils::webserver_get_query(req)) wants_json_query = *result; diff --git a/main/webserver_displaycontrol.cpp b/main/webserver_displaycontrol.cpp index 32227c7..7e9ef9d 100644 --- a/main/webserver_displaycontrol.cpp +++ b/main/webserver_displaycontrol.cpp @@ -281,6 +281,8 @@ esp_err_t webserver_root_handler(httpd_req_t *req) } } + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); + CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, (key_result == ESP_OK) ? "application/json":"text/html", body) } @@ -354,6 +356,7 @@ esp_err_t webserver_triggerRawButton_handler(httpd_req_t *req) espgui::currentDisplay->rawButtonReleased(button); CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Location", "/") + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::TemporaryRedirect, "text/html", "Ok, continue at /") } @@ -427,6 +430,7 @@ esp_err_t webserver_triggerButton_handler(httpd_req_t *req) espgui::currentDisplay->buttonReleased(button); CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Location", "/") + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::TemporaryRedirect, "text/html", "Ok, continue at /") } @@ -441,6 +445,7 @@ esp_err_t webserver_triggerItem_handler(httpd_req_t *req) CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); } #endif + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) @@ -530,6 +535,8 @@ esp_err_t webserver_setValue_handler(httpd_req_t *req) } #endif + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); + std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) query = *result; diff --git a/main/webserver_ota.cpp b/main/webserver_ota.cpp index 2d614f6..f2546ce 100644 --- a/main/webserver_ota.cpp +++ b/main/webserver_ota.cpp @@ -43,7 +43,7 @@ esp_err_t webserver_ota_percentage_handler(httpd_req_t *req) CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); } #endif - + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); std::string body; std::string wants_json_query; @@ -102,7 +102,7 @@ esp_err_t webserver_ota_handler(httpd_req_t *req) CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); } #endif - + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); std::string body; std::string wants_json_query; @@ -370,7 +370,7 @@ esp_err_t webserver_trigger_ota_handler(httpd_req_t *req) CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); } #endif - + CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Access-Control-Allow-Origin", "http://web.bobbycar.cloud"); std::string query; if (auto result = esphttpdutils::webserver_get_query(req)) query = *result;