uhf_scene_save_name.c 2.5 KB

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