text_input.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 = strchr(old_file_path, '/') != NULL ? &strrchr(old_file_path, '/')[1] : old_file_path;
  8. int file_path_size = (strlen(old_file_path) - strlen(old_file_name) + 1);
  9. char* file_path = (char*)malloc(file_path_size);
  10. snprintf(file_path, file_path_size, "%s", old_file_path);
  11. int new_file_path_size = (strlen(nfc_playlist->playlist_name) + strlen(".txt") + file_path_size + 1);
  12. char* new_file_name = (char*)malloc(new_file_path_size);
  13. snprintf(new_file_name, new_file_path_size, "%s%s%s", file_path, nfc_playlist->playlist_name, ".txt");
  14. if (!storage_file_exists(storage, new_file_name)) {
  15. storage_common_rename_safe(storage, furi_string_get_cstr(nfc_playlist->settings.file_path), new_file_name);
  16. nfc_playlist->settings.file_path = furi_string_alloc_set_str(new_file_name);
  17. } else {
  18. scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_FileExistsError);
  19. }
  20. free(new_file_name);
  21. free(file_path);
  22. free(nfc_playlist->playlist_name);
  23. furi_record_close(RECORD_STORAGE);
  24. scene_manager_previous_scene(nfc_playlist->scene_manager);
  25. }
  26. void nfc_playlist_text_input_scene_on_enter(void* context) {
  27. NfcPlaylist* nfc_playlist = context;
  28. nfc_playlist->playlist_name = (char*)malloc(50);
  29. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  30. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  31. text_input_set_result_callback(nfc_playlist->text_input, nfc_playlist_text_input_menu_callback, nfc_playlist, nfc_playlist->playlist_name, 50, true);
  32. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  33. }
  34. bool nfc_playlist_text_input_scene_on_event(void* context, SceneManagerEvent event) {
  35. UNUSED(context);
  36. UNUSED(event);
  37. return false;
  38. }
  39. void nfc_playlist_text_input_scene_on_exit(void* context) {
  40. NfcPlaylist* nfc_playlist = context;
  41. text_input_reset(nfc_playlist->text_input);
  42. }