nfc_scene_save_name.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "../nfc_i.h"
  2. #include <lib/toolbox/random_name.h>
  3. #include <gui/modules/validators.h>
  4. #define SCENE_SAVE_NAME_CUSTOM_EVENT (0UL)
  5. void nfc_scene_save_name_text_input_callback(void* context) {
  6. Nfc* nfc = (Nfc*)context;
  7. view_dispatcher_send_custom_event(nfc->view_dispatcher, SCENE_SAVE_NAME_CUSTOM_EVENT);
  8. }
  9. void nfc_scene_save_name_on_enter(void* context) {
  10. Nfc* nfc = (Nfc*)context;
  11. // Setup view
  12. TextInput* text_input = nfc->text_input;
  13. bool dev_name_empty = false;
  14. if(!strcmp(nfc->dev->dev_name, "")) {
  15. set_random_name(nfc->text_store, sizeof(nfc->text_store));
  16. dev_name_empty = true;
  17. } else {
  18. nfc_text_store_set(nfc, nfc->dev->dev_name);
  19. }
  20. text_input_set_header_text(text_input, "Name the card");
  21. text_input_set_result_callback(
  22. text_input,
  23. nfc_scene_save_name_text_input_callback,
  24. nfc,
  25. nfc->text_store,
  26. NFC_DEV_NAME_MAX_LEN,
  27. dev_name_empty);
  28. ValidatorIsFile* validator_is_file =
  29. validator_is_file_alloc_init(NFC_APP_FOLDER, NFC_APP_EXTENSION);
  30. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  31. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextInput);
  32. }
  33. bool nfc_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  34. Nfc* nfc = (Nfc*)context;
  35. if(event.type == SceneManagerEventTypeCustom) {
  36. if(event.event == SCENE_SAVE_NAME_CUSTOM_EVENT) {
  37. if(strcmp(nfc->dev->dev_name, "")) {
  38. nfc_device_delete(nfc->dev);
  39. }
  40. if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetUid)) {
  41. nfc->dev->dev_data.nfc_data = nfc->dev_edit_data;
  42. }
  43. strlcpy(nfc->dev->dev_name, nfc->text_store, strlen(nfc->text_store) + 1);
  44. if(nfc_device_save(nfc->dev, nfc->text_store)) {
  45. scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
  46. return true;
  47. } else {
  48. return scene_manager_search_and_switch_to_previous_scene(
  49. nfc->scene_manager, NfcSceneStart);
  50. }
  51. }
  52. }
  53. return false;
  54. }
  55. void nfc_scene_save_name_on_exit(void* context) {
  56. Nfc* nfc = (Nfc*)context;
  57. // Clear view
  58. void* validator_context = text_input_get_validator_callback_context(nfc->text_input);
  59. text_input_set_validator(nfc->text_input, NULL, NULL);
  60. validator_is_file_free(validator_context);
  61. text_input_reset(nfc->text_input);
  62. }