infrared_scene_edit_rename.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "../infrared_i.h"
  2. #include <string.h>
  3. #include <toolbox/path.h>
  4. void infrared_scene_edit_rename_on_enter(void* context) {
  5. Infrared* infrared = context;
  6. InfraredRemote* remote = infrared->remote;
  7. TextInput* text_input = infrared->text_input;
  8. size_t enter_name_length = 0;
  9. const InfraredEditTarget edit_target = infrared->app_state.edit_target;
  10. if(edit_target == InfraredEditTargetButton) {
  11. text_input_set_header_text(text_input, "Name the button");
  12. const int32_t current_button_index = infrared->app_state.current_button_index;
  13. furi_assert(current_button_index != InfraredButtonIndexNone);
  14. InfraredRemoteButton* current_button =
  15. infrared_remote_get_button(remote, current_button_index);
  16. enter_name_length = INFRARED_MAX_BUTTON_NAME_LENGTH;
  17. strncpy(
  18. infrared->text_store[0],
  19. infrared_remote_button_get_name(current_button),
  20. enter_name_length);
  21. } else if(edit_target == InfraredEditTargetRemote) {
  22. text_input_set_header_text(text_input, "Name the remote");
  23. enter_name_length = INFRARED_MAX_REMOTE_NAME_LENGTH;
  24. strncpy(infrared->text_store[0], infrared_remote_get_name(remote), enter_name_length);
  25. FuriString* folder_path;
  26. folder_path = furi_string_alloc();
  27. if(furi_string_end_with(infrared->file_path, INFRARED_APP_EXTENSION)) {
  28. path_extract_dirname(furi_string_get_cstr(infrared->file_path), folder_path);
  29. }
  30. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  31. furi_string_get_cstr(folder_path),
  32. INFRARED_APP_EXTENSION,
  33. infrared_remote_get_name(remote));
  34. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  35. furi_string_free(folder_path);
  36. } else {
  37. furi_assert(0);
  38. }
  39. text_input_set_result_callback(
  40. text_input,
  41. infrared_text_input_callback,
  42. context,
  43. infrared->text_store[0],
  44. enter_name_length,
  45. false);
  46. view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewTextInput);
  47. }
  48. bool infrared_scene_edit_rename_on_event(void* context, SceneManagerEvent event) {
  49. Infrared* infrared = context;
  50. InfraredRemote* remote = infrared->remote;
  51. SceneManager* scene_manager = infrared->scene_manager;
  52. InfraredAppState* app_state = &infrared->app_state;
  53. bool consumed = false;
  54. if(event.type == SceneManagerEventTypeCustom) {
  55. if(event.event == InfraredCustomEventTypeTextEditDone) {
  56. bool success = false;
  57. const InfraredEditTarget edit_target = app_state->edit_target;
  58. if(edit_target == InfraredEditTargetButton) {
  59. const int32_t current_button_index = app_state->current_button_index;
  60. furi_assert(current_button_index != InfraredButtonIndexNone);
  61. success = infrared_remote_rename_button(
  62. remote, infrared->text_store[0], current_button_index);
  63. app_state->current_button_index = InfraredButtonIndexNone;
  64. } else if(edit_target == InfraredEditTargetRemote) {
  65. success = infrared_rename_current_remote(infrared, infrared->text_store[0]);
  66. } else {
  67. furi_assert(0);
  68. }
  69. if(success) {
  70. scene_manager_next_scene(scene_manager, InfraredSceneEditRenameDone);
  71. } else {
  72. scene_manager_search_and_switch_to_previous_scene(
  73. scene_manager, InfraredSceneRemoteList);
  74. }
  75. consumed = true;
  76. }
  77. }
  78. return consumed;
  79. }
  80. void infrared_scene_edit_rename_on_exit(void* context) {
  81. Infrared* infrared = context;
  82. TextInput* text_input = infrared->text_input;
  83. void* validator_context = text_input_get_validator_callback_context(text_input);
  84. text_input_set_validator(text_input, NULL, NULL);
  85. if(validator_context) {
  86. validator_is_file_free((ValidatorIsFile*)validator_context);
  87. }
  88. }