metroflip_scene_load.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "../metroflip_i.h"
  2. #include <dolphin/dolphin.h>
  3. #include <furi.h>
  4. #include <bit_lib.h>
  5. #include <lib/nfc/protocols/nfc_protocol.h>
  6. #include "../api/metroflip/metroflip_api.h"
  7. #include "../api/suica/suica_loading.h"
  8. #define TAG "Metroflip:Scene:Load"
  9. void metroflip_scene_load_on_enter(void* context) {
  10. Metroflip* app = (Metroflip*)context;
  11. // We initialized this to be false every time we enter
  12. app->data_loaded = false;
  13. // The same string we will use to direct parse scene which plugin to call
  14. // Extracted from the file
  15. FuriString* card_type = furi_string_alloc();
  16. // All the app_data browser stuff. Don't worry about this
  17. DialogsFileBrowserOptions browser_options;
  18. Storage* storage = furi_record_open(RECORD_STORAGE);
  19. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  20. dialog_file_browser_set_basic_options(&browser_options, METROFLIP_FILE_EXTENSION, &I_icon);
  21. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  22. FuriString* file_path = furi_string_alloc_set(browser_options.base_path);
  23. if(dialog_file_browser_show(app->dialogs, file_path, file_path, &browser_options)) {
  24. FlipperFormat* format = flipper_format_file_alloc(storage);
  25. do {
  26. if(!flipper_format_file_open_existing(format, furi_string_get_cstr(file_path))) break;
  27. if(!flipper_format_read_string(format, "Card Type", card_type)) break;
  28. if(furi_string_equal_str(card_type, "suica")) {
  29. load_suica_data(app, format);
  30. }
  31. app->data_loaded = true;
  32. } while(0);
  33. flipper_format_free(format);
  34. }
  35. if(app->data_loaded) {
  36. // Direct to the parsing screen just like the auto scene does
  37. app->card_type = furi_string_get_cstr(card_type);
  38. scene_manager_next_scene(app->scene_manager, MetroflipSceneParse);
  39. } else {
  40. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  41. }
  42. furi_string_free(file_path);
  43. furi_record_close(RECORD_STORAGE);
  44. }
  45. bool metroflip_scene_load_on_event(void* context, SceneManagerEvent event) {
  46. Metroflip* app = context;
  47. UNUSED(event);
  48. bool consumed = false;
  49. // If they don't select any file in the brwoser and press back button,
  50. // the data is not loaded
  51. if(!app->data_loaded) {
  52. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  53. }
  54. consumed = true;
  55. return consumed;
  56. }
  57. void metroflip_scene_load_on_exit(void* context) {
  58. Metroflip* app = context;
  59. UNUSED(app);
  60. }