ibutton_scene_save_name.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "../ibutton_i.h"
  2. #include <toolbox/random_name.h>
  3. #include <toolbox/path.h>
  4. #include <dolphin/dolphin.h>
  5. static void ibutton_scene_save_name_text_input_callback(void* context) {
  6. iButton* ibutton = context;
  7. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventTextEditResult);
  8. }
  9. void ibutton_scene_save_name_on_enter(void* context) {
  10. iButton* ibutton = context;
  11. TextInput* text_input = ibutton->text_input;
  12. const bool is_new_file = furi_string_empty(ibutton->file_path);
  13. if(is_new_file) {
  14. set_random_name(ibutton->key_name, IBUTTON_KEY_NAME_SIZE);
  15. }
  16. text_input_set_header_text(text_input, "Name the key");
  17. text_input_set_result_callback(
  18. text_input,
  19. ibutton_scene_save_name_text_input_callback,
  20. ibutton,
  21. ibutton->key_name,
  22. IBUTTON_KEY_NAME_SIZE,
  23. is_new_file);
  24. ValidatorIsFile* validator_is_file =
  25. validator_is_file_alloc_init(IBUTTON_APP_FOLDER, IBUTTON_APP_EXTENSION, ibutton->key_name);
  26. text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
  27. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewTextInput);
  28. }
  29. bool ibutton_scene_save_name_on_event(void* context, SceneManagerEvent event) {
  30. iButton* ibutton = context;
  31. bool consumed = false;
  32. if(event.type == SceneManagerEventTypeCustom) {
  33. consumed = true;
  34. if(event.event == iButtonCustomEventTextEditResult) {
  35. furi_string_printf(
  36. ibutton->file_path,
  37. "%s/%s%s",
  38. IBUTTON_APP_FOLDER,
  39. ibutton->key_name,
  40. IBUTTON_APP_EXTENSION);
  41. if(ibutton_save_key(ibutton)) {
  42. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneSaveSuccess);
  43. if(scene_manager_has_previous_scene(
  44. ibutton->scene_manager, iButtonSceneSavedKeyMenu)) {
  45. // Nothing, do not count editing as saving
  46. } else if(scene_manager_has_previous_scene(
  47. ibutton->scene_manager, iButtonSceneAddType)) {
  48. DOLPHIN_DEED(DolphinDeedIbuttonAdd);
  49. } else {
  50. DOLPHIN_DEED(DolphinDeedIbuttonSave);
  51. }
  52. } else {
  53. const uint32_t possible_scenes[] = {
  54. iButtonSceneReadKeyMenu, iButtonSceneSavedKeyMenu, iButtonSceneAddType};
  55. scene_manager_search_and_switch_to_previous_scene_one_of(
  56. ibutton->scene_manager, possible_scenes, COUNT_OF(possible_scenes));
  57. }
  58. }
  59. }
  60. return consumed;
  61. }
  62. void ibutton_scene_save_name_on_exit(void* context) {
  63. iButton* ibutton = context;
  64. TextInput* text_input = ibutton->text_input;
  65. void* validator_context = text_input_get_validator_callback_context(text_input);
  66. text_input_set_validator(text_input, NULL, NULL);
  67. validator_is_file_free((ValidatorIsFile*)validator_context);
  68. text_input_reset(text_input);
  69. }