ibutton-scene-save-name.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "ibutton-scene-save-name.h"
  2. #include "../ibutton-app.h"
  3. #include "../ibutton-view-manager.h"
  4. #include "../ibutton-event.h"
  5. #include "../ibutton-key.h"
  6. #include <callback-connector.h>
  7. void iButtonSceneSaveName::on_enter(iButtonApp* app) {
  8. iButtonAppViewManager* view_manager = app->get_view_manager();
  9. TextInput* text_input = view_manager->get_text_input();
  10. auto callback = cbc::obtain_connector(this, &iButtonSceneSaveName::text_input_callback);
  11. iButtonKey* key = app->get_key();
  12. const char* key_name = key->get_name();
  13. if(strcmp(key_name, "") == 0) {
  14. app->generate_random_name(app->get_text_store(), app->get_text_store_size());
  15. }
  16. text_input_set_header_text(text_input, "Name the key");
  17. text_input_set_result_callback(
  18. text_input, callback, app, app->get_text_store(), app->get_text_store_size());
  19. view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewTextInput);
  20. }
  21. bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
  22. bool consumed = false;
  23. if(event->type == iButtonEvent::Type::EventTypeTextEditResult) {
  24. KeyStore* store = app->get_key_store();
  25. uint8_t key_index = store->add_key();
  26. iButtonKey* key = app->get_key();
  27. store->set_key_type(key_index, key->get_key_type());
  28. store->set_key_name(key_index, app->get_text_store());
  29. store->set_key_data(key_index, key->get_data(), key->get_size());
  30. app->switch_to_next_scene(iButtonApp::Scene::SceneSaveSuccess);
  31. consumed = true;
  32. }
  33. return consumed;
  34. }
  35. void iButtonSceneSaveName::on_exit(iButtonApp* app) {
  36. TextInput* text_input = app->get_view_manager()->get_text_input();
  37. text_input_set_header_text(text_input, "");
  38. text_input_set_result_callback(text_input, NULL, NULL, NULL, 0);
  39. }
  40. void iButtonSceneSaveName::text_input_callback(void* context, char* text) {
  41. iButtonApp* app = static_cast<iButtonApp*>(context);
  42. iButtonEvent event;
  43. event.type = iButtonEvent::Type::EventTypeTextEditResult;
  44. app->get_view_manager()->send_event(&event);
  45. }