totp_app_settings.c 8.7 KB

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