lfrfid_scene_save_name.c 2.6 KB

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