totp_scene_token_menu.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "totp_scene_token_menu.h"
  2. #include <gui/gui.h>
  3. #include <dialogs/dialogs.h>
  4. #include "../../ui_controls.h"
  5. #include "../../common_dialogs.h"
  6. #include "../../constants.h"
  7. #include "../../scene_director.h"
  8. #include "../../../services/config/config.h"
  9. #include "../../../types/token_info.h"
  10. #include "../generate_token/totp_scene_generate_token.h"
  11. #include "../add_new_token/totp_scene_add_new_token.h"
  12. #include "../app_settings/totp_app_settings.h"
  13. #include <roll_value.h>
  14. #define SCREEN_HEIGHT_THIRD (SCREEN_HEIGHT / 3)
  15. #define SCREEN_HEIGHT_THIRD_CENTER (SCREEN_HEIGHT_THIRD >> 1)
  16. typedef enum { AddNewToken, DeleteToken, AppSettings } Control;
  17. typedef struct {
  18. Control selected_control;
  19. } SceneState;
  20. void totp_scene_token_menu_activate(PluginState* plugin_state) {
  21. SceneState* scene_state = malloc(sizeof(SceneState));
  22. furi_check(scene_state != NULL);
  23. plugin_state->current_scene_state = scene_state;
  24. }
  25. void totp_scene_token_menu_render(Canvas* const canvas, PluginState* plugin_state) {
  26. const SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  27. const TokenInfoIteratorContext* iterator_context =
  28. totp_config_get_token_iterator_context(plugin_state);
  29. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  30. ui_control_button_render(
  31. canvas,
  32. SCREEN_WIDTH_CENTER - 36,
  33. 5,
  34. 72,
  35. 21,
  36. "Add new token",
  37. scene_state->selected_control == AddNewToken);
  38. ui_control_button_render(
  39. canvas,
  40. SCREEN_WIDTH_CENTER - 36,
  41. 39,
  42. 72,
  43. 21,
  44. "Settings",
  45. scene_state->selected_control == AppSettings);
  46. } else {
  47. ui_control_button_render(
  48. canvas,
  49. SCREEN_WIDTH_CENTER - 36,
  50. SCREEN_HEIGHT_THIRD_CENTER - 8,
  51. 72,
  52. 16,
  53. "Add new token",
  54. scene_state->selected_control == AddNewToken);
  55. ui_control_button_render(
  56. canvas,
  57. SCREEN_WIDTH_CENTER - 36,
  58. SCREEN_HEIGHT_THIRD + SCREEN_HEIGHT_THIRD_CENTER - 8,
  59. 72,
  60. 16,
  61. "Delete token",
  62. scene_state->selected_control == DeleteToken);
  63. ui_control_button_render(
  64. canvas,
  65. SCREEN_WIDTH_CENTER - 36,
  66. SCREEN_HEIGHT_THIRD + SCREEN_HEIGHT_THIRD + SCREEN_HEIGHT_THIRD_CENTER - 8,
  67. 72,
  68. 16,
  69. "Settings",
  70. scene_state->selected_control == AppSettings);
  71. }
  72. }
  73. bool totp_scene_token_menu_handle_event(const PluginEvent* const event, PluginState* plugin_state) {
  74. if(event->type != EventTypeKey) {
  75. return true;
  76. }
  77. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  78. if(event->input.type != InputTypePress) {
  79. return true;
  80. }
  81. switch(event->input.key) {
  82. case InputKeyUp: {
  83. const TokenInfoIteratorContext* iterator_context =
  84. totp_config_get_token_iterator_context(plugin_state);
  85. totp_roll_value_uint8_t(
  86. &scene_state->selected_control, -1, AddNewToken, AppSettings, RollOverflowBehaviorRoll);
  87. if(scene_state->selected_control == DeleteToken &&
  88. totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  89. scene_state->selected_control--;
  90. }
  91. break;
  92. }
  93. case InputKeyDown: {
  94. const TokenInfoIteratorContext* iterator_context =
  95. totp_config_get_token_iterator_context(plugin_state);
  96. totp_roll_value_uint8_t(
  97. &scene_state->selected_control, 1, AddNewToken, AppSettings, RollOverflowBehaviorRoll);
  98. if(scene_state->selected_control == DeleteToken &&
  99. totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  100. scene_state->selected_control++;
  101. }
  102. break;
  103. }
  104. case InputKeyRight:
  105. break;
  106. case InputKeyLeft:
  107. break;
  108. case InputKeyOk:
  109. switch(scene_state->selected_control) {
  110. case AddNewToken: {
  111. totp_scene_director_activate_scene(plugin_state, TotpSceneAddNewToken);
  112. break;
  113. }
  114. case DeleteToken: {
  115. DialogMessage* message = dialog_message_alloc();
  116. dialog_message_set_buttons(message, "No", NULL, "Yes");
  117. dialog_message_set_header(message, "Confirmation", 0, 0, AlignLeft, AlignTop);
  118. dialog_message_set_text(
  119. message,
  120. "Are you sure want to delete?",
  121. SCREEN_WIDTH_CENTER,
  122. SCREEN_HEIGHT_CENTER,
  123. AlignCenter,
  124. AlignCenter);
  125. DialogMessageButton dialog_result =
  126. dialog_message_show(plugin_state->dialogs_app, message);
  127. dialog_message_free(message);
  128. TokenInfoIteratorContext* iterator_context =
  129. totp_config_get_token_iterator_context(plugin_state);
  130. if(dialog_result == DialogMessageButtonRight &&
  131. totp_token_info_iterator_get_total_count(iterator_context) > 0) {
  132. if(!totp_token_info_iterator_remove_current_token_info(iterator_context)) {
  133. totp_dialogs_config_updating_error(plugin_state);
  134. return false;
  135. }
  136. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  137. }
  138. break;
  139. }
  140. case AppSettings: {
  141. totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings);
  142. break;
  143. }
  144. default:
  145. break;
  146. }
  147. break;
  148. case InputKeyBack: {
  149. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  150. break;
  151. }
  152. default:
  153. break;
  154. }
  155. return true;
  156. }
  157. void totp_scene_token_menu_deactivate(PluginState* plugin_state) {
  158. if(plugin_state->current_scene_state == NULL) return;
  159. free(plugin_state->current_scene_state);
  160. plugin_state->current_scene_state = NULL;
  161. }