scene_items.c 4.0 KB

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