nfc_scene_save_name.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "../nfc_i.h"
  2. #include <lib/toolbox/random_name.h>
  3. #include <gui/modules/validators.h>
  4. #include <toolbox/path.h>
  5. #include <dolphin/dolphin.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. FuriString* folder_path;
  30. folder_path = furi_string_alloc();
  31. if(furi_string_end_with(nfc->dev->load_path, NFC_APP_EXTENSION)) {
  32. path_extract_dirname(furi_string_get_cstr(nfc->dev->load_path), folder_path);
  33. } else {
  34. furi_string_set(folder_path, NFC_APP_FOLDER);
  35. }
  36. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  37. furi_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. furi_string_free(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, "") != 0) {
  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_save_file(nfc)) {
  55. scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
  56. if(!scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSavedMenu)) {
  57. // Nothing, do not count editing as saving
  58. } else if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetType)) {
  59. DOLPHIN_DEED(DolphinDeedNfcAddSave);
  60. } else {
  61. DOLPHIN_DEED(DolphinDeedNfcSave);
  62. }
  63. consumed = true;
  64. } else {
  65. consumed = scene_manager_search_and_switch_to_previous_scene(
  66. nfc->scene_manager, NfcSceneStart);
  67. }
  68. }
  69. }
  70. return consumed;
  71. }
  72. void nfc_scene_save_name_on_exit(void* context) {
  73. Nfc* nfc = context;
  74. // Clear view
  75. void* validator_context = text_input_get_validator_callback_context(nfc->text_input);
  76. text_input_set_validator(nfc->text_input, NULL, NULL);
  77. validator_is_file_free(validator_context);
  78. text_input_reset(nfc->text_input);
  79. }