infrared_app_scene_edit_rename.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "../infrared_app.h"
  2. #include "m-string.h"
  3. #include "toolbox/path.h"
  4. void InfraredAppSceneEditRename::on_enter(InfraredApp* app) {
  5. InfraredAppViewManager* view_manager = app->get_view_manager();
  6. TextInput* text_input = view_manager->get_text_input();
  7. size_t enter_name_length = 0;
  8. auto remote_manager = app->get_remote_manager();
  9. if(app->get_edit_element() == InfraredApp::EditElement::Button) {
  10. furi_assert(app->get_current_button() != InfraredApp::ButtonNA);
  11. auto button_name = remote_manager->get_button_name(app->get_current_button());
  12. char* buffer_str = app->get_text_store(0);
  13. size_t max_len = InfraredAppRemoteManager::max_button_name_length;
  14. strncpy(buffer_str, button_name.c_str(), max_len);
  15. buffer_str[max_len + 1] = 0;
  16. enter_name_length = max_len;
  17. text_input_set_header_text(text_input, "Name the button");
  18. } else {
  19. auto remote_name = remote_manager->get_remote_name();
  20. strncpy(app->get_text_store(0), remote_name.c_str(), app->get_text_store_size());
  21. enter_name_length = InfraredAppRemoteManager::max_remote_name_length;
  22. text_input_set_header_text(text_input, "Name the remote");
  23. string_t folder_path;
  24. string_init(folder_path);
  25. if(string_end_with_str_p(app->file_path, InfraredApp::infrared_extension)) {
  26. path_extract_dirname(string_get_cstr(app->file_path), folder_path);
  27. }
  28. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  29. string_get_cstr(folder_path), app->infrared_extension, remote_name.c_str());
  30. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  31. string_clear(folder_path);
  32. }
  33. text_input_set_result_callback(
  34. text_input,
  35. InfraredApp::text_input_callback,
  36. app,
  37. app->get_text_store(0),
  38. enter_name_length,
  39. false);
  40. view_manager->switch_to(InfraredAppViewManager::ViewId::TextInput);
  41. }
  42. bool InfraredAppSceneEditRename::on_event(InfraredApp* app, InfraredAppEvent* event) {
  43. bool consumed = false;
  44. if(event->type == InfraredAppEvent::Type::TextEditDone) {
  45. auto remote_manager = app->get_remote_manager();
  46. bool result = false;
  47. if(app->get_edit_element() == InfraredApp::EditElement::Button) {
  48. result =
  49. remote_manager->rename_button(app->get_current_button(), app->get_text_store(0));
  50. app->set_current_button(InfraredApp::ButtonNA);
  51. } else {
  52. result = remote_manager->rename_remote(app->get_text_store(0));
  53. }
  54. if(!result) {
  55. app->search_and_switch_to_previous_scene(
  56. {InfraredApp::Scene::Start, InfraredApp::Scene::RemoteList});
  57. } else {
  58. app->switch_to_next_scene_without_saving(InfraredApp::Scene::EditRenameDone);
  59. }
  60. consumed = true;
  61. }
  62. return consumed;
  63. }
  64. void InfraredAppSceneEditRename::on_exit(InfraredApp* app) {
  65. TextInput* text_input = app->get_view_manager()->get_text_input();
  66. void* validator_context = text_input_get_validator_callback_context(text_input);
  67. text_input_set_validator(text_input, NULL, NULL);
  68. if(validator_context != NULL) validator_is_file_free((ValidatorIsFile*)validator_context);
  69. }