scene_items.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // FURI_LOG_I(TAG, "scene_items callback, type == %s", input_get_type_name(type));
  23. if(type == InputTypeShort) {
  24. app->selected_item = index;
  25. view_dispatcher_send_custom_event(app->view_dispatcher, Event_ButtonPressed);
  26. } else if(type == InputTypeLong) {
  27. app->selected_item = index;
  28. view_dispatcher_send_custom_event(app->view_dispatcher, Event_ButtonPressedLong);
  29. } else {
  30. // do nothing
  31. }
  32. }
  33. // For each scene, implement handler callbacks
  34. void scene_items_on_enter(void* context) {
  35. App* app = context;
  36. ActionMenu* menu = app->action_menu;
  37. action_menu_reset(menu);
  38. if(app->settings.layout == QUAC_APP_LANDSCAPE)
  39. action_menu_set_layout(menu, ActionMenuLayoutLandscape);
  40. else
  41. action_menu_set_layout(menu, ActionMenuLayoutPortrait);
  42. action_menu_set_show_icons(menu, app->settings.show_icons);
  43. action_menu_set_show_headers(menu, app->settings.show_headers);
  44. ItemsView* items_view = app->items_view;
  45. FURI_LOG_I(
  46. TAG, "Generating scene: [depth=%d] %s", app->depth, furi_string_get_cstr(items_view->path));
  47. action_menu_set_header(menu, furi_string_get_cstr(items_view->name));
  48. size_t item_view_size = ItemArray_size(items_view->items);
  49. if(item_view_size > 0) {
  50. ItemArray_it_t iter;
  51. int32_t index = 0;
  52. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter);
  53. ItemArray_next(iter), ++index) {
  54. const char* label = furi_string_get_cstr(ItemArray_cref(iter)->name);
  55. ActionMenuItemType type = ItemToMenuItem[ItemArray_cref(iter)->type];
  56. action_menu_add_item(menu, label, index, scene_items_item_callback, type, app);
  57. }
  58. } else {
  59. FURI_LOG_W(TAG, "No items for: %s", furi_string_get_cstr(items_view->path));
  60. // Add a bogus item - this lets the user still access the Action menu to import, etc
  61. action_menu_add_item(
  62. menu,
  63. "<Empty>",
  64. EMPTY_ACTION_INDEX,
  65. scene_items_item_callback,
  66. ActionMenuItemTypeGroup,
  67. app);
  68. }
  69. // Always add the "Settings" item at the end of our list - but only at top level!
  70. if(app->depth == 0) {
  71. action_menu_add_item(
  72. menu,
  73. "Settings",
  74. item_view_size, // last item!
  75. scene_items_item_callback,
  76. ActionMenuItemTypeSettings,
  77. app);
  78. }
  79. view_dispatcher_switch_to_view(app->view_dispatcher, QView_ActionMenu);
  80. }
  81. bool scene_items_on_event(void* context, SceneManagerEvent event) {
  82. App* app = context;
  83. bool consumed = false;
  84. switch(event.type) {
  85. case SceneManagerEventTypeCustom:
  86. if(event.event == Event_ButtonPressed && app->selected_item != EMPTY_ACTION_INDEX) {
  87. consumed = true;
  88. // FURI_LOG_I(TAG, "button pressed is %d", app->selected_item);
  89. if(app->selected_item < (int)ItemArray_size(app->items_view->items)) {
  90. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  91. if(item->type == Item_Group) {
  92. app->depth++;
  93. ItemsView* new_items = item_get_items_view_from_path(app, item->path);
  94. item_items_view_free(app->items_view);
  95. app->items_view = new_items;
  96. scene_manager_next_scene(app->scene_manager, QScene_Items);
  97. } else {
  98. FURI_LOG_I(
  99. TAG, "Initiating item action: %s", furi_string_get_cstr(item->name));
  100. // LED goes blinky blinky
  101. App* app = context;
  102. notification_message(app->notifications, &sequence_blink_start_blue);
  103. // Prepare error string for action calls
  104. FuriString* error;
  105. error = furi_string_alloc();
  106. action_tx(app, item, error);
  107. if(furi_string_size(error)) {
  108. FURI_LOG_E(TAG, furi_string_get_cstr(error));
  109. // Change LED to Red and Vibrate!
  110. notification_message(app->notifications, &sequence_error);
  111. // Display DialogEx popup or something?
  112. }
  113. furi_string_free(error);
  114. // Turn off LED light
  115. notification_message(app->notifications, &sequence_blink_stop);
  116. }
  117. } else {
  118. // FURI_LOG_I(TAG, "Selected Settings!");
  119. // TODO: Do we need to free this current items_view??
  120. scene_manager_next_scene(app->scene_manager, QScene_Settings);
  121. }
  122. } else if(event.event == Event_ButtonPressedLong) {
  123. if(app->selected_item < (int)ItemArray_size(app->items_view->items)) {
  124. consumed = true;
  125. scene_manager_next_scene(app->scene_manager, QScene_ActionSettings);
  126. }
  127. }
  128. break;
  129. case SceneManagerEventTypeBack:
  130. // FURI_LOG_I(TAG, "Back button pressed!");
  131. consumed = false; // Ensure Back event continues to propagate
  132. if(app->depth > 0) {
  133. // take our current ItemsView path, and go back up a level
  134. FuriString* parent_path;
  135. parent_path = furi_string_alloc();
  136. path_extract_dirname(furi_string_get_cstr(app->items_view->path), parent_path);
  137. app->depth--;
  138. ItemsView* new_items = item_get_items_view_from_path(app, parent_path);
  139. item_items_view_free(app->items_view);
  140. app->items_view = new_items;
  141. furi_string_free(parent_path);
  142. } else {
  143. // FURI_LOG_I(TAG, "At the root level!");
  144. }
  145. break;
  146. default:
  147. FURI_LOG_I(TAG, "Custom event not handled");
  148. break;
  149. }
  150. // FURI_LOG_I(TAG, "Generic event not handled");
  151. return consumed;
  152. }
  153. void scene_items_on_exit(void* context) {
  154. App* app = context;
  155. ActionMenu* menu = app->action_menu;
  156. action_menu_reset(menu);
  157. }