scene_items.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "flipper.h"
  2. #include "app_state.h"
  3. #include "scenes.h"
  4. #include "scene_items.h"
  5. #include "../actions/action.h"
  6. #include <lib/toolbox/path.h>
  7. void scene_items_item_callback(void* context, int32_t index, InputType type) {
  8. App* app = context;
  9. // FURI_LOG_I(TAG, "item_callback: %ld, %s", index, input_get_type_name(type));
  10. if(type == InputTypeShort || type == InputTypeRelease) {
  11. // FURI_LOG_I(TAG, "You clicked button %li", index);
  12. app->selected_item = index;
  13. view_dispatcher_send_custom_event(app->view_dispatcher, Event_ButtonPressed);
  14. } else {
  15. // FURI_LOG_I(TAG, "[Ignored event of type %i]", type);
  16. }
  17. }
  18. // For each scene, implement handler callbacks
  19. void scene_items_on_enter(void* context) {
  20. App* app = context;
  21. ButtonMenu* menu = app->btn_menu;
  22. button_menu_reset(menu);
  23. ItemsView* items_view = app->items_view;
  24. FURI_LOG_I(TAG, "items on_enter: [%d] %s", app->depth, furi_string_get_cstr(items_view->path));
  25. const char* header = furi_string_get_cstr(items_view->name);
  26. button_menu_set_header(menu, header);
  27. if(ItemArray_size(items_view->items)) {
  28. ItemArray_it_t iter;
  29. int32_t index = 0;
  30. for(ItemArray_it(iter, items_view->items); !ItemArray_end_p(iter);
  31. ItemArray_next(iter), ++index) {
  32. const char* label = furi_string_get_cstr(ItemArray_cref(iter)->name);
  33. ButtonMenuItemType type = ItemArray_cref(iter)->type == Item_Action ?
  34. ButtonMenuItemTypeCommon :
  35. ButtonMenuItemTypeControl;
  36. button_menu_add_item(menu, label, index, scene_items_item_callback, type, app);
  37. }
  38. } else {
  39. FURI_LOG_W(TAG, "No items for: %s", furi_string_get_cstr(items_view->path));
  40. // TODO: Display Error popup?
  41. }
  42. // ...
  43. view_dispatcher_switch_to_view(app->view_dispatcher, SR_ButtonMenu);
  44. }
  45. bool scene_items_on_event(void* context, SceneManagerEvent event) {
  46. App* app = context;
  47. bool consumed = false;
  48. FURI_LOG_I(TAG, "device on_event");
  49. switch(event.type) {
  50. case SceneManagerEventTypeCustom:
  51. if(event.event == Event_ButtonPressed) {
  52. consumed = true;
  53. FURI_LOG_I(TAG, "button pressed is %d", app->selected_item);
  54. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  55. if(item->type == Item_Group) {
  56. ItemsView* new_items = item_get_items_view_from_path(app, item->path);
  57. FURI_LOG_I(TAG, "calling item_items_view_free");
  58. item_items_view_free(app->items_view);
  59. app->items_view = new_items;
  60. app->depth++;
  61. scene_manager_next_scene(app->scene_manager, SR_Scene_Items);
  62. } else {
  63. FURI_LOG_I(TAG, "Initiating item action: %s", furi_string_get_cstr(item->name));
  64. // LED goes blinky blinky
  65. App* app = context;
  66. notification_message(app->notifications, &sequence_blink_start_green);
  67. action_tx(app, item);
  68. // Turn off LED light
  69. notification_message(app->notifications, &sequence_blink_stop);
  70. }
  71. }
  72. break;
  73. case SceneManagerEventTypeBack:
  74. FURI_LOG_I(TAG, "Back button pressed!");
  75. if(app->depth) {
  76. // take our current ItemsView path, and back it up a level
  77. FuriString* new_path;
  78. new_path = furi_string_alloc();
  79. path_extract_dirname(furi_string_get_cstr(app->items_view->path), new_path);
  80. ItemsView* new_items = item_get_items_view_from_path(app, new_path);
  81. item_items_view_free(app->items_view);
  82. app->items_view = new_items;
  83. app->depth--;
  84. furi_string_free(new_path);
  85. } else {
  86. FURI_LOG_W(TAG, "At the root level!");
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. return consumed;
  93. }
  94. void scene_items_on_exit(void* context) {
  95. App* app = context;
  96. ButtonMenu* menu = app->btn_menu;
  97. button_menu_reset(menu);
  98. FURI_LOG_I(TAG, "on_exit. depth = %d", app->depth);
  99. }