picopass_scene_save_name.c 3.0 KB

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