picopass_scene_save_name.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. if(strcmp(picopass->dev->dev_name, "") != 0) {
  45. // picopass_device_delete(picopass->dev, true);
  46. }
  47. strlcpy(
  48. picopass->dev->dev_name, picopass->text_store, strlen(picopass->text_store) + 1);
  49. if(picopass_device_save(picopass->dev, picopass->text_store)) {
  50. scene_manager_next_scene(picopass->scene_manager, PicopassSceneSaveSuccess);
  51. consumed = true;
  52. } else {
  53. consumed = scene_manager_search_and_switch_to_previous_scene(
  54. picopass->scene_manager, PicopassSceneStart);
  55. }
  56. }
  57. }
  58. return consumed;
  59. }
  60. void picopass_scene_save_name_on_exit(void* context) {
  61. Picopass* picopass = context;
  62. // Clear view
  63. void* validator_context = text_input_get_validator_callback_context(picopass->text_input);
  64. text_input_set_validator(picopass->text_input, NULL, NULL);
  65. validator_is_file_free(validator_context);
  66. text_input_reset(picopass->text_input);
  67. }