nfc_scene_save_name.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "../nfc_i.h"
  2. #include <lib/toolbox/random_name.h>
  3. #define SCENE_SAVE_NAME_CUSTOM_EVENT (0UL)
  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, SCENE_SAVE_NAME_CUSTOM_EVENT);
  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. view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextInput);
  28. }
  29. bool nfc_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  30. Nfc* nfc = (Nfc*)context;
  31. if(event.type == SceneManagerEventTypeCustom) {
  32. if(event.event == SCENE_SAVE_NAME_CUSTOM_EVENT) {
  33. if(strcmp(nfc->dev->dev_name, "")) {
  34. nfc_device_delete(nfc->dev);
  35. }
  36. if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetUid)) {
  37. nfc->dev->dev_data.nfc_data = nfc->dev_edit_data;
  38. }
  39. strlcpy(nfc->dev->dev_name, nfc->text_store, strlen(nfc->text_store) + 1);
  40. if(nfc_device_save(nfc->dev, nfc->text_store)) {
  41. scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
  42. return true;
  43. } else {
  44. return scene_manager_search_and_switch_to_previous_scene(
  45. nfc->scene_manager, NfcSceneStart);
  46. }
  47. }
  48. }
  49. return false;
  50. }
  51. void nfc_scene_save_name_on_exit(void* context) {
  52. Nfc* nfc = (Nfc*)context;
  53. // Clear view
  54. text_input_clean(nfc->text_input);
  55. }