scene_action_create_group.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include <furi.h>
  2. #include <gui/view_dispatcher.h>
  3. #include <gui/scene_manager.h>
  4. #include <gui/modules/text_input.h>
  5. #include "quac.h"
  6. #include "scenes.h"
  7. #include "scene_action_create_group.h"
  8. #include "../actions/action.h"
  9. #include <lib/toolbox/path.h>
  10. enum {
  11. SceneActionCreateGroupEvent,
  12. };
  13. void scene_action_create_group_callback(void* context) {
  14. App* app = context;
  15. view_dispatcher_send_custom_event(app->view_dispatcher, SceneActionCreateGroupEvent);
  16. }
  17. void scene_action_create_group_on_enter(void* context) {
  18. App* app = context;
  19. TextInput* text = app->text_input;
  20. text_input_set_header_text(text, "Enter new group name:");
  21. app->temp_cstr[0] = 0;
  22. text_input_set_result_callback(
  23. text, scene_action_create_group_callback, app, app->temp_cstr, MAX_NAME_LEN, false);
  24. // TextInputValidatorCallback
  25. // text_input_set_validator(text, validator_callback, context)
  26. view_dispatcher_switch_to_view(app->view_dispatcher, QView_TextInput);
  27. }
  28. bool scene_action_create_group_on_event(void* context, SceneManagerEvent event) {
  29. App* app = context;
  30. bool consumed = false;
  31. if(event.type == SceneManagerEventTypeCustom) {
  32. if(event.event == SceneActionCreateGroupEvent) {
  33. // FURI_LOG_I(TAG, "Attempting to create group %s", app->temp_cstr);
  34. if(!strcmp(app->temp_cstr, "")) {
  35. return false;
  36. }
  37. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  38. FuriString* current_path = furi_string_alloc();
  39. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  40. FuriString* new_group_path = furi_string_alloc();
  41. furi_string_printf(
  42. new_group_path, "%s/%s", furi_string_get_cstr(current_path), app->temp_cstr);
  43. // FURI_LOG_I(TAG, "Full new path: %s", furi_string_get_cstr(new_group_path));
  44. FS_Error fs_result =
  45. storage_common_mkdir(app->storage, furi_string_get_cstr(new_group_path));
  46. if(fs_result == FSE_OK) {
  47. ItemsView* new_items = item_get_items_view_from_path(app, current_path);
  48. item_items_view_free(app->items_view);
  49. app->items_view = new_items;
  50. } else {
  51. FURI_LOG_E(
  52. TAG, "Create Group failed! %s", filesystem_api_error_get_desc(fs_result));
  53. FuriString* error_msg = furi_string_alloc_printf(
  54. "Create Group failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  55. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  56. furi_string_free(error_msg);
  57. }
  58. furi_string_free(current_path);
  59. furi_string_free(new_group_path);
  60. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, QScene_Items);
  61. consumed = true;
  62. }
  63. }
  64. return consumed;
  65. }
  66. void scene_action_create_group_on_exit(void* context) {
  67. App* app = context;
  68. text_input_reset(app->text_input);
  69. }