uhf_scene_save_name.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. bool dev_name_empty = false;
  16. set_random_name(uhf_app->text_store, sizeof(uhf_app->text_store));
  17. text_input_set_header_text(text_input, "Name the tag");
  18. text_input_set_result_callback(
  19. text_input,
  20. uhf_scene_save_name_text_input_callback,
  21. uhf_app,
  22. uhf_app->text_store,
  23. UHF_DEV_NAME_MAX_LEN,
  24. dev_name_empty);
  25. FuriString* folder_path;
  26. folder_path = furi_string_alloc_set(STORAGE_APP_DATA_PATH_PREFIX);
  27. // if(furi_string_end_with(uhf_app->dev->load_path, UHF_APP_EXTENSION)) {
  28. // path_extract_dirname(furi_string_get_cstr(uhf_app->dev->load_path), folder_path);
  29. // }
  30. ValidatorIsFile* validator_is_file =
  31. validator_is_file_alloc_init(furi_string_get_cstr(folder_path), UHF_APP_EXTENSION, "test");
  32. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  33. view_dispatcher_switch_to_view(uhf_app->view_dispatcher, UHFViewTextInput);
  34. furi_string_free(folder_path);
  35. }
  36. bool uhf_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  37. UHFApp* uhf_app = context;
  38. UHFResponseData* uhf_data_save = uhf_app->worker->data;
  39. bool consumed = false;
  40. if(event.type == SceneManagerEventTypeCustom) {
  41. if(event.event == UHFCustomEventTextInputDone) {
  42. if(uhf_save_data(uhf_data_save, uhf_app->storage, 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. }