totp_app_settings.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "totp_app_settings.h"
  2. #include <math.h>
  3. #include <totp_icons.h>
  4. #include "../../ui_controls.h"
  5. #include "../../common_dialogs.h"
  6. #include "../../scene_director.h"
  7. #include "../token_menu/totp_scene_token_menu.h"
  8. #include "../../constants.h"
  9. #include "../../../services/config/config.h"
  10. #include "../../../services/convert/convert.h"
  11. #include "../../../lib/roll_value/roll_value.h"
  12. #include "../../../types/nullable.h"
  13. char* YES_NO_LIST[] = {"NO", "YES"};
  14. typedef enum { HoursInput, MinutesInput, Sound, Vibro, ConfirmButton } Control;
  15. typedef struct {
  16. int8_t tz_offset_hours;
  17. uint8_t tz_offset_minutes;
  18. bool notification_sound;
  19. bool notification_vibro;
  20. uint8_t y_offset;
  21. TotpNullable_uint16_t current_token_index;
  22. Control selected_control;
  23. } SceneState;
  24. void totp_scene_app_settings_init(const PluginState* plugin_state) {
  25. UNUSED(plugin_state);
  26. }
  27. void totp_scene_app_settings_activate(
  28. PluginState* plugin_state,
  29. const AppSettingsSceneContext* context) {
  30. SceneState* scene_state = malloc(sizeof(SceneState));
  31. furi_check(scene_state != NULL);
  32. plugin_state->current_scene_state = scene_state;
  33. if(context != NULL) {
  34. TOTP_NULLABLE_VALUE(scene_state->current_token_index, context->current_token_index);
  35. } else {
  36. TOTP_NULLABLE_NULL(scene_state->current_token_index);
  37. }
  38. float off_int;
  39. float off_dec = modff(plugin_state->timezone_offset, &off_int);
  40. scene_state->tz_offset_hours = off_int;
  41. scene_state->tz_offset_minutes = 60.0f * off_dec;
  42. scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
  43. scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
  44. }
  45. static void two_digit_to_str(int8_t num, char* str) {
  46. uint8_t index = 0;
  47. if(num < 0) {
  48. str[index++] = '-';
  49. num = -num;
  50. }
  51. uint8_t d1 = (num / 10) % 10;
  52. uint8_t d2 = num % 10;
  53. str[index++] = CONVERT_DIGIT_TO_CHAR(d1);
  54. str[index++] = CONVERT_DIGIT_TO_CHAR(d2);
  55. str[index++] = '\0';
  56. }
  57. void totp_scene_app_settings_render(Canvas* const canvas, const PluginState* plugin_state) {
  58. const SceneState* scene_state = plugin_state->current_scene_state;
  59. canvas_set_font(canvas, FontPrimary);
  60. canvas_draw_str_aligned(
  61. canvas, 0, 0 - scene_state->y_offset, AlignLeft, AlignTop, "Timezone offset");
  62. canvas_set_font(canvas, FontSecondary);
  63. char tmp_str[4];
  64. two_digit_to_str(scene_state->tz_offset_hours, &tmp_str[0]);
  65. canvas_draw_str_aligned(canvas, 0, 16 - scene_state->y_offset, AlignLeft, AlignTop, "Hours:");
  66. ui_control_select_render(
  67. canvas,
  68. 36,
  69. 10 - scene_state->y_offset,
  70. SCREEN_WIDTH - 36,
  71. &tmp_str[0],
  72. scene_state->selected_control == HoursInput);
  73. two_digit_to_str(scene_state->tz_offset_minutes, &tmp_str[0]);
  74. canvas_draw_str_aligned(
  75. canvas, 0, 34 - scene_state->y_offset, AlignLeft, AlignTop, "Minutes:");
  76. ui_control_select_render(
  77. canvas,
  78. 36,
  79. 28 - scene_state->y_offset,
  80. SCREEN_WIDTH - 36,
  81. &tmp_str[0],
  82. scene_state->selected_control == MinutesInput);
  83. canvas_draw_icon(
  84. canvas,
  85. SCREEN_WIDTH_CENTER - 5,
  86. SCREEN_HEIGHT - 5 - scene_state->y_offset,
  87. &I_totp_arrow_bottom_10x5);
  88. canvas_set_font(canvas, FontPrimary);
  89. canvas_draw_str_aligned(
  90. canvas, 0, 64 - scene_state->y_offset, AlignLeft, AlignTop, "Notifications");
  91. canvas_set_font(canvas, FontSecondary);
  92. canvas_draw_str_aligned(canvas, 0, 80 - scene_state->y_offset, AlignLeft, AlignTop, "Sound:");
  93. ui_control_select_render(
  94. canvas,
  95. 36,
  96. 74 - scene_state->y_offset,
  97. SCREEN_WIDTH - 36,
  98. YES_NO_LIST[scene_state->notification_sound],
  99. scene_state->selected_control == Sound);
  100. canvas_draw_str_aligned(canvas, 0, 98 - scene_state->y_offset, AlignLeft, AlignTop, "Vibro:");
  101. ui_control_select_render(
  102. canvas,
  103. 36,
  104. 92 - scene_state->y_offset,
  105. SCREEN_WIDTH - 36,
  106. YES_NO_LIST[scene_state->notification_vibro],
  107. scene_state->selected_control == Vibro);
  108. ui_control_button_render(
  109. canvas,
  110. SCREEN_WIDTH_CENTER - 24,
  111. 115 - scene_state->y_offset,
  112. 48,
  113. 13,
  114. "Confirm",
  115. scene_state->selected_control == ConfirmButton);
  116. }
  117. bool totp_scene_app_settings_handle_event(
  118. const PluginEvent* const event,
  119. PluginState* plugin_state) {
  120. if(event->type != EventTypeKey) {
  121. return true;
  122. }
  123. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  124. if(event->input.type != InputTypePress && event->input.type != InputTypeRepeat) {
  125. return true;
  126. }
  127. switch(event->input.key) {
  128. case InputKeyUp:
  129. totp_roll_value_uint8_t(
  130. &scene_state->selected_control,
  131. -1,
  132. HoursInput,
  133. ConfirmButton,
  134. RollOverflowBehaviorStop);
  135. if(scene_state->selected_control > MinutesInput) {
  136. scene_state->y_offset = 64;
  137. } else {
  138. scene_state->y_offset = 0;
  139. }
  140. break;
  141. case InputKeyDown:
  142. totp_roll_value_uint8_t(
  143. &scene_state->selected_control, 1, HoursInput, ConfirmButton, RollOverflowBehaviorStop);
  144. if(scene_state->selected_control > MinutesInput) {
  145. scene_state->y_offset = 64;
  146. } else {
  147. scene_state->y_offset = 0;
  148. }
  149. break;
  150. case InputKeyRight:
  151. if(scene_state->selected_control == HoursInput) {
  152. totp_roll_value_int8_t(
  153. &scene_state->tz_offset_hours, 1, -12, 12, RollOverflowBehaviorStop);
  154. } else if(scene_state->selected_control == MinutesInput) {
  155. totp_roll_value_uint8_t(
  156. &scene_state->tz_offset_minutes, 15, 0, 45, RollOverflowBehaviorRoll);
  157. } else if(scene_state->selected_control == Sound) {
  158. scene_state->notification_sound = !scene_state->notification_sound;
  159. } else if(scene_state->selected_control == Vibro) {
  160. scene_state->notification_vibro = !scene_state->notification_vibro;
  161. }
  162. break;
  163. case InputKeyLeft:
  164. if(scene_state->selected_control == HoursInput) {
  165. totp_roll_value_int8_t(
  166. &scene_state->tz_offset_hours, -1, -12, 12, RollOverflowBehaviorStop);
  167. } else if(scene_state->selected_control == MinutesInput) {
  168. totp_roll_value_uint8_t(
  169. &scene_state->tz_offset_minutes, -15, 0, 45, RollOverflowBehaviorRoll);
  170. } else if(scene_state->selected_control == Sound) {
  171. scene_state->notification_sound = !scene_state->notification_sound;
  172. } else if(scene_state->selected_control == Vibro) {
  173. scene_state->notification_vibro = !scene_state->notification_vibro;
  174. }
  175. break;
  176. case InputKeyOk:
  177. if(scene_state->selected_control == ConfirmButton) {
  178. plugin_state->timezone_offset = (float)scene_state->tz_offset_hours +
  179. (float)scene_state->tz_offset_minutes / 60.0f;
  180. plugin_state->notification_method =
  181. (scene_state->notification_sound ? NotificationMethodSound :
  182. NotificationMethodNone) |
  183. (scene_state->notification_vibro ? NotificationMethodVibro :
  184. NotificationMethodNone);
  185. if(totp_config_file_update_user_settings(plugin_state) !=
  186. TotpConfigFileUpdateSuccess) {
  187. totp_dialogs_config_updating_error(plugin_state);
  188. return false;
  189. }
  190. if(!scene_state->current_token_index.is_null) {
  191. TokenMenuSceneContext generate_scene_context = {
  192. .current_token_index = scene_state->current_token_index.value};
  193. totp_scene_director_activate_scene(
  194. plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  195. } else {
  196. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  197. }
  198. }
  199. break;
  200. case InputKeyBack: {
  201. if(!scene_state->current_token_index.is_null) {
  202. TokenMenuSceneContext generate_scene_context = {
  203. .current_token_index = scene_state->current_token_index.value};
  204. totp_scene_director_activate_scene(
  205. plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  206. } else {
  207. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  208. }
  209. break;
  210. }
  211. default:
  212. break;
  213. }
  214. return true;
  215. }
  216. void totp_scene_app_settings_deactivate(PluginState* plugin_state) {
  217. if(plugin_state->current_scene_state == NULL) return;
  218. free(plugin_state->current_scene_state);
  219. plugin_state->current_scene_state = NULL;
  220. }
  221. void totp_scene_app_settings_free(const PluginState* plugin_state) {
  222. UNUSED(plugin_state);
  223. }