metroflip_scene_load.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. FURI_LOG_I(TAG, "Card type: %s", app->card_type);
  39. scene_manager_next_scene(app->scene_manager, MetroflipSceneParse);
  40. } else {
  41. scene_manager_next_scene(app->scene_manager, MetroflipSceneStart);
  42. }
  43. furi_string_free(file_path);
  44. furi_record_close(RECORD_STORAGE);
  45. }
  46. bool metroflip_scene_load_on_event(void* context, SceneManagerEvent event) {
  47. Metroflip* app = context;
  48. UNUSED(event);
  49. bool consumed = false;
  50. // If they don't select any file in the brwoser and press back button,
  51. // the data is not loaded
  52. if(!app->data_loaded) {
  53. scene_manager_search_and_switch_to_previous_scene(app->scene_manager, MetroflipSceneStart);
  54. }
  55. consumed = true;
  56. return consumed;
  57. }
  58. void metroflip_scene_load_on_exit(void* context) {
  59. Metroflip* app = context;
  60. UNUSED(app);
  61. }