ibutton_scene_save_name.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "../ibutton_i.h"
  2. #include <lib/toolbox/random_name.h>
  3. static void ibutton_scene_save_name_text_input_callback(void* context) {
  4. iButton* ibutton = context;
  5. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventTextEditResult);
  6. }
  7. void ibutton_scene_save_name_on_enter(void* context) {
  8. iButton* ibutton = context;
  9. TextInput* text_input = ibutton->text_input;
  10. const char* key_name = ibutton_key_get_name_p(ibutton->key);
  11. const bool key_name_is_empty = !strcmp(key_name, "");
  12. if(key_name_is_empty) {
  13. set_random_name(ibutton->text_store, IBUTTON_TEXT_STORE_SIZE);
  14. } else {
  15. ibutton_text_store_set(ibutton, "%s", key_name);
  16. }
  17. text_input_set_header_text(text_input, "Name the key");
  18. text_input_set_result_callback(
  19. text_input,
  20. ibutton_scene_save_name_text_input_callback,
  21. ibutton,
  22. ibutton->text_store,
  23. IBUTTON_KEY_NAME_SIZE,
  24. key_name_is_empty);
  25. ValidatorIsFile* validator_is_file =
  26. validator_is_file_alloc_init(IBUTTON_APP_FOLDER, IBUTTON_APP_EXTENSION, key_name);
  27. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  28. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewTextInput);
  29. }
  30. bool ibutton_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  31. iButton* ibutton = context;
  32. bool consumed = false;
  33. if(event.type == SceneManagerEventTypeCustom) {
  34. consumed = true;
  35. if(event.event == iButtonCustomEventTextEditResult) {
  36. if(ibutton_save_key(ibutton, ibutton->text_store)) {
  37. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSaveSuccess);
  38. } else {
  39. const uint32_t possible_scenes[] = {
  40. iButtonSceneReadKeyMenu, iButtonSceneSavedKeyMenu, iButtonSceneAddType};
  41. ibutton_switch_to_previous_scene_one_of(
  42. ibutton, possible_scenes, sizeof(possible_scenes) / sizeof(uint32_t));
  43. }
  44. }
  45. }
  46. return consumed;
  47. }
  48. void ibutton_scene_save_name_on_exit(void* context) {
  49. iButton* ibutton = context;
  50. TextInput* text_input = ibutton->text_input;
  51. void* validator_context = text_input_get_validator_callback_context(text_input);
  52. text_input_set_validator(text_input, NULL, NULL);
  53. validator_is_file_free((ValidatorIsFile*)validator_context);
  54. text_input_reset(text_input);
  55. }