nfc_playlist_scene_name_new_playlist.c 2.6 KB

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