lfrfid_app_scene_save_name.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "lfrfid_app_scene_save_name.h"
  2. #include "m-string.h"
  3. #include <lib/toolbox/random_name.h>
  4. #include <lib/toolbox/path.h>
  5. void LfRfidAppSceneSaveName::on_enter(LfRfidApp* app, bool /* need_restore */) {
  6. const char* key_name = string_get_cstr(app->file_name);
  7. bool key_name_empty = (string_size(app->file_name) == 0);
  8. if(key_name_empty) {
  9. string_set_str(app->file_path, app->app_folder);
  10. set_random_name(app->text_store.text, app->text_store.text_size);
  11. } else {
  12. app->text_store.set("%s", key_name);
  13. }
  14. auto text_input = app->view_controller.get<TextInputVM>();
  15. text_input->set_header_text("Name the card");
  16. text_input->set_result_callback(
  17. save_callback, app, app->text_store.text, LFRFID_KEY_NAME_SIZE, key_name_empty);
  18. string_t folder_path;
  19. string_init(folder_path);
  20. path_extract_dirname(string_get_cstr(app->file_path), folder_path);
  21. ValidatorIsFile* validator_is_file =
  22. validator_is_file_alloc_init(string_get_cstr(folder_path), app->app_extension, key_name);
  23. text_input->set_validator(validator_is_file_callback, validator_is_file);
  24. string_clear(folder_path);
  25. app->view_controller.switch_to<TextInputVM>();
  26. }
  27. bool LfRfidAppSceneSaveName::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  28. bool consumed = false;
  29. if(event->type == LfRfidApp::EventType::Next) {
  30. if(string_size(app->file_name) > 0) {
  31. app->delete_key();
  32. }
  33. string_set_str(app->file_name, app->text_store.text);
  34. if(app->save_key()) {
  35. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SaveSuccess);
  36. } else {
  37. app->scene_controller.search_and_switch_to_previous_scene(
  38. {LfRfidApp::SceneType::ReadKeyMenu});
  39. }
  40. }
  41. return consumed;
  42. }
  43. void LfRfidAppSceneSaveName::on_exit(LfRfidApp* app) {
  44. void* validator_context =
  45. app->view_controller.get<TextInputVM>()->get_validator_callback_context();
  46. app->view_controller.get<TextInputVM>()->set_validator(NULL, NULL);
  47. validator_is_file_free((ValidatorIsFile*)validator_context);
  48. app->view_controller.get<TextInputVM>()->clean();
  49. }
  50. void LfRfidAppSceneSaveName::save_callback(void* context) {
  51. LfRfidApp* app = static_cast<LfRfidApp*>(context);
  52. LfRfidApp::Event event;
  53. event.type = LfRfidApp::EventType::Next;
  54. app->view_controller.send_event(&event);
  55. }