picopass_scene_save_name.c 3.0 KB

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