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