irda_app_scene_universal_common.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "../irda_app.h"
  2. #include "assets_icons.h"
  3. #include <dolphin/dolphin.h>
  4. #include "gui/modules/button_menu.h"
  5. #include "gui/modules/button_panel.h"
  6. #include "../view/irda_app_brut_view.h"
  7. #include "gui/view.h"
  8. #include "irda/irda_app_event.h"
  9. #include "irda/irda_app_view_manager.h"
  10. #include "irda/scene/irda_app_scene.h"
  11. void IrdaAppSceneUniversalCommon::irda_app_item_callback(void* context, uint32_t index) {
  12. IrdaApp* app = static_cast<IrdaApp*>(context);
  13. IrdaAppEvent event;
  14. event.type = IrdaAppEvent::Type::ButtonPanelPressed;
  15. event.payload.menu_index = index;
  16. app->get_view_manager()->send_event(&event);
  17. }
  18. static bool irda_popup_brut_input_callback(InputEvent* event, void* context) {
  19. furi_assert(context);
  20. furi_assert(event);
  21. auto app = static_cast<IrdaApp*>(context);
  22. bool consumed = false;
  23. if((event->type == InputTypeShort) && (event->key == InputKeyBack)) {
  24. consumed = true;
  25. IrdaAppEvent irda_event;
  26. irda_event.type = IrdaAppEvent::Type::Back;
  27. app->get_view_manager()->send_event(&irda_event);
  28. }
  29. return consumed;
  30. }
  31. void IrdaAppSceneUniversalCommon::remove_popup(IrdaApp* app) {
  32. auto button_panel = app->get_view_manager()->get_button_panel();
  33. button_panel_set_popup_draw_callback(button_panel, NULL, NULL);
  34. button_panel_set_popup_input_callback(button_panel, NULL, NULL);
  35. }
  36. void IrdaAppSceneUniversalCommon::show_popup(IrdaApp* app, int record_amount) {
  37. auto button_panel = app->get_view_manager()->get_button_panel();
  38. auto popup_brut = app->get_view_manager()->get_popup_brut();
  39. popup_brut_set_progress_max(popup_brut, record_amount);
  40. button_panel_set_popup_draw_callback(button_panel, popup_brut_draw_callback, popup_brut);
  41. button_panel_set_popup_input_callback(button_panel, irda_popup_brut_input_callback, app);
  42. }
  43. bool IrdaAppSceneUniversalCommon::progress_popup(IrdaApp* app) {
  44. bool result = popup_brut_increase_progress(app->get_view_manager()->get_popup_brut());
  45. auto button_panel = app->get_view_manager()->get_button_panel();
  46. with_view_model_cpp(button_panel_get_view(button_panel), void*, model, { return true; });
  47. return result;
  48. }
  49. bool IrdaAppSceneUniversalCommon::on_event(IrdaApp* app, IrdaAppEvent* event) {
  50. bool consumed = false;
  51. if(brute_force_started) {
  52. if(event->type == IrdaAppEvent::Type::Tick) {
  53. auto view_manager = app->get_view_manager();
  54. IrdaAppEvent tick_event = {.type = IrdaAppEvent::Type::Tick};
  55. view_manager->send_event(&tick_event);
  56. bool result = brute_force.send_next_bruteforce();
  57. if(result) {
  58. result = progress_popup(app);
  59. }
  60. if(!result) {
  61. brute_force.stop_bruteforce();
  62. brute_force_started = false;
  63. remove_popup(app);
  64. }
  65. consumed = true;
  66. } else if(event->type == IrdaAppEvent::Type::Back) {
  67. brute_force_started = false;
  68. brute_force.stop_bruteforce();
  69. remove_popup(app);
  70. consumed = true;
  71. }
  72. } else {
  73. if(event->type == IrdaAppEvent::Type::ButtonPanelPressed) {
  74. int record_amount = 0;
  75. if(brute_force.start_bruteforce(event->payload.menu_index, record_amount)) {
  76. DOLPHIN_DEED(DolphinDeedIrBruteForce);
  77. brute_force_started = true;
  78. show_popup(app, record_amount);
  79. } else {
  80. app->switch_to_previous_scene();
  81. }
  82. consumed = true;
  83. } else if(event->type == IrdaAppEvent::Type::Back) {
  84. app->switch_to_previous_scene();
  85. consumed = true;
  86. }
  87. }
  88. return consumed;
  89. }
  90. void IrdaAppSceneUniversalCommon::on_exit(IrdaApp* app) {
  91. IrdaAppViewManager* view_manager = app->get_view_manager();
  92. ButtonPanel* button_panel = view_manager->get_button_panel();
  93. button_panel_reset(button_panel);
  94. }