lfrfid_app_scene_delete_confirm.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "lfrfid_app_scene_delete_confirm.h"
  2. #include "../view/elements/button_element.h"
  3. #include "../view/elements/icon_element.h"
  4. #include "../view/elements/string_element.h"
  5. void LfRfidAppSceneDeleteConfirm::on_enter(LfRfidApp* app, bool /* need_restore */) {
  6. string_init(string_data);
  7. string_init(string_header);
  8. auto container = app->view_controller.get<ContainerVM>();
  9. auto button = container->add<ButtonElement>();
  10. button->set_type(ButtonElement::Type::Left, "Back");
  11. button->set_callback(app, LfRfidAppSceneDeleteConfirm::back_callback);
  12. button = container->add<ButtonElement>();
  13. button->set_type(ButtonElement::Type::Right, "Delete");
  14. button->set_callback(app, LfRfidAppSceneDeleteConfirm::delete_callback);
  15. auto line_1 = container->add<StringElement>();
  16. auto line_2 = container->add<StringElement>();
  17. auto line_3 = container->add<StringElement>();
  18. size_t size = protocol_dict_get_data_size(app->dict, app->protocol_id);
  19. uint8_t* data = (uint8_t*)malloc(size);
  20. protocol_dict_get_data(app->dict, app->protocol_id, data, size);
  21. for(uint8_t i = 0; i < MIN(size, (size_t)8); i++) {
  22. if(i != 0) {
  23. string_cat_printf(string_data, " ");
  24. }
  25. string_cat_printf(string_data, "%02X", data[i]);
  26. }
  27. free(data);
  28. string_printf(string_header, "Delete %s?", string_get_cstr(app->file_name));
  29. line_1->set_text(
  30. string_get_cstr(string_header), 64, 0, 128 - 2, AlignCenter, AlignTop, FontPrimary);
  31. line_2->set_text(
  32. string_get_cstr(string_data), 64, 19, 0, AlignCenter, AlignTop, FontSecondary);
  33. line_3->set_text(
  34. protocol_dict_get_name(app->dict, app->protocol_id),
  35. 64,
  36. 49,
  37. 0,
  38. AlignCenter,
  39. AlignBottom,
  40. FontSecondary);
  41. app->view_controller.switch_to<ContainerVM>();
  42. }
  43. bool LfRfidAppSceneDeleteConfirm::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
  44. bool consumed = false;
  45. if(event->type == LfRfidApp::EventType::Next) {
  46. app->delete_key();
  47. app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::DeleteSuccess);
  48. consumed = true;
  49. } else if(event->type == LfRfidApp::EventType::Stay) {
  50. app->scene_controller.switch_to_previous_scene();
  51. consumed = true;
  52. } else if(event->type == LfRfidApp::EventType::Back) {
  53. consumed = true;
  54. }
  55. return consumed;
  56. }
  57. void LfRfidAppSceneDeleteConfirm::on_exit(LfRfidApp* app) {
  58. app->view_controller.get<ContainerVM>()->clean();
  59. string_clear(string_data);
  60. string_clear(string_header);
  61. }
  62. void LfRfidAppSceneDeleteConfirm::back_callback(void* context) {
  63. LfRfidApp* app = static_cast<LfRfidApp*>(context);
  64. LfRfidApp::Event event;
  65. event.type = LfRfidApp::EventType::Stay;
  66. app->view_controller.send_event(&event);
  67. }
  68. void LfRfidAppSceneDeleteConfirm::delete_callback(void* context) {
  69. LfRfidApp* app = static_cast<LfRfidApp*>(context);
  70. LfRfidApp::Event event;
  71. event.type = LfRfidApp::EventType::Next;
  72. app->view_controller.send_event(&event);
  73. }