totp_app_settings.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "totp_app_settings.h"
  2. #include <math.h>
  3. #include "../../ui_controls.h"
  4. #include "../../scene_director.h"
  5. #include "../token_menu/totp_scene_token_menu.h"
  6. #include "../../constants.h"
  7. #include "../../../services/config/config.h"
  8. #include "../../../lib/roll_value/roll_value.h"
  9. #include "../../../types/nullable.h"
  10. #define DIGIT_TO_CHAR(digit) ((digit) + '0')
  11. typedef enum { HoursInput, MinutesInput, ConfirmButton } Control;
  12. typedef struct {
  13. int8_t tz_offset_hours;
  14. uint8_t tz_offset_minutes;
  15. TotpNullable_uint16_t current_token_index;
  16. Control selected_control;
  17. } SceneState;
  18. void totp_scene_app_settings_init(const PluginState* plugin_state) {
  19. UNUSED(plugin_state);
  20. }
  21. void totp_scene_app_settings_activate(
  22. PluginState* plugin_state,
  23. const AppSettingsSceneContext* context) {
  24. SceneState* scene_state = malloc(sizeof(SceneState));
  25. furi_check(scene_state != NULL);
  26. plugin_state->current_scene_state = scene_state;
  27. if(context != NULL) {
  28. TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index);
  29. } else {
  30. TOTP_NULLABLE_NULL(scene_state->current_token_index);
  31. }
  32. float off_int;
  33. float off_dec = modff(plugin_state->timezone_offset, &off_int);
  34. scene_state->tz_offset_hours = off_int;
  35. scene_state->tz_offset_minutes = 60.0f * off_dec;
  36. }
  37. static void two_digit_to_str(int8_t num, char* str) {
  38. uint8_t index = 0;
  39. if(num < 0) {
  40. str[0] = '-';
  41. index++;
  42. num = -num;
  43. }
  44. uint8_t d1 = (num / 10) % 10;
  45. uint8_t d2 = num % 10;
  46. str[index] = DIGIT_TO_CHAR(d1);
  47. str[index + 1] = DIGIT_TO_CHAR(d2);
  48. str[index + 2] = '\0';
  49. }
  50. void totp_scene_app_settings_render(Canvas* const canvas, PluginState* plugin_state) {
  51. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  52. canvas_set_font(canvas, FontPrimary);
  53. canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "Timezone offset");
  54. canvas_set_font(canvas, FontSecondary);
  55. char tmp_str[4];
  56. two_digit_to_str(scene_state->tz_offset_hours, &tmp_str[0]);
  57. canvas_draw_str_aligned(canvas, 0, 16, AlignLeft, AlignTop, "Hours:");
  58. ui_control_select_render(
  59. canvas,
  60. 36,
  61. 10,
  62. SCREEN_WIDTH - 36,
  63. &tmp_str[0],
  64. scene_state->selected_control == HoursInput);
  65. two_digit_to_str(scene_state->tz_offset_minutes, &tmp_str[0]);
  66. canvas_draw_str_aligned(canvas, 0, 34, AlignLeft, AlignTop, "Minutes:");
  67. ui_control_select_render(
  68. canvas,
  69. 36,
  70. 28,
  71. SCREEN_WIDTH - 36,
  72. &tmp_str[0],
  73. scene_state->selected_control == MinutesInput);
  74. ui_control_button_render(
  75. canvas,
  76. SCREEN_WIDTH_CENTER - 24,
  77. 50,
  78. 48,
  79. 13,
  80. "Confirm",
  81. scene_state->selected_control == ConfirmButton);
  82. }
  83. bool totp_scene_app_settings_handle_event(
  84. const PluginEvent* const event,
  85. PluginState* plugin_state) {
  86. if(event->type != EventTypeKey) {
  87. return true;
  88. }
  89. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  90. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  91. return true;
  92. }
  93. switch(event->input.key) {
  94. case InputKeyUp:
  95. totp_roll_value_uint8_t(
  96. &scene_state->selected_control,
  97. -1,
  98. HoursInput,
  99. ConfirmButton,
  100. RollOverflowBehaviorStop);
  101. break;
  102. case InputKeyDown:
  103. totp_roll_value_uint8_t(
  104. &scene_state->selected_control, 1, HoursInput, ConfirmButton, RollOverflowBehaviorStop);
  105. break;
  106. case InputKeyRight:
  107. if(scene_state->selected_control == HoursInput) {
  108. totp_roll_value_int8_t(
  109. &scene_state->tz_offset_hours, 1, -12, 12, RollOverflowBehaviorStop);
  110. } else if(scene_state->selected_control == MinutesInput) {
  111. totp_roll_value_uint8_t(
  112. &scene_state->tz_offset_minutes, 15, 0, 45, RollOverflowBehaviorRoll);
  113. }
  114. break;
  115. case InputKeyLeft:
  116. if(scene_state->selected_control == HoursInput) {
  117. totp_roll_value_int8_t(
  118. &scene_state->tz_offset_hours, -1, -12, 12, RollOverflowBehaviorStop);
  119. } else if(scene_state->selected_control == MinutesInput) {
  120. totp_roll_value_uint8_t(
  121. &scene_state->tz_offset_minutes, -15, 0, 45, RollOverflowBehaviorRoll);
  122. }
  123. break;
  124. case InputKeyOk:
  125. if(scene_state->selected_control == ConfirmButton) {
  126. plugin_state->timezone_offset = (float)scene_state->tz_offset_hours +
  127. (float)scene_state->tz_offset_minutes / 60.0f;
  128. totp_config_file_update_timezone_offset(plugin_state->timezone_offset);
  129. if(!scene_state->current_token_index.is_null) {
  130. TokenMenuSceneContext generate_scene_context = {
  131. .current_token_index = scene_state->current_token_index.value};
  132. totp_scene_director_activate_scene(
  133. plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  134. } else {
  135. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  136. }
  137. }
  138. break;
  139. case InputKeyBack: {
  140. if(!scene_state->current_token_index.is_null) {
  141. TokenMenuSceneContext generate_scene_context = {
  142. .current_token_index = scene_state->current_token_index.value};
  143. totp_scene_director_activate_scene(
  144. plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  145. } else {
  146. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  147. }
  148. break;
  149. }
  150. default:
  151. break;
  152. }
  153. return true;
  154. }
  155. void totp_scene_app_settings_deactivate(PluginState* plugin_state) {
  156. if(plugin_state->current_scene_state == NULL) return;
  157. free(plugin_state->current_scene_state);
  158. plugin_state->current_scene_state = NULL;
  159. }
  160. void totp_scene_app_settings_free(const PluginState* plugin_state) {
  161. UNUSED(plugin_state);
  162. }