picopass_scene_save_name.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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();
  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. } else {
  33. furi_string_set(folder_path, PICOPASS_APP_FOLDER);
  34. }
  35. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  36. furi_string_get_cstr(folder_path), PICOPASS_APP_EXTENSION, picopass->dev->dev_name);
  37. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  38. view_dispatcher_switch_to_view(picopass->view_dispatcher, PicopassViewTextInput);
  39. furi_string_free(folder_path);
  40. }
  41. bool picopass_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  42. Picopass* picopass = context;
  43. bool consumed = false;
  44. if(event.type == SceneManagerEventTypeCustom) {
  45. if(event.event == PicopassCustomEventTextInputDone) {
  46. if(strcmp(picopass->dev->dev_name, "")) {
  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. }