infrared_app_scene_edit_rename.cpp 2.9 KB

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