lfrfid_scene_save_name.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <lib/toolbox/random_name.h>
  2. #include "../lfrfid_i.h"
  3. #include <dolphin/dolphin.h>
  4. void lfrfid_scene_save_name_on_enter(void* context) {
  5. LfRfid* app = context;
  6. TextInput* text_input = app->text_input;
  7. FuriString* folder_path;
  8. folder_path = furi_string_alloc();
  9. bool key_name_is_empty = furi_string_empty(app->file_name);
  10. if(key_name_is_empty) {
  11. furi_string_set(app->file_path, LFRFID_APP_FOLDER);
  12. set_random_name(app->text_store, LFRFID_TEXT_STORE_SIZE);
  13. furi_string_set(folder_path, LFRFID_APP_FOLDER);
  14. } else {
  15. lfrfid_text_store_set(app, "%s", furi_string_get_cstr(app->file_name));
  16. path_extract_dirname(furi_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", furi_string_get_cstr(folder_path), app->text_store);
  27. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  28. furi_string_get_cstr(folder_path),
  29. LFRFID_APP_EXTENSION,
  30. furi_string_get_cstr(app->file_name));
  31. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  32. furi_string_free(folder_path);
  33. view_dispatcher_switch_to_view(app->view_dispatcher, LfRfidViewTextInput);
  34. }
  35. bool lfrfid_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  36. LfRfid* app = context;
  37. SceneManager* scene_manager = app->scene_manager;
  38. bool consumed = false;
  39. if(event.type == SceneManagerEventTypeCustom) {
  40. if(event.event == LfRfidEventNext) {
  41. consumed = true;
  42. if(!furi_string_empty(app->file_name)) {
  43. lfrfid_delete_key(app);
  44. }
  45. furi_string_set(app->file_name, app->text_store);
  46. if(lfrfid_save_key(app)) {
  47. scene_manager_next_scene(scene_manager, LfRfidSceneSaveSuccess);
  48. if(scene_manager_has_previous_scene(scene_manager, LfRfidSceneSavedKeyMenu)) {
  49. // Nothing, do not count editing as saving
  50. } else if(scene_manager_has_previous_scene(scene_manager, LfRfidSceneSaveType)) {
  51. DOLPHIN_DEED(DolphinDeedRfidAdd);
  52. } else {
  53. DOLPHIN_DEED(DolphinDeedRfidSave);
  54. }
  55. } else {
  56. scene_manager_search_and_switch_to_previous_scene(
  57. scene_manager, LfRfidSceneReadKeyMenu);
  58. }
  59. }
  60. }
  61. return consumed;
  62. }
  63. void lfrfid_scene_save_name_on_exit(void* context) {
  64. LfRfid* app = context;
  65. TextInput* text_input = app->text_input;
  66. void* validator_context = text_input_get_validator_callback_context(text_input);
  67. text_input_set_validator(text_input, NULL, NULL);
  68. validator_is_file_free((ValidatorIsFile*)validator_context);
  69. text_input_reset(text_input);
  70. }