uhf_scene_save_name.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "../uhf_app_i.h"
  2. #include <lib/toolbox/name_generator.h>
  3. #include <gui/modules/validators.h>
  4. #include <toolbox/path.h>
  5. void uhf_scene_save_name_text_input_callback(void* context) {
  6. UHFApp* uhf_app = context;
  7. view_dispatcher_send_custom_event(uhf_app->view_dispatcher, UHFCustomEventTextInputDone);
  8. }
  9. void uhf_scene_save_name_on_enter(void* context) {
  10. UHFApp* uhf_app = context;
  11. // Setup view
  12. TextInput* text_input = uhf_app->text_input;
  13. name_generator_make_auto(uhf_app->text_store, sizeof(uhf_app->text_store), "UHF");
  14. text_input_set_header_text(text_input, "Name the tag");
  15. text_input_set_result_callback(
  16. text_input,
  17. uhf_scene_save_name_text_input_callback,
  18. uhf_app,
  19. uhf_app->text_store,
  20. UHF_DEV_NAME_MAX_LEN,
  21. true);
  22. FuriString* folder_path;
  23. folder_path = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  24. if(furi_string_end_with(uhf_app->uhf_device->load_path, UHF_APP_EXTENSION)) {
  25. path_extract_dirname(furi_string_get_cstr(uhf_app->uhf_device->load_path), folder_path);
  26. }
  27. ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
  28. furi_string_get_cstr(folder_path), UHF_APP_EXTENSION, uhf_app->uhf_device->dev_name);
  29. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  30. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewTextInput);
  31. furi_string_free(folder_path);
  32. }
  33. bool uhf_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  34. UHFApp* uhf_app = context;
  35. bool consumed = false;
  36. if(event.type == SceneManagerEventTypeCustom) {
  37. if(event.event == UHFCustomEventTextInputDone) {
  38. strlcpy(
  39. uhf_app->uhf_device->dev_name,
  40. uhf_app->text_store,
  41. strlen(uhf_app->text_store) + 1);
  42. if(uhf_device_save(uhf_app->uhf_device, uhf_app->text_store)) {
  43. scene_manager_next_scene(uhf_app->scene_manager, UHFSceneSaveSuccess);
  44. consumed = true;
  45. } else {
  46. consumed = scene_manager_search_and_switch_to_previous_scene(
  47. uhf_app->scene_manager, UHFSceneStart);
  48. }
  49. }
  50. }
  51. return consumed;
  52. }
  53. void uhf_scene_save_name_on_exit(void* context) {
  54. UHFApp* uhf_app = context;
  55. // Clear view
  56. void* validator_context = text_input_get_validator_callback_context(uhf_app->text_input);
  57. text_input_set_validator(uhf_app->text_input, NULL, NULL);
  58. validator_is_file_free(validator_context);
  59. text_input_reset(uhf_app->text_input);
  60. }