1
1
#include "../nfc_playlist.h"
2
2
3
- void nfc_playlist_playlist_rename_menu_callback (void * context ) {
3
+ int32_t nfc_playlist_playlist_rename_task (void * context ) {
4
4
NfcPlaylist * nfc_playlist = context ;
5
5
6
6
char const * old_file_path = furi_string_get_cstr (nfc_playlist -> settings .playlist_path );
7
- char const * old_file_name = strchr (old_file_path , '/' ) != NULL ? & strrchr (old_file_path , '/' )[1 ] : old_file_path ;
8
- FuriString * new_file_path = furi_string_alloc_set_str (old_file_path );
9
- furi_string_replace (new_file_path , old_file_name , nfc_playlist -> text_input_output );
10
- furi_string_cat_str (new_file_path , ".txt" );
11
-
7
+
8
+ FuriString * new_file_path = furi_string_alloc ();
9
+ path_extract_dirname (old_file_path , new_file_path );
10
+ furi_string_cat_printf (new_file_path , "/%s.txt" , nfc_playlist -> text_input_output );
11
+ char const * new_file_path_cstr = furi_string_get_cstr (new_file_path );
12
+
12
13
Storage * storage = furi_record_open (RECORD_STORAGE );
13
14
14
- if (!storage_file_exists (storage , furi_string_get_cstr ( new_file_path ) )) {
15
- if (storage_common_rename (storage , old_file_path , furi_string_get_cstr ( new_file_path )) == 0 ) {
15
+ if (!storage_file_exists (storage , new_file_path_cstr )) {
16
+ if (storage_common_rename (storage , old_file_path , new_file_path_cstr ) ) {
16
17
furi_string_move (nfc_playlist -> settings .playlist_path , new_file_path );
17
18
}
18
19
}
19
20
20
21
furi_record_close (RECORD_STORAGE );
21
- scene_manager_search_and_switch_to_previous_scene (nfc_playlist -> scene_manager , NfcPlaylistScene_MainMenu );
22
+
23
+ return 0 ;
24
+ }
25
+
26
+ void nfc_playlist_playlist_rename_free (NfcPlaylist * nfc_playlist ) {
27
+ furi_assert (nfc_playlist );
28
+ furi_thread_free (nfc_playlist -> thread );
29
+ }
30
+
31
+ void nfc_playlist_playlist_rename_stop (NfcPlaylist * nfc_playlist ) {
32
+ furi_assert (nfc_playlist );
33
+ furi_thread_join (nfc_playlist -> thread );
34
+ }
35
+
36
+ void nfc_playlist_playlist_rename_thread_state_callback (FuriThreadState state , void * context ) {
37
+ if (state == FuriThreadStateStopped ) {
38
+ NfcPlaylist * nfc_playlist = context ;
39
+ scene_manager_handle_custom_event (nfc_playlist -> scene_manager , 0 );
40
+ }
41
+ }
42
+
43
+ void nfc_playlist_playlist_rename_menu_callback (void * context ) {
44
+ NfcPlaylist * nfc_playlist = context ;
45
+ nfc_playlist -> thread = furi_thread_alloc_ex ("NfcPlaylistRenamer" , 8192 , nfc_playlist_playlist_rename_task , nfc_playlist );
46
+ // DISABLED FOR NOW due to it causing a crash once finished renaming the file not triggering the scene switch nto sure why but looking into it
47
+ // once fixed this will also be applied to new playlist creation to fix similar view port issues
48
+ // furi_thread_set_state_context(nfc_playlist->thread, nfc_playlist);
49
+ // furi_thread_set_state_callback(nfc_playlist->thread, nfc_playlist_playlist_rename_thread_state_callback);
50
+ furi_thread_start (nfc_playlist -> thread );
22
51
}
23
52
24
53
void nfc_playlist_playlist_rename_scene_on_enter (void * context ) {
25
54
NfcPlaylist * nfc_playlist = context ;
26
55
27
- char const * tmp_file_path = furi_string_get_cstr (nfc_playlist -> settings .playlist_path );
28
- char const * tmp_file_name = strchr (tmp_file_path , '/' ) != NULL ? & strrchr (tmp_file_path , '/' )[1 ] : tmp_file_path ;
29
- FuriString * tmp_file_name_furi = furi_string_alloc_set_str (tmp_file_name );
30
- furi_string_replace (tmp_file_name_furi , ".txt" , "" );
56
+ FuriString * tmp_str = furi_string_alloc ();
57
+ path_extract_filename (nfc_playlist -> settings .playlist_path , tmp_str , true);
31
58
32
- nfc_playlist -> text_input_output = strdup (furi_string_get_cstr (tmp_file_name_furi ));
33
- furi_string_free (tmp_file_name_furi );
59
+ nfc_playlist -> text_input_output = malloc (MAX_PLAYLIST_NAME_LEN + 1 );
60
+ strcpy (nfc_playlist -> text_input_output , furi_string_get_cstr (tmp_str ));
61
+ furi_string_free (tmp_str );
34
62
35
63
text_input_set_header_text (nfc_playlist -> text_input , "Enter new file name" );
36
64
text_input_set_minimum_length (nfc_playlist -> text_input , 1 );
@@ -40,8 +68,13 @@ void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
40
68
}
41
69
42
70
bool nfc_playlist_playlist_rename_scene_on_event (void * context , SceneManagerEvent event ) {
43
- UNUSED (context );
44
- UNUSED (event );
71
+ NfcPlaylist * nfc_playlist = context ;
72
+ if (event .type == SceneManagerEventTypeCustom && event .event == 0 ) {
73
+ nfc_playlist_playlist_rename_stop (nfc_playlist );
74
+ nfc_playlist_playlist_rename_free (nfc_playlist );
75
+ scene_manager_search_and_switch_to_previous_scene (nfc_playlist -> scene_manager , NfcPlaylistScene_MainMenu );
76
+ return true;
77
+ }
45
78
return false;
46
79
}
47
80
0 commit comments