totp_scene_token_menu.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. switch(event->input.key) {
  80. case InputKeyUp: {
  81. const TokenInfoIteratorContext* iterator_context =
  82. totp_config_get_token_iterator_context(plugin_state);
  83. totp_roll_value_uint8_t(
  84. &scene_state->selected_control,
  85. -1,
  86. AddNewToken,
  87. AppSettings,
  88. RollOverflowBehaviorRoll);
  89. if(scene_state->selected_control == DeleteToken &&
  90. totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  91. scene_state->selected_control--;
  92. }
  93. break;
  94. }
  95. case InputKeyDown: {
  96. const TokenInfoIteratorContext* iterator_context =
  97. totp_config_get_token_iterator_context(plugin_state);
  98. totp_roll_value_uint8_t(
  99. &scene_state->selected_control,
  100. 1,
  101. AddNewToken,
  102. AppSettings,
  103. RollOverflowBehaviorRoll);
  104. if(scene_state->selected_control == DeleteToken &&
  105. totp_token_info_iterator_get_total_count(iterator_context) == 0) {
  106. scene_state->selected_control++;
  107. }
  108. break;
  109. }
  110. case InputKeyRight:
  111. break;
  112. case InputKeyLeft:
  113. break;
  114. case InputKeyOk:
  115. break;
  116. case InputKeyBack: {
  117. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  118. break;
  119. }
  120. default:
  121. break;
  122. }
  123. } else if(event->input.type == InputTypeRelease && event->input.key == InputKeyOk) {
  124. switch(scene_state->selected_control) {
  125. case AddNewToken: {
  126. totp_scene_director_activate_scene(plugin_state, TotpSceneAddNewToken);
  127. break;
  128. }
  129. case DeleteToken: {
  130. DialogMessage* message = dialog_message_alloc();
  131. dialog_message_set_buttons(message, "No", NULL, "Yes");
  132. dialog_message_set_header(message, "Confirmation", 0, 0, AlignLeft, AlignTop);
  133. dialog_message_set_text(
  134. message,
  135. "Are you sure want to delete?",
  136. SCREEN_WIDTH_CENTER,
  137. SCREEN_HEIGHT_CENTER,
  138. AlignCenter,
  139. AlignCenter);
  140. DialogMessageButton dialog_result =
  141. dialog_message_show(plugin_state->dialogs_app, message);
  142. dialog_message_free(message);
  143. TokenInfoIteratorContext* iterator_context =
  144. totp_config_get_token_iterator_context(plugin_state);
  145. if(dialog_result == DialogMessageButtonRight &&
  146. totp_token_info_iterator_get_total_count(iterator_context) > 0) {
  147. if(!totp_token_info_iterator_remove_current_token_info(iterator_context)) {
  148. totp_dialogs_config_updating_error(plugin_state);
  149. return false;
  150. }
  151. totp_scene_director_activate_scene(plugin_state, TotpSceneGenerateToken);
  152. }
  153. break;
  154. }
  155. case AppSettings: {
  156. totp_scene_director_activate_scene(plugin_state, TotpSceneAppSettings);
  157. break;
  158. }
  159. default:
  160. break;
  161. }
  162. }
  163. return true;
  164. }
  165. void totp_scene_token_menu_deactivate(PluginState* plugin_state) {
  166. if(plugin_state->current_scene_state == NULL) return;
  167. free(plugin_state->current_scene_state);
  168. plugin_state->current_scene_state = NULL;
  169. }