nfc_playlist_scene_name_new_playlist.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "../nfc_playlist.h"
  2. static bool playlist_exist_already = false;
  3. int32_t nfc_playlist_name_new_playlist_thread_task(void* context) {
  4. NfcPlaylist* nfc_playlist = context;
  5. FuriString* file_name =
  6. furi_string_alloc_printf("%s%s.txt", PLAYLIST_LOCATION, nfc_playlist->text_input_output);
  7. char const* file_name_cstr = furi_string_get_cstr(file_name);
  8. Storage* storage = furi_record_open(RECORD_STORAGE);
  9. File* file = storage_file_alloc(storage);
  10. if(!storage_file_exists(storage, file_name_cstr)) {
  11. if(storage_file_open(file, file_name_cstr, FSAM_READ_WRITE, FSOM_CREATE_NEW)) {
  12. storage_file_close(file);
  13. furi_string_swap(nfc_playlist->settings.playlist_path, file_name);
  14. nfc_playlist->settings.playlist_length = 0;
  15. }
  16. } else {
  17. playlist_exist_already = true;
  18. }
  19. furi_string_free(file_name);
  20. storage_file_free(file);
  21. furi_record_close(RECORD_STORAGE);
  22. return 0;
  23. }
  24. void nfc_playlist_name_new_playlist_thread_state_callback(FuriThreadState state, void* context) {
  25. NfcPlaylist* nfc_playlist = context;
  26. if(state == FuriThreadStateStopped) {
  27. furi_thread_yield();
  28. nfc_playlist->thread = NULL;
  29. if(playlist_exist_already) {
  30. scene_manager_next_scene(
  31. nfc_playlist->scene_manager, NfcPlaylistScene_ErrorPlaylistAlreadyExists);
  32. } else {
  33. scene_manager_search_and_switch_to_previous_scene(
  34. nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
  35. }
  36. }
  37. }
  38. void nfc_playlist_name_new_playlist_menu_callback(void* context) {
  39. NfcPlaylist* nfc_playlist = context;
  40. nfc_playlist->thread = furi_thread_alloc_ex(
  41. "NfcPlaylistCreator", 1024, nfc_playlist_name_new_playlist_thread_task, nfc_playlist);
  42. furi_thread_set_state_context(nfc_playlist->thread, nfc_playlist);
  43. furi_thread_set_state_callback(
  44. nfc_playlist->thread, nfc_playlist_name_new_playlist_thread_state_callback);
  45. furi_thread_start(nfc_playlist->thread);
  46. }
  47. void nfc_playlist_name_new_playlist_scene_on_enter(void* context) {
  48. NfcPlaylist* nfc_playlist = context;
  49. playlist_exist_already = false;
  50. nfc_playlist->text_input_output = malloc(MAX_PLAYLIST_NAME_LEN + 1);
  51. text_input_set_header_text(nfc_playlist->text_input, "Enter file name");
  52. text_input_set_minimum_length(nfc_playlist->text_input, 1);
  53. text_input_set_result_callback(
  54. nfc_playlist->text_input,
  55. nfc_playlist_name_new_playlist_menu_callback,
  56. nfc_playlist,
  57. nfc_playlist->text_input_output,
  58. MAX_PLAYLIST_NAME_LEN,
  59. true);
  60. view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
  61. }
  62. bool nfc_playlist_name_new_playlist_scene_on_event(void* context, SceneManagerEvent event) {
  63. UNUSED(context);
  64. UNUSED(event);
  65. return false;
  66. }
  67. void nfc_playlist_name_new_playlist_scene_on_exit(void* context) {
  68. NfcPlaylist* nfc_playlist = context;
  69. free(nfc_playlist->text_input_output);
  70. text_input_reset(nfc_playlist->text_input);
  71. }