scene_action_create_group.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. FuriString* current_path = furi_string_alloc();
  38. if(app->selected_item != EMPTY_ACTION_INDEX) {
  39. Item* item = ItemArray_get(app->items_view->items, app->selected_item);
  40. path_extract_dirname(furi_string_get_cstr(item->path), current_path);
  41. } else {
  42. furi_string_set(current_path, app->items_view->path);
  43. }
  44. FuriString* new_group_path = furi_string_alloc();
  45. furi_string_printf(
  46. new_group_path, "%s/%s", furi_string_get_cstr(current_path), app->temp_cstr);
  47. // FURI_LOG_I(TAG, "Full new path: %s", furi_string_get_cstr(new_group_path));
  48. FS_Error fs_result =
  49. storage_common_mkdir(app->storage, furi_string_get_cstr(new_group_path));
  50. if(fs_result == FSE_OK) {
  51. ItemsView* new_items = item_get_items_view_from_path(app, current_path);
  52. item_items_view_free(app->items_view);
  53. app->items_view = new_items;
  54. } else {
  55. FURI_LOG_E(
  56. TAG, "Create Group failed! %s", filesystem_api_error_get_desc(fs_result));
  57. FuriString* error_msg = furi_string_alloc_printf(
  58. "Create Group failed!\nError: %s", filesystem_api_error_get_desc(fs_result));
  59. dialog_message_show_storage_error(app->dialog, furi_string_get_cstr(error_msg));
  60. furi_string_free(error_msg);
  61. }
  62. furi_string_free(current_path);
  63. furi_string_free(new_group_path);
  64. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, QScene_Items);
  65. consumed = true;
  66. }
  67. }
  68. return consumed;
  69. }
  70. void scene_action_create_group_on_exit(void* context) {
  71. App* app = context;
  72. text_input_reset(app->text_input);
  73. }