totp_scene_token_menu.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = totp_config_get_token_iterator_context(plugin_state);
  28. if(totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  29. ui_control_button_render(
  30. canvas,
  31. SCREEN_WIDTH_CENTER - 36,
  32. 5,
  33. 72,
  34. 21,
  35. "Add new token",
  36. scene_state->selected_control == AddNewToken);
  37. ui_control_button_render(
  38. canvas,
  39. SCREEN_WIDTH_CENTER - 36,
  40. 39,
  41. 72,
  42. 21,
  43. "Settings",
  44. scene_state->selected_control == AppSettings);
  45. } else {
  46. ui_control_button_render(
  47. canvas,
  48. SCREEN_WIDTH_CENTER - 36,
  49. SCREEN_HEIGHT_THIRD_CENTER - 8,
  50. 72,
  51. 16,
  52. "Add new token",
  53. scene_state->selected_control == AddNewToken);
  54. ui_control_button_render(
  55. canvas,
  56. SCREEN_WIDTH_CENTER - 36,
  57. SCREEN_HEIGHT_THIRD + SCREEN_HEIGHT_THIRD_CENTER - 8,
  58. 72,
  59. 16,
  60. "Delete token",
  61. scene_state->selected_control == DeleteToken);
  62. ui_control_button_render(
  63. canvas,
  64. SCREEN_WIDTH_CENTER - 36,
  65. SCREEN_HEIGHT_THIRD + SCREEN_HEIGHT_THIRD + SCREEN_HEIGHT_THIRD_CENTER - 8,
  66. 72,
  67. 16,
  68. "Settings",
  69. scene_state->selected_control == AppSettings);
  70. }
  71. }
  72. bool totp_scene_token_menu_handle_event(const PluginEvent* const event, PluginState* plugin_state) {
  73. if(event->type != EventTypeKey) {
  74. return true;
  75. }
  76. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  77. if(event->input.type != InputTypePress) {
  78. return true;
  79. }
  80. switch(event->input.key) {
  81. case InputKeyUp: {
  82. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  83. totp_roll_value_uint8_t(
  84. &scene_state->selected_control, -1, AddNewToken, AppSettings, RollOverflowBehaviorRoll);
  85. if(scene_state->selected_control == DeleteToken &&
  86. totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  87. scene_state->selected_control--;
  88. }
  89. break;
  90. }
  91. case InputKeyDown: {
  92. const TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  93. totp_roll_value_uint8_t(
  94. &scene_state->selected_control, 1, AddNewToken, AppSettings, RollOverflowBehaviorRoll);
  95. if(scene_state->selected_control == DeleteToken &&
  96. totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  97. scene_state->selected_control++;
  98. }
  99. break;
  100. }
  101. case InputKeyRight:
  102. break;
  103. case InputKeyLeft:
  104. break;
  105. case InputKeyOk:
  106. switch(scene_state->selected_control) {
  107. case AddNewToken: {
  108. totp_scene_director_activate_scene(plugin_state, TotpSceneAddNewToken);
  109. break;
  110. }
  111. case DeleteToken: {
  112. DialogMessage* message = dialog_message_alloc();
  113. dialog_message_set_buttons(message, "No", NULL, "Yes");
  114. dialog_message_set_header(message, "Confirmation", 0, 0, AlignLeft, AlignTop);
  115. dialog_message_set_text(
  116. message,
  117. "Are you sure want to delete?",
  118. SCREEN_WIDTH_CENTER,
  119. SCREEN_HEIGHT_CENTER,
  120. AlignCenter,
  121. AlignCenter);
  122. DialogMessageButton dialog_result =
  123. dialog_message_show(plugin_state->dialogs_app, message);
  124. dialog_message_free(message);
  125. TokenInfoIteratorContext* iterator_context = totp_config_get_token_iterator_context(plugin_state);
  126. if(dialog_result == DialogMessageButtonRight &&
  127. totp_token_info_iterator_get_total_count(iterator_context) > 0) {
  128. if(!totp_token_info_iterator_remove_current_token_info(iterator_context)) {
  129. totp_dialogs_config_updating_error(plugin_state);
  130. return false;
  131. }
  132. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  133. }
  134. break;
  135. }
  136. case AppSettings: {
  137. totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings);
  138. break;
  139. }
  140. default:
  141. break;
  142. }
  143. break;
  144. case InputKeyBack: {
  145. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  146. break;
  147. }
  148. default:
  149. break;
  150. }
  151. return true;
  152. }
  153. void totp_scene_token_menu_deactivate(PluginState* plugin_state) {
  154. if(plugin_state->current_scene_state == NULL) return;
  155. free(plugin_state->current_scene_state);
  156. plugin_state->current_scene_state = NULL;
  157. }