seos_scene_save_name.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "../seos_i.h"
  2. #include <lib/toolbox/name_generator.h>
  3. #include <gui/modules/validators.h>
  4. #include <toolbox/path.h>
  5. #define SEOS_APP_FILE_PREFIX "Seos"
  6. void seos_scene_save_name_text_input_callback(void* context) {
  7. Seos* seos = context;
  8. view_dispatcher_send_custom_event(seos->view_dispatcher, SeosCustomEventTextInputDone);
  9. }
  10. void seos_scene_save_name_on_enter(void* context) {
  11. Seos* seos = context;
  12. // Setup view
  13. TextInput* text_input = seos->text_input;
  14. bool dev_name_empty = false;
  15. if(!strcmp(seos->credential->name, "")) {
  16. name_generator_make_auto(seos->text_store, sizeof(seos->text_store), SEOS_APP_FILE_PREFIX);
  17. dev_name_empty = true;
  18. } else {
  19. seos_text_store_set(seos, seos->credential->name);
  20. }
  21. text_input_set_header_text(text_input, "Name the card");
  22. text_input_set_result_callback(
  23. text_input,
  24. seos_scene_save_name_text_input_callback,
  25. seos,
  26. seos->text_store,
  27. sizeof(seos->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(seos->credential->load_path, SEOS_APP_EXTENSION)) {
  32. path_extract_dirname(furi_string_get_cstr(seos->credential->load_path), folder_path);
  33. }
  34. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  35. furi_string_get_cstr(folder_path), SEOS_APP_EXTENSION, seos->credential->name);
  36. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  37. view_dispatcher_switch_to_view(seos->view_dispatcher, SeosViewTextInput);
  38. furi_string_free(folder_path);
  39. }
  40. bool seos_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  41. Seos* seos = context;
  42. bool consumed = false;
  43. if(event.type == SceneManagerEventTypeCustom) {
  44. if(event.event == SeosCustomEventTextInputDone) {
  45. strlcpy(seos->credential->name, seos->text_store, strlen(seos->text_store) + 1);
  46. if(seos_credential_save(seos->credential, seos->text_store)) {
  47. scene_manager_next_scene(seos->scene_manager, SeosSceneSaveSuccess);
  48. consumed = true;
  49. } else {
  50. consumed = scene_manager_search_and_switch_to_previous_scene(
  51. seos->scene_manager, SeosSceneMainMenu);
  52. }
  53. }
  54. }
  55. return consumed;
  56. }
  57. void seos_scene_save_name_on_exit(void* context) {
  58. Seos* seos = context;
  59. // Clear view
  60. void* validator_context = text_input_get_validator_callback_context(seos->text_input);
  61. text_input_set_validator(seos->text_input, NULL, NULL);
  62. validator_is_file_free(validator_context);
  63. text_input_reset(seos->text_input);
  64. }