Settings_view.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "UnitempViews.h"
  16. #include <gui/modules/variable_item_list.h>
  17. //Текущий вид
  18. static View* view;
  19. //Список
  20. static VariableItemList* variable_item_list;
  21. static const char states[2][9] = {"Auto", "Infinity"};
  22. static const char units[2][3] = {"*C", "*F"};
  23. //Элемент списка - бесконечная подсветка
  24. VariableItem* infinity_backlight_item;
  25. //Единица измерения температуры
  26. VariableItem* temperature_unit_item;
  27. #define VIEW_ID UnitempViewSettings
  28. /**
  29. * @brief Функция обработки нажатия кнопки "Назад"
  30. *
  31. * @param context Указатель на данные приложения
  32. * @return ID вида в который нужно переключиться
  33. */
  34. static uint32_t _exit_callback(void* context) {
  35. UNUSED(context);
  36. //Костыль с зависающей подсветкой
  37. if((bool)variable_item_get_current_value_index(infinity_backlight_item) !=
  38. app->settings.infinityBacklight) {
  39. if((bool)variable_item_get_current_value_index(infinity_backlight_item)) {
  40. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  41. } else {
  42. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  43. }
  44. }
  45. app->settings.infinityBacklight =
  46. (bool)variable_item_get_current_value_index(infinity_backlight_item);
  47. app->settings.unit = (bool)variable_item_get_current_value_index(temperature_unit_item);
  48. unitemp_saveSettings();
  49. unitemp_loadSettings();
  50. //Возврат предыдущий вид
  51. return UnitempViewMainMenu;
  52. }
  53. /**
  54. * @brief Функция обработки нажатия средней кнопки
  55. *
  56. * @param context Указатель на данные приложения
  57. * @param index На каком элементе списка была нажата кнопка
  58. */
  59. static void _enter_callback(void* context, uint32_t index) {
  60. UNUSED(context);
  61. UNUSED(index);
  62. }
  63. static void _setting_change_callback(VariableItem* item) {
  64. if(item == infinity_backlight_item) {
  65. variable_item_set_current_value_text(
  66. infinity_backlight_item,
  67. states[variable_item_get_current_value_index(infinity_backlight_item)]);
  68. }
  69. if(item == temperature_unit_item) {
  70. variable_item_set_current_value_text(
  71. temperature_unit_item,
  72. units[variable_item_get_current_value_index(temperature_unit_item)]);
  73. }
  74. }
  75. /**
  76. * @brief Создание меню редактирования настроек
  77. */
  78. void unitemp_Settings_alloc(void) {
  79. variable_item_list = variable_item_list_alloc();
  80. //Сброс всех элементов меню
  81. variable_item_list_reset(variable_item_list);
  82. infinity_backlight_item = variable_item_list_add(
  83. variable_item_list, "Backlight time", 2, _setting_change_callback, app);
  84. temperature_unit_item =
  85. variable_item_list_add(variable_item_list, "Temp. unit", 2, _setting_change_callback, app);
  86. //Добавление колбека на нажатие средней кнопки
  87. variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app);
  88. //Создание вида из списка
  89. view = variable_item_list_get_view(variable_item_list);
  90. //Добавление колбека на нажатие кнопки "Назад"
  91. view_set_previous_callback(view, _exit_callback);
  92. //Добавление вида в диспетчер
  93. view_dispatcher_add_view(app->view_dispatcher, VIEW_ID, view);
  94. }
  95. void unitemp_Settings_switch(void) {
  96. //Обнуление последнего выбранного пункта
  97. variable_item_list_set_selected_item(variable_item_list, 0);
  98. variable_item_set_current_value_index(
  99. infinity_backlight_item, (uint8_t)app->settings.infinityBacklight);
  100. variable_item_set_current_value_text(
  101. infinity_backlight_item,
  102. states[variable_item_get_current_value_index(infinity_backlight_item)]);
  103. variable_item_set_current_value_index(temperature_unit_item, (uint8_t)app->settings.unit);
  104. variable_item_set_current_value_text(
  105. temperature_unit_item,
  106. units[variable_item_get_current_value_index(temperature_unit_item)]);
  107. view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID);
  108. }
  109. void unitemp_Settings_free(void) {
  110. //Очистка списка элементов
  111. variable_item_list_free(variable_item_list);
  112. //Очистка вида
  113. view_free(view);
  114. //Удаление вида после обработки
  115. view_dispatcher_remove_view(app->view_dispatcher, VIEW_ID);
  116. }