nfc_scene_save_name.c 2.9 KB

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