picopass_scene_save_name.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "../picopass_i.h"
  2. #include <lib/toolbox/name_generator.h>
  3. #include <gui/modules/validators.h>
  4. #include <toolbox/path.h>
  5. void picopass_scene_save_name_text_input_callback(void* context) {
  6. Picopass* picopass = context;
  7. view_dispatcher_send_custom_event(picopass->view_dispatcher, PicopassCustomEventTextInputDone);
  8. }
  9. void picopass_scene_save_name_on_enter(void* context) {
  10. Picopass* picopass = context;
  11. // Setup view
  12. TextInput* text_input = picopass->text_input;
  13. bool dev_name_empty = false;
  14. if(!strcmp(picopass->dev->dev_name, "")) {
  15. name_generator_make_auto(
  16. picopass->text_store, sizeof(picopass->text_store), PICOPASS_APP_FILE_PREFIX);
  17. dev_name_empty = true;
  18. } else {
  19. picopass_text_store_set(picopass, picopass->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. picopass_scene_save_name_text_input_callback,
  25. picopass,
  26. picopass->text_store,
  27. sizeof(picopass->text_store),
  28. dev_name_empty);
  29. FuriString* folder_path;
  30. folder_path = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  31. if(furi_string_end_with(picopass->dev->load_path, PICOPASS_APP_EXTENSION)) {
  32. path_extract_dirname(furi_string_get_cstr(picopass->dev->load_path), folder_path);
  33. }
  34. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  35. furi_string_get_cstr(folder_path), PICOPASS_APP_EXTENSION, picopass->dev->dev_name);
  36. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  37. view_dispatcher_switch_to_view(picopass->view_dispatcher, PicopassViewTextInput);
  38. furi_string_free(folder_path);
  39. }
  40. bool picopass_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  41. Picopass* picopass = context;
  42. bool consumed = false;
  43. if(event.type == SceneManagerEventTypeCustom) {
  44. if(event.event == PicopassCustomEventTextInputDone) {
  45. // Delete old file if renaming
  46. if(strcmp(picopass->dev->dev_name, "") != 0) {
  47. picopass_device_delete(picopass->dev, true);
  48. }
  49. strlcpy(
  50. picopass->dev->dev_name, picopass->text_store, strlen(picopass->text_store) + 1);
  51. if(picopass_device_save(picopass->dev, picopass->text_store)) {
  52. scene_manager_next_scene(picopass->scene_manager, PicopassSceneSaveSuccess);
  53. consumed = true;
  54. } else {
  55. consumed = scene_manager_search_and_switch_to_previous_scene(
  56. picopass->scene_manager, PicopassSceneStart);
  57. }
  58. }
  59. }
  60. return consumed;
  61. }
  62. void picopass_scene_save_name_on_exit(void* context) {
  63. Picopass* picopass = context;
  64. // Clear view
  65. void* validator_context = text_input_get_validator_callback_context(picopass->text_input);
  66. text_input_set_validator(picopass->text_input, NULL, NULL);
  67. validator_is_file_free(validator_context);
  68. text_input_reset(picopass->text_input);
  69. }