subrem_scene_open_sub_file.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "../subghz_remote_app_i.h"
  2. void subrem_scene_open_sub_file_error_popup_callback(void* context) {
  3. SubGhzRemoteApp* app = context;
  4. view_dispatcher_send_custom_event(
  5. app->view_dispatcher, SubRemCustomEventSceneEditOpenSubErrorPopup);
  6. }
  7. SubRemLoadSubState subrem_scene_open_sub_file_dialog(SubGhzRemoteApp* app) {
  8. furi_assert(app);
  9. SubRemSubFilePreset* sub = app->map_preset->subs_preset[app->chosen_sub];
  10. FuriString* temp_file_path = furi_string_alloc();
  11. if(furi_string_empty(sub->file_path)) {
  12. furi_string_set(temp_file_path, SUBGHZ_RAW_FOLDER);
  13. } else {
  14. furi_string_set(temp_file_path, sub->file_path);
  15. }
  16. SubRemLoadSubState ret = SubRemLoadSubStateNotSet;
  17. DialogsFileBrowserOptions browser_options;
  18. dialog_file_browser_set_basic_options(&browser_options, SUBGHZ_APP_FILENAME_EXTENSION, &I_sub1_10px);
  19. browser_options.base_path = SUBGHZ_RAW_FOLDER;
  20. // Input events and views are managed by file_select
  21. if(!dialog_file_browser_show(app->dialogs, temp_file_path, temp_file_path, &browser_options)) {
  22. } else {
  23. // Check sub file
  24. SubRemSubFilePreset* sub_candidate = subrem_sub_file_preset_alloc();
  25. furi_string_set(sub_candidate->label, sub->label);
  26. furi_string_set(sub_candidate->file_path, temp_file_path);
  27. Storage* storage = furi_record_open(RECORD_STORAGE);
  28. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  29. if(flipper_format_file_open_existing(
  30. fff_file, furi_string_get_cstr(sub_candidate->file_path))) {
  31. ret = subrem_sub_preset_load(sub_candidate, app->txrx, fff_file);
  32. }
  33. flipper_format_file_close(fff_file);
  34. flipper_format_free(fff_file);
  35. furi_record_close(RECORD_STORAGE);
  36. if(ret == SubRemLoadSubStateOK) {
  37. subrem_sub_file_preset_free(app->map_preset->subs_preset[app->chosen_sub]);
  38. app->map_preset->subs_preset[app->chosen_sub] = sub_candidate;
  39. app->map_not_saved = true;
  40. } else {
  41. subrem_sub_file_preset_free(sub_candidate);
  42. }
  43. }
  44. furi_string_free(temp_file_path);
  45. return ret;
  46. }
  47. void subrem_scene_open_sub_file_on_enter(void* context) {
  48. furi_assert(context);
  49. SubGhzRemoteApp* app = context;
  50. SubRemLoadSubState load_state = subrem_scene_open_sub_file_dialog(app);
  51. Popup* popup = app->popup;
  52. // popup_set_icon();
  53. popup_set_header(popup, "ERROR", 63, 16, AlignCenter, AlignBottom);
  54. popup_set_timeout(popup, 1500);
  55. popup_set_context(popup, app);
  56. popup_set_callback(popup, subrem_scene_open_sub_file_error_popup_callback);
  57. popup_enable_timeout(popup);
  58. if(load_state == SubRemLoadSubStateOK) {
  59. scene_manager_previous_scene(app->scene_manager);
  60. } else if(load_state == SubRemLoadSubStateNotSet) {
  61. scene_manager_previous_scene(app->scene_manager);
  62. } else {
  63. switch(load_state) {
  64. case SubRemLoadSubStateErrorFreq:
  65. popup_set_text(popup, "Bad frequency", 63, 30, AlignCenter, AlignBottom);
  66. break;
  67. case SubRemLoadSubStateErrorMod:
  68. popup_set_text(popup, "Bad modulation", 63, 30, AlignCenter, AlignBottom);
  69. break;
  70. case SubRemLoadSubStateErrorProtocol:
  71. popup_set_text(popup, "Unsupported protocol", 63, 30, AlignCenter, AlignBottom);
  72. break;
  73. default:
  74. break;
  75. }
  76. view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewIDPopup);
  77. }
  78. }
  79. bool subrem_scene_open_sub_file_on_event(void* context, SceneManagerEvent event) {
  80. SubGhzRemoteApp* app = context;
  81. if(event.type == SceneManagerEventTypeCustom &&
  82. event.event == SubRemCustomEventSceneEditOpenSubErrorPopup) {
  83. scene_manager_previous_scene(app->scene_manager);
  84. return true;
  85. }
  86. return false;
  87. }
  88. void subrem_scene_open_sub_file_on_exit(void* context) {
  89. SubGhzRemoteApp* app = context;
  90. popup_reset(app->popup);
  91. }