text_input.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. }
  18. free(new_file_name);
  19. free(file_path);
  20. free(nfc_playlist->playlist_name);
  21. furi_record_close(RECORD_STORAGE);
  22. scene_manager_previous_scene(nfc_playlist->scene_manager);
  23. }
  24. void nfc_playlist_text_input_scene_on_enter(void* context) {
  25. NfcPlaylist* nfc_playlist = context;
  26. nfc_playlist->playlist_name = (char*)malloc(50);
  27. text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
  28. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  29. text_input_set_result_callback(nfc_playlist->text_input, nfc_playlist_text_input_menu_callback, nfc_playlist, nfc_playlist->playlist_name, 50, true);
  30. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  31. }
  32. bool nfc_playlist_text_input_scene_on_event(void* context, SceneManagerEvent event) {
  33. UNUSED(context);
  34. UNUSED(event);
  35. return false;
  36. }
  37. void nfc_playlist_text_input_scene_on_exit(void* context) {
  38. NfcPlaylist* nfc_playlist = context;
  39. text_input_reset(nfc_playlist->text_input);
  40. }