nfc_apdu_runner_scene_save_file.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "../nfc_apdu_runner.h"
  2. #include "nfc_apdu_runner_scene.h"
  3. #include <toolbox/path.h>
  4. // 文本输入回调
  5. static void nfc_apdu_runner_scene_save_file_text_input_callback(void* context) {
  6. NfcApduRunner* app = context;
  7. view_dispatcher_send_custom_event(app->view_dispatcher, NfcApduRunnerCustomEventViewExit);
  8. }
  9. // 保存文件场景进入回调
  10. void nfc_apdu_runner_scene_save_file_on_enter(void* context) {
  11. NfcApduRunner* app = context;
  12. // 分配文本输入视图
  13. TextInput* text_input = app->text_input;
  14. // 提取原始文件名作为默认值
  15. FuriString* filename_str = furi_string_alloc();
  16. FuriString* file_path_str = furi_string_alloc_set(app->file_path);
  17. path_extract_filename(file_path_str, filename_str, true);
  18. // 移除扩展名
  19. size_t ext_pos = furi_string_search_str(filename_str, FILE_EXTENSION);
  20. if(ext_pos != FURI_STRING_FAILURE) {
  21. furi_string_left(filename_str, ext_pos);
  22. }
  23. // 添加响应扩展名
  24. furi_string_cat_str(filename_str, RESPONSE_EXTENSION);
  25. // 设置文本输入
  26. text_input_reset(text_input);
  27. text_input_set_header_text(text_input, "Input save file name");
  28. text_input_set_result_callback(
  29. text_input,
  30. nfc_apdu_runner_scene_save_file_text_input_callback,
  31. app,
  32. app->file_name_buf,
  33. MAX_FILENAME_LEN,
  34. true);
  35. // 设置默认文件名
  36. strncpy(app->file_name_buf, furi_string_get_cstr(filename_str), MAX_FILENAME_LEN);
  37. // 释放临时字符串
  38. furi_string_free(filename_str);
  39. furi_string_free(file_path_str);
  40. // 切换到文本输入视图
  41. view_dispatcher_switch_to_view(app->view_dispatcher, NfcApduRunnerViewTextInput);
  42. }
  43. // 保存文件场景事件回调
  44. bool nfc_apdu_runner_scene_save_file_on_event(void* context, SceneManagerEvent event) {
  45. NfcApduRunner* app = context;
  46. bool consumed = false;
  47. if(event.type == SceneManagerEventTypeCustom) {
  48. if(event.event == NfcApduRunnerCustomEventViewExit) {
  49. // 构建完整的保存路径
  50. FuriString* save_path = furi_string_alloc();
  51. furi_string_printf(save_path, "%s/%s", APP_DIRECTORY_PATH, app->file_name_buf);
  52. // 保存响应结果
  53. bool success = nfc_apdu_save_responses(
  54. app->storage,
  55. furi_string_get_cstr(app->file_path),
  56. app->responses,
  57. app->response_count,
  58. furi_string_get_cstr(save_path));
  59. furi_string_free(save_path);
  60. if(!success) {
  61. // 显示错误消息
  62. dialog_message_show_storage_error(app->dialogs, "Save failed");
  63. } else {
  64. // 显示成功消息
  65. DialogMessage* message = dialog_message_alloc();
  66. dialog_message_set_header(message, "Save success", 64, 0, AlignCenter, AlignTop);
  67. dialog_message_set_text(
  68. message, "Responses saved", 64, 32, AlignCenter, AlignCenter);
  69. dialog_message_set_buttons(message, "OK", NULL, NULL);
  70. dialog_message_show(app->dialogs, message);
  71. dialog_message_free(message);
  72. }
  73. // 返回到开始场景
  74. scene_manager_search_and_switch_to_previous_scene(
  75. app->scene_manager, NfcApduRunnerSceneStart);
  76. consumed = true;
  77. }
  78. } else if(event.type == SceneManagerEventTypeBack) {
  79. // 返回到结果场景
  80. scene_manager_previous_scene(app->scene_manager);
  81. consumed = true;
  82. }
  83. return consumed;
  84. }
  85. // 保存文件场景退出回调
  86. void nfc_apdu_runner_scene_save_file_on_exit(void* context) {
  87. NfcApduRunner* app = context;
  88. text_input_reset(app->text_input);
  89. // 释放响应资源
  90. if(app->responses != NULL) {
  91. nfc_apdu_responses_free(app->responses, app->response_count);
  92. app->responses = NULL;
  93. app->response_count = 0;
  94. }
  95. }