subrem_scene_edit_label.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "../subghz_remote_app_i.h"
  2. #include <lib/toolbox/path.h>
  3. typedef enum {
  4. SubRemSceneEditLabelStateTextInput,
  5. SubRemSceneEditLabelStateWidget,
  6. } SubRemSceneEditLabelState;
  7. void subrem_scene_edit_label_text_input_callback(void* context) {
  8. furi_assert(context);
  9. SubGhzRemoteApp* app = context;
  10. view_dispatcher_send_custom_event(
  11. app->view_dispatcher, SubRemCustomEventSceneEditLabelInputDone);
  12. }
  13. void subrem_scene_edit_label_widget_callback(GuiButtonType result, InputType type, void* context) {
  14. furi_assert(context);
  15. SubGhzRemoteApp* app = context;
  16. if((result == GuiButtonTypeCenter) && (type == InputTypeShort)) {
  17. view_dispatcher_send_custom_event(
  18. app->view_dispatcher, SubRemCustomEventSceneEditLabelWidgetAcces);
  19. } else if((result == GuiButtonTypeLeft) && (type == InputTypeShort)) {
  20. view_dispatcher_send_custom_event(
  21. app->view_dispatcher, SubRemCustomEventSceneEditLabelWidgetBack);
  22. }
  23. }
  24. void subrem_scene_edit_label_on_enter(void* context) {
  25. SubGhzRemoteApp* app = context;
  26. SubRemSubFilePreset* sub_preset = app->map_preset->subs_preset[app->chosen_sub];
  27. FuriString* temp_str = furi_string_alloc();
  28. if(furi_string_empty(sub_preset->label)) {
  29. if(furi_string_empty(sub_preset->file_path)) {
  30. path_extract_filename(sub_preset->file_path, temp_str, true);
  31. strcpy(app->file_name_tmp, furi_string_get_cstr(temp_str));
  32. } else {
  33. strcpy(app->file_name_tmp, "");
  34. }
  35. } else {
  36. strcpy(app->file_name_tmp, furi_string_get_cstr(sub_preset->label));
  37. }
  38. TextInput* text_input = app->text_input;
  39. text_input_set_header_text(text_input, "Label name");
  40. text_input_set_result_callback(
  41. text_input,
  42. subrem_scene_edit_label_text_input_callback,
  43. app,
  44. app->file_name_tmp,
  45. 25,
  46. false);
  47. #ifndef FW_ORIGIN_Official
  48. text_input_set_minimum_length(app->text_input, 0);
  49. #endif
  50. widget_add_string_element(
  51. app->widget, 63, 12, AlignCenter, AlignCenter, FontPrimary, "Empty Label Name");
  52. widget_add_string_element(
  53. app->widget, 63, 32, AlignCenter, AlignCenter, FontSecondary, "Continue?");
  54. widget_add_button_element(
  55. app->widget, GuiButtonTypeCenter, "Ok", subrem_scene_edit_label_widget_callback, app);
  56. widget_add_button_element(
  57. app->widget, GuiButtonTypeLeft, "Back", subrem_scene_edit_label_widget_callback, app);
  58. scene_manager_set_scene_state(
  59. app->scene_manager, SubRemSceneEditLabel, SubRemSceneEditLabelStateTextInput);
  60. view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewIDTextInput);
  61. furi_string_free(temp_str);
  62. }
  63. bool subrem_scene_edit_label_on_event(void* context, SceneManagerEvent event) {
  64. SubGhzRemoteApp* app = context;
  65. FuriString* label = app->map_preset->subs_preset[app->chosen_sub]->label;
  66. if(event.type == SceneManagerEventTypeBack) {
  67. if(scene_manager_get_scene_state(app->scene_manager, SubRemSceneEditLabel) ==
  68. SubRemSceneEditLabelStateWidget) {
  69. scene_manager_set_scene_state(
  70. app->scene_manager, SubRemSceneEditLabel, SubRemSceneEditLabelStateTextInput);
  71. view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewIDTextInput);
  72. return true;
  73. } else if(
  74. scene_manager_get_scene_state(app->scene_manager, SubRemSceneEditLabel) ==
  75. SubRemSceneEditLabelStateTextInput) {
  76. scene_manager_previous_scene(app->scene_manager);
  77. return true;
  78. }
  79. scene_manager_previous_scene(app->scene_manager);
  80. return true;
  81. } else if(event.type == SceneManagerEventTypeCustom) {
  82. if(event.event == SubRemCustomEventSceneEditLabelInputDone) {
  83. if(strcmp(app->file_name_tmp, "") == 0) {
  84. scene_manager_set_scene_state(
  85. app->scene_manager, SubRemSceneEditLabel, SubRemSceneEditLabelStateWidget);
  86. view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewIDWidget);
  87. } else {
  88. furi_string_set(label, app->file_name_tmp);
  89. app->map_not_saved = true;
  90. scene_manager_previous_scene(app->scene_manager);
  91. }
  92. return true;
  93. } else if(event.event == SubRemCustomEventSceneEditLabelWidgetAcces) {
  94. furi_string_set(label, app->file_name_tmp);
  95. app->map_not_saved = true;
  96. scene_manager_previous_scene(app->scene_manager);
  97. return true;
  98. } else if(event.event == SubRemCustomEventSceneEditLabelWidgetBack) {
  99. scene_manager_set_scene_state(
  100. app->scene_manager, SubRemSceneEditLabel, SubRemSceneEditLabelStateTextInput);
  101. view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewIDTextInput);
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. void subrem_scene_edit_label_on_exit(void* context) {
  108. SubGhzRemoteApp* app = context;
  109. // Clear view
  110. text_input_reset(app->text_input);
  111. widget_reset(app->widget);
  112. }