scene_items.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <furi.h>
  2. #include <gui/view_dispatcher.h>
  3. #include <gui/scene_manager.h>
  4. #include <gui/modules/dialog_ex.h>
  5. #include <notification/notification_messages.h>
  6. #include "quac.h"
  7. #include "scenes.h"
  8. #include "scene_items.h"
  9. #include "../actions/action.h"
  10. #include "../views/action_menu.h"
  11. #include <lib/toolbox/path.h>
  12. static const ActionMenuItemType ItemToMenuItem[] = {
  13. [Item_SubGhz] = ActionMenuItemTypeSubGHz,
  14. [Item_RFID] = ActionMenuItemTypeRFID,
  15. [Item_IR] = ActionMenuItemTypeIR,
  16. [Item_Playlist] = ActionMenuItemTypePlaylist,
  17. [Item_Group] = ActionMenuItemTypeGroup,
  18. [Item_Settings] = ActionMenuItemTypeSettings,
  19. };
  20. void scene_items_item_callback(void* context, int32_t index, InputType type) {
  21. App* app = context;
  22. if(type == InputTypeShort || type == InputTypeRelease) {
  23. app->selected_item = index;
  24. view_dispatcher_send_custom_event(app->view_dispatcher, Event_ButtonPressed);
  25. } else {
  26. // do nothing
  27. }
  28. }
  29. // For each scene, implement handler callbacks
  30. void scene_items_on_enter(void* context) {
  31. App* app = context;
  32. ActionMenu* menu = app->action_menu;
  33. action_menu_reset(menu);
  34. if(app->settings.layout == QUAC_APP_LANDSCAPE)
  35. action_menu_set_layout(menu, ActionMenuLayoutLandscape);
  36. else
  37. action_menu_set_layout(menu, ActionMenuLayoutPortrait);
  38. action_menu_set_show_icons(menu, app->settings.show_icons);
  39. action_menu_set_show_headers(menu, app->settings.show_headers);
  40. ItemsView* items_view = app->items_view;
  41. FURI_LOG_I(
  42. TAG, "Generating scene: [depth=%d] %s", app->depth, furi_string_get_cstr(items_view->path));
  43. action_menu_set_header(menu, furi_string_get_cstr(items_view->name));
  44. size_t item_view_size = ItemArray_size(items_view->items);
  45. if(item_view_size > 0) {
  46. ItemArray_it_t iter;
  47. int32_t index = 0;
  48. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter);
  49. ItemArray_next(iter), ++index) {
  50. const char* label = furi_string_get_cstr(ItemArray_cref(iter)->name);
  51. ActionMenuItemType type = ItemToMenuItem[ItemArray_cref(iter)->type];
  52. action_menu_add_item(menu, label, index, scene_items_item_callback, type, app);
  53. }
  54. } else {
  55. FURI_LOG_W(TAG, "No items for: %s", furi_string_get_cstr(items_view->path));
  56. // TODO: Display Error popup? Empty folder?
  57. }
  58. // Always add the "Settings" item at the end of our list - but only at top level!
  59. if(app->depth == 0) {
  60. action_menu_add_item(
  61. menu,
  62. "Settings",
  63. item_view_size, // last item!
  64. scene_items_item_callback,
  65. ActionMenuItemTypeSettings,
  66. app);
  67. }
  68. view_dispatcher_switch_to_view(app->view_dispatcher, Q_ActionMenu);
  69. }
  70. bool scene_items_on_event(void* context, SceneManagerEvent event) {
  71. App* app = context;
  72. bool consumed = false;
  73. switch(event.type) {
  74. case SceneManagerEventTypeCustom:
  75. if(event.event == Event_ButtonPressed) {
  76. consumed = true;
  77. // furi_delay_ms(100);
  78. // FURI_LOG_I(TAG, "button pressed is %d", app->selected_item);
  79. if(app->selected_item < (int)ItemArray_size(app->items_view->items)) {
  80. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  81. if(item->type == Item_Group) {
  82. app->depth++;
  83. ItemsView* new_items = item_get_items_view_from_path(app, item->path);
  84. item_items_view_free(app->items_view);
  85. app->items_view = new_items;
  86. scene_manager_next_scene(app->scene_manager, Q_Scene_Items);
  87. } else {
  88. FURI_LOG_I(
  89. TAG, "Initiating item action: %s", furi_string_get_cstr(item->name));
  90. // LED goes blinky blinky
  91. App* app = context;
  92. notification_message(app->notifications, &sequence_blink_start_blue);
  93. // Prepare error string for action calls
  94. FuriString* error;
  95. error = furi_string_alloc();
  96. action_tx(app, item, error);
  97. if(furi_string_size(error)) {
  98. FURI_LOG_E(TAG, furi_string_get_cstr(error));
  99. // Change LED to Red and Vibrate!
  100. notification_message(app->notifications, &sequence_error);
  101. // Display DialogEx popup or something?
  102. }
  103. furi_string_free(error);
  104. // Turn off LED light
  105. notification_message(app->notifications, &sequence_blink_stop);
  106. }
  107. } else {
  108. // FURI_LOG_I(TAG, "Selected Settings!");
  109. // TODO: Do we need to free this current items_view??
  110. scene_manager_next_scene(app->scene_manager, Q_Scene_Settings);
  111. }
  112. }
  113. break;
  114. case SceneManagerEventTypeBack:
  115. // FURI_LOG_I(TAG, "Back button pressed!");
  116. consumed = false; // Ensure Back event continues to propagate
  117. if(app->depth > 0) {
  118. // take our current ItemsView path, and go back up a level
  119. FuriString* parent_path;
  120. parent_path = furi_string_alloc();
  121. path_extract_dirname(furi_string_get_cstr(app->items_view->path), parent_path);
  122. app->depth--;
  123. ItemsView* new_items = item_get_items_view_from_path(app, parent_path);
  124. item_items_view_free(app->items_view);
  125. app->items_view = new_items;
  126. furi_string_free(parent_path);
  127. } else {
  128. // FURI_LOG_I(TAG, "At the root level!");
  129. }
  130. break;
  131. default:
  132. FURI_LOG_I(TAG, "Custom event not handled");
  133. break;
  134. }
  135. // FURI_LOG_I(TAG, "Generic event not handled");
  136. return consumed;
  137. }
  138. void scene_items_on_exit(void* context) {
  139. App* app = context;
  140. ActionMenu* menu = app->action_menu;
  141. action_menu_reset(menu);
  142. }