nfc_scene_save_name.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. const 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. const 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(nfc->dev.dev_name) {
  34. nfc_device_delete(&nfc->dev);
  35. }
  36. memcpy(&nfc->dev.dev_name, nfc->text_store, strlen(nfc->text_store));
  37. if(nfc_device_save(&nfc->dev, nfc->text_store)) {
  38. scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
  39. return true;
  40. } else {
  41. return scene_manager_search_previous_scene(nfc->scene_manager, NfcSceneStart);
  42. }
  43. }
  44. }
  45. return false;
  46. }
  47. const void nfc_scene_save_name_on_exit(void* context) {
  48. Nfc* nfc = (Nfc*)context;
  49. // Clear view
  50. text_input_set_header_text(nfc->text_input, NULL);
  51. text_input_set_result_callback(nfc->text_input, NULL, NULL, NULL, 0, false);
  52. }