irda_app_scene_edit_rename.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "../irda_app.h"
  2. void IrdaAppSceneEditRename::on_enter(IrdaApp* app) {
  3. IrdaAppViewManager* 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() == IrdaApp::EditElement::Button) {
  8. furi_assert(app->get_current_button() != IrdaApp::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 = IrdaAppRemoteManager::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 = IrdaAppRemoteManager::max_remote_name_length;
  20. text_input_set_header_text(text_input, "Name the remote");
  21. ValidatorIsFile* validator_is_file =
  22. validator_is_file_alloc_init(app->irda_directory, app->irda_extension);
  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. IrdaApp::text_input_callback,
  28. app,
  29. app->get_text_store(0),
  30. enter_name_length,
  31. true);
  32. view_manager->switch_to(IrdaAppViewManager::ViewType::TextInput);
  33. }
  34. bool IrdaAppSceneEditRename::on_event(IrdaApp* app, IrdaAppEvent* event) {
  35. bool consumed = false;
  36. if(event->type == IrdaAppEvent::Type::TextEditDone) {
  37. auto remote_manager = app->get_remote_manager();
  38. bool result = false;
  39. if(app->get_edit_element() == IrdaApp::EditElement::Button) {
  40. result =
  41. remote_manager->rename_button(app->get_current_button(), app->get_text_store(0));
  42. app->set_current_button(IrdaApp::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. {IrdaApp::Scene::Start, IrdaApp::Scene::RemoteList});
  49. } else {
  50. app->switch_to_next_scene_without_saving(IrdaApp::Scene::EditRenameDone);
  51. }
  52. consumed = true;
  53. }
  54. return consumed;
  55. }
  56. void IrdaAppSceneEditRename::on_exit(IrdaApp* 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. }