nfc_scene_save_name.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "../nfc_i.h"
  2. #include "m-string.h"
  3. #include <lib/toolbox/random_name.h>
  4. #include <gui/modules/validators.h>
  5. #include <toolbox/path.h>
  6. void nfc_scene_save_name_text_input_callback(void* context) {
  7. Nfc* nfc = context;
  8. view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventTextInputDone);
  9. }
  10. void nfc_scene_save_name_on_enter(void* context) {
  11. Nfc* nfc = context;
  12. // Setup view
  13. TextInput* text_input = nfc->text_input;
  14. bool dev_name_empty = false;
  15. if(!strcmp(nfc->dev->dev_name, "")) {
  16. set_random_name(nfc->text_store, sizeof(nfc->text_store));
  17. dev_name_empty = true;
  18. } else {
  19. nfc_text_store_set(nfc, nfc->dev->dev_name);
  20. }
  21. text_input_set_header_text(text_input, "Name the card");
  22. text_input_set_result_callback(
  23. text_input,
  24. nfc_scene_save_name_text_input_callback,
  25. nfc,
  26. nfc->text_store,
  27. NFC_DEV_NAME_MAX_LEN,
  28. dev_name_empty);
  29. string_t folder_path;
  30. string_init(folder_path);
  31. if(string_end_with_str_p(nfc->dev->load_path, NFC_APP_EXTENSION)) {
  32. path_extract_dirname(string_get_cstr(nfc->dev->load_path), folder_path);
  33. } else {
  34. string_set_str(folder_path, NFC_APP_FOLDER);
  35. }
  36. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  37. string_get_cstr(folder_path), NFC_APP_EXTENSION, nfc->dev->dev_name);
  38. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  39. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextInput);
  40. string_clear(folder_path);
  41. }
  42. bool nfc_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  43. Nfc* nfc = context;
  44. bool consumed = false;
  45. if(event.type == SceneManagerEventTypeCustom) {
  46. if(event.event == NfcCustomEventTextInputDone) {
  47. if(strcmp(nfc->dev->dev_name, "")) {
  48. nfc_device_delete(nfc->dev, true);
  49. }
  50. if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetUid)) {
  51. nfc->dev->dev_data.nfc_data = nfc->dev_edit_data;
  52. }
  53. strlcpy(nfc->dev->dev_name, nfc->text_store, strlen(nfc->text_store) + 1);
  54. if(nfc_device_save(nfc->dev, nfc->text_store)) {
  55. scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
  56. consumed = true;
  57. } else {
  58. consumed = scene_manager_search_and_switch_to_previous_scene(
  59. nfc->scene_manager, NfcSceneStart);
  60. }
  61. }
  62. }
  63. return consumed;
  64. }
  65. void nfc_scene_save_name_on_exit(void* context) {
  66. Nfc* nfc = context;
  67. // Clear view
  68. void* validator_context = text_input_get_validator_callback_context(nfc->text_input);
  69. text_input_set_validator(nfc->text_input, NULL, NULL);
  70. validator_is_file_free(validator_context);
  71. text_input_reset(nfc->text_input);
  72. }