nfc_scene_save_name.c 2.5 KB

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