scene_items.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. };
  21. void scene_items_item_callback(void* context, int32_t index, InputType type) {
  22. App* app = context;
  23. // FURI_LOG_I(TAG, "scene_items callback, type == %s", input_get_type_name(type));
  24. if(type == InputTypeShort) {
  25. app->selected_item = index;
  26. view_dispatcher_send_custom_event(app->view_dispatcher, Event_ButtonPressed);
  27. } else if(type == InputTypeLong) {
  28. app->selected_item = index;
  29. view_dispatcher_send_custom_event(app->view_dispatcher, Event_ButtonPressedLong);
  30. } else {
  31. // do nothing
  32. }
  33. }
  34. // For each scene, implement handler callbacks
  35. void scene_items_on_enter(void* context) {
  36. App* app = context;
  37. ActionMenu* menu = app->action_menu;
  38. action_menu_reset(menu);
  39. if(app->settings.layout == QUAC_APP_LANDSCAPE)
  40. action_menu_set_layout(menu, ActionMenuLayoutLandscape);
  41. else
  42. action_menu_set_layout(menu, ActionMenuLayoutPortrait);
  43. action_menu_set_show_icons(menu, app->settings.show_icons);
  44. action_menu_set_show_headers(menu, app->settings.show_headers);
  45. ItemsView* items_view = app->items_view;
  46. FURI_LOG_I(
  47. TAG, "Generating scene: [depth=%d] %s", app->depth, furi_string_get_cstr(items_view->path));
  48. action_menu_set_header(menu, furi_string_get_cstr(items_view->name));
  49. size_t item_view_size = ItemArray_size(items_view->items);
  50. if(item_view_size > 0) {
  51. ItemArray_it_t iter;
  52. int32_t index = 0;
  53. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter);
  54. ItemArray_next(iter), ++index) {
  55. const char* label = furi_string_get_cstr(ItemArray_cref(iter)->name);
  56. ActionMenuItemType type = ItemToMenuItem[ItemArray_cref(iter)->type];
  57. action_menu_add_item(menu, label, index, scene_items_item_callback, type, app);
  58. }
  59. } else {
  60. FURI_LOG_W(TAG, "No items for: %s", furi_string_get_cstr(items_view->path));
  61. // Add a bogus item - this lets the user still access the Action menu to import, etc
  62. action_menu_add_item(
  63. menu,
  64. "<Empty>",
  65. EMPTY_ACTION_INDEX,
  66. scene_items_item_callback,
  67. ActionMenuItemTypeGroup,
  68. app);
  69. }
  70. // Always add the "Settings" item at the end of our list - but only at top level!
  71. if(app->depth == 0) {
  72. action_menu_add_item(
  73. menu,
  74. "Settings",
  75. item_view_size, // last item!
  76. scene_items_item_callback,
  77. ActionMenuItemTypeSettings,
  78. app);
  79. }
  80. view_dispatcher_switch_to_view(app->view_dispatcher, QView_ActionMenu);
  81. }
  82. bool scene_items_on_event(void* context, SceneManagerEvent event) {
  83. App* app = context;
  84. bool consumed = false;
  85. switch(event.type) {
  86. case SceneManagerEventTypeCustom:
  87. if(event.event == Event_ButtonPressed && app->selected_item != EMPTY_ACTION_INDEX) {
  88. consumed = true;
  89. // FURI_LOG_I(TAG, "button pressed is %d", app->selected_item);
  90. if(app->selected_item < (int)ItemArray_size(app->items_view->items)) {
  91. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  92. if(item->type == Item_Group) {
  93. app->depth++;
  94. ItemsView* new_items = item_get_items_view_from_path(app, item->path);
  95. item_items_view_free(app->items_view);
  96. app->items_view = new_items;
  97. scene_manager_next_scene(app->scene_manager, QScene_Items);
  98. } else {
  99. FURI_LOG_I(
  100. TAG, "Initiating item action: %s", furi_string_get_cstr(item->name));
  101. // LED goes blinky blinky
  102. App* app = context;
  103. notification_message(app->notifications, &sequence_blink_start_blue);
  104. // Prepare error string for action calls
  105. FuriString* error;
  106. error = furi_string_alloc();
  107. action_tx(app, item, error);
  108. if(furi_string_size(error)) {
  109. FURI_LOG_E(TAG, furi_string_get_cstr(error));
  110. // Change LED to Red and Vibrate!
  111. notification_message(app->notifications, &sequence_error);
  112. // Display DialogEx popup or something?
  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. }