weebo_scene_save_name.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. weebo_text_store_set(weebo, weebo->file_name);
  24. }
  25. text_input_set_header_text(text_input, "Name the card");
  26. text_input_set_result_callback(
  27. text_input,
  28. weebo_scene_save_name_text_input_callback,
  29. weebo,
  30. weebo->text_store,
  31. sizeof(weebo->text_store),
  32. file_name_empty);
  33. FuriString* folder_path;
  34. folder_path = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  35. if(furi_string_end_with(weebo->load_path, NFC_APP_EXTENSION)) {
  36. path_extract_dirname(furi_string_get_cstr(weebo->load_path), folder_path);
  37. }
  38. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  39. furi_string_get_cstr(folder_path), NFC_APP_EXTENSION, weebo->file_name);
  40. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  41. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewTextInput);
  42. furi_string_free(folder_path);
  43. }
  44. bool weebo_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  45. Weebo* weebo = context;
  46. bool consumed = false;
  47. if(event.type == SceneManagerEventTypeCustom) {
  48. if(event.event == WeeboCustomEventTextInputDone) {
  49. strlcpy(weebo->file_name, weebo->text_store, strlen(weebo->text_store) + 1);
  50. FuriString* path = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  51. if(furi_string_end_with(weebo->load_path, NFC_APP_EXTENSION)) {
  52. path_extract_dirname(furi_string_get_cstr(weebo->load_path), path);
  53. }
  54. furi_string_cat_printf(path, "/%s%s", weebo->file_name, NFC_APP_EXTENSION);
  55. FURI_LOG_D(TAG, "Saving to %s", furi_string_get_cstr(path));
  56. if(nfc_device_save(weebo->nfc_device, furi_string_get_cstr(path))) {
  57. scene_manager_next_scene(weebo->scene_manager, WeeboSceneSaveSuccess);
  58. consumed = true;
  59. } else {
  60. consumed = scene_manager_search_and_switch_to_previous_scene(
  61. weebo->scene_manager, WeeboSceneMainMenu);
  62. }
  63. furi_string_free(path);
  64. }
  65. }
  66. return consumed;
  67. }
  68. void weebo_scene_save_name_on_exit(void* context) {
  69. Weebo* weebo = context;
  70. // Clear view
  71. void* validator_context = text_input_get_validator_callback_context(weebo->text_input);
  72. text_input_set_validator(weebo->text_input, NULL, NULL);
  73. validator_is_file_free(validator_context);
  74. text_input_reset(weebo->text_input);
  75. }