text_input.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "nfc_playlist.h"
  2. #include "scences/text_input.h"
  3. void nfc_playlist_text_input_menu_callback(void* context) {
  4. NfcPlaylist* nfc_playlist = context;
  5. Storage* storage = furi_record_open(RECORD_STORAGE);
  6. char const* old_file_path = (char*)furi_string_get_cstr(nfc_playlist->settings.file_path);
  7. char const* old_file_name =
  8. strchr(old_file_path, '/') != NULL ? &strrchr(old_file_path, '/')[1] : old_file_path;
  9. int file_path_size = (strlen(old_file_path) - strlen(old_file_name) + 1);
  10. char* file_path = (char*)malloc(file_path_size);
  11. snprintf(file_path, file_path_size, "%s", old_file_path);
  12. int new_file_path_size =
  13. (strlen(nfc_playlist->playlist_name) + strlen(".txt") + file_path_size + 1);
  14. char* new_file_name = (char*)malloc(new_file_path_size);
  15. snprintf(
  16. new_file_name,
  17. new_file_path_size,
  18. "%s%s%s",
  19. file_path,
  20. nfc_playlist->playlist_name,
  21. ".txt");
  22. if(!storage_file_exists(storage, new_file_name)) {
  23. storage_common_rename(
  24. storage, furi_string_get_cstr(nfc_playlist->settings.file_path), new_file_name);
  25. nfc_playlist->settings.file_path = furi_string_alloc_set_str(new_file_name);
  26. }
  27. free(new_file_name);
  28. free(file_path);
  29. free(nfc_playlist->playlist_name);
  30. furi_record_close(RECORD_STORAGE);
  31. scene_manager_previous_scene(nfc_playlist->scene_manager);
  32. }
  33. void nfc_playlist_text_input_scene_on_enter(void* context) {
  34. NfcPlaylist* nfc_playlist = context;
  35. nfc_playlist->playlist_name = (char*)malloc(50);
  36. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  37. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  38. text_input_set_result_callback(
  39. nfc_playlist->text_input,
  40. nfc_playlist_text_input_menu_callback,
  41. nfc_playlist,
  42. nfc_playlist->playlist_name,
  43. 50,
  44. true);
  45. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  46. }
  47. bool nfc_playlist_text_input_scene_on_event(void* context, SceneManagerEvent event) {
  48. UNUSED(context);
  49. UNUSED(event);
  50. return false;
  51. }
  52. void nfc_playlist_text_input_scene_on_exit(void* context) {
  53. NfcPlaylist* nfc_playlist = context;
  54. text_input_reset(nfc_playlist->text_input);
  55. }