weebo_scene_save_name.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "../weebo_i.h"
  2. #include <lib/toolbox/name_generator.h>
  3. #include <gui/modules/validators.h>
  4. #include <toolbox/path.h>
  5. #define NFC_APP_EXTENSION ".nfc"
  6. #define NFC_APP_PATH_PREFIX "/ext/nfc"
  7. #define WEEBO_APP_FILE_PREFIX "weebo_"
  8. #define TAG "WeeboSceneSaveName"
  9. void weebo_scene_save_name_text_input_callback(void* context) {
  10. Weebo* weebo = context;
  11. view_dispatcher_send_custom_event(weebo->view_dispatcher, WeeboCustomEventTextInputDone);
  12. }
  13. void weebo_scene_save_name_on_enter(void* context) {
  14. Weebo* weebo = context;
  15. // Setup view
  16. TextInput* text_input = weebo->text_input;
  17. bool file_name_empty = false;
  18. if(!strcmp(weebo->file_name, "")) {
  19. name_generator_make_auto(
  20. weebo->text_store, sizeof(weebo->text_store), WEEBO_APP_FILE_PREFIX);
  21. file_name_empty = true;
  22. } else {
  23. // Add "_" suffix to make name unique
  24. weebo->file_name[strlen(weebo->file_name)] = '_';
  25. weebo->file_name[strlen(weebo->file_name)] = '\0';
  26. weebo_text_store_set(weebo, weebo->file_name);
  27. }
  28. text_input_set_header_text(text_input, "Name the card");
  29. text_input_set_result_callback(
  30. text_input,
  31. weebo_scene_save_name_text_input_callback,
  32. weebo,
  33. weebo->text_store,
  34. sizeof(weebo->text_store),
  35. file_name_empty);
  36. FuriString* folder_path;
  37. folder_path = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  38. if(furi_string_end_with(weebo->load_path, NFC_APP_EXTENSION)) {
  39. path_extract_dirname(furi_string_get_cstr(weebo->load_path), folder_path);
  40. }
  41. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  42. furi_string_get_cstr(folder_path), NFC_APP_EXTENSION, weebo->file_name);
  43. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  44. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewTextInput);
  45. furi_string_free(folder_path);
  46. }
  47. bool weebo_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  48. Weebo* weebo = context;
  49. bool consumed = false;
  50. if(event.type == SceneManagerEventTypeCustom) {
  51. if(event.event == WeeboCustomEventTextInputDone) {
  52. strlcpy(weebo->file_name, weebo->text_store, strlen(weebo->text_store) + 1);
  53. FuriString* path = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  54. if(furi_string_end_with(weebo->load_path, NFC_APP_EXTENSION)) {
  55. path_extract_dirname(furi_string_get_cstr(weebo->load_path), path);
  56. }
  57. furi_string_cat_printf(path, "/%s%s", weebo->file_name, NFC_APP_EXTENSION);
  58. FURI_LOG_D(TAG, "Saving to %s", furi_string_get_cstr(path));
  59. if(nfc_device_save(weebo->nfc_device, furi_string_get_cstr(path))) {
  60. scene_manager_next_scene(weebo->scene_manager, WeeboSceneSaveSuccess);
  61. consumed = true;
  62. } else {
  63. consumed = scene_manager_search_and_switch_to_previous_scene(
  64. weebo->scene_manager, WeeboSceneMainMenu);
  65. }
  66. furi_string_free(path);
  67. }
  68. }
  69. return consumed;
  70. }
  71. void weebo_scene_save_name_on_exit(void* context) {
  72. Weebo* weebo = context;
  73. // Clear view
  74. void* validator_context = text_input_get_validator_callback_context(weebo->text_input);
  75. text_input_set_validator(weebo->text_input, NULL, NULL);
  76. validator_is_file_free(validator_context);
  77. text_input_reset(weebo->text_input);
  78. }