lfrfid_scene_save_name.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "m-string.h"
  2. #include <lib/toolbox/random_name.h>
  3. #include "../lfrfid_i.h"
  4. void lfrfid_scene_save_name_on_enter(void* context) {
  5. LfRfid* app = context;
  6. TextInput* text_input = app->text_input;
  7. string_t folder_path;
  8. string_init(folder_path);
  9. bool key_name_is_empty = string_empty_p(app->file_name);
  10. if(key_name_is_empty) {
  11. string_set_str(app->file_path, LFRFID_APP_FOLDER);
  12. set_random_name(app->text_store, LFRFID_TEXT_STORE_SIZE);
  13. string_set_str(folder_path, LFRFID_APP_FOLDER);
  14. } else {
  15. lfrfid_text_store_set(app, "%s", string_get_cstr(app->file_name));
  16. path_extract_dirname(string_get_cstr(app->file_path), folder_path);
  17. }
  18. text_input_set_header_text(text_input, "Name the card");
  19. text_input_set_result_callback(
  20. text_input,
  21. lfrfid_text_input_callback,
  22. app,
  23. app->text_store,
  24. LFRFID_KEY_NAME_SIZE,
  25. key_name_is_empty);
  26. FURI_LOG_I("", "%s %s", string_get_cstr(folder_path), app->text_store);
  27. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  28. string_get_cstr(folder_path), LFRFID_APP_EXTENSION, string_get_cstr(app->file_name));
  29. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  30. string_clear(folder_path);
  31. view_dispatcher_switch_to_view(app->view_dispatcher, LfRfidViewTextInput);
  32. }
  33. bool lfrfid_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  34. LfRfid* app = context;
  35. SceneManager* scene_manager = app->scene_manager;
  36. bool consumed = false;
  37. if(event.type == SceneManagerEventTypeCustom) {
  38. if(event.event == LfRfidEventNext) {
  39. consumed = true;
  40. if(!string_empty_p(app->file_name)) {
  41. lfrfid_delete_key(app);
  42. }
  43. string_set_str(app->file_name, app->text_store);
  44. if(lfrfid_save_key(app)) {
  45. scene_manager_next_scene(scene_manager, LfRfidSceneSaveSuccess);
  46. } else {
  47. scene_manager_search_and_switch_to_previous_scene(
  48. scene_manager, LfRfidSceneReadKeyMenu);
  49. }
  50. }
  51. }
  52. return consumed;
  53. }
  54. void lfrfid_scene_save_name_on_exit(void* context) {
  55. LfRfid* app = context;
  56. TextInput* text_input = app->text_input;
  57. void* validator_context = text_input_get_validator_callback_context(text_input);
  58. text_input_set_validator(text_input, NULL, NULL);
  59. validator_is_file_free((ValidatorIsFile*)validator_context);
  60. text_input_reset(text_input);
  61. }