nfc_scene_save_name.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_and_switch_to_previous_scene(
  42. nfc->scene_manager, NfcSceneStart);
  43. }
  44. }
  45. }
  46. return false;
  47. }
  48. const void nfc_scene_save_name_on_exit(void* context) {
  49. Nfc* nfc = (Nfc*)context;
  50. // Clear view
  51. text_input_set_header_text(nfc->text_input, NULL);
  52. text_input_set_result_callback(nfc->text_input, NULL, NULL, NULL, 0, false);
  53. }