Skip to content

Commit 5c277a9

Browse files
committed
Improvements
- Uses built in functions to get file name, director and extensions to make it easier - Adds a thread to rename function to test (The state callback is disabled due to it causing a crash involving the rename function)
1 parent 0e7428b commit 5c277a9

5 files changed

+68
-29
lines changed

nfc_playlist.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include <toolbox/stream/stream.h>
2525
#include <toolbox/stream/file_stream.h>
26-
#include <toolbox/name_generator.h>
26+
#include <toolbox/path.h>
2727

2828
#include "lib/emulation_worker/nfc_playlist_emulation_worker.h"
2929

scenes/nfc_playlist_scene_confirm_delete.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ void nfc_playlist_confirm_delete_menu_callback(GuiButtonType result, InputType t
1010
void nfc_playlist_confirm_delete_scene_on_enter(void* context) {
1111
NfcPlaylist* nfc_playlist = context;
1212

13-
char const* file_path = furi_string_get_cstr(nfc_playlist->settings.playlist_path);
14-
FuriString* temp_str = furi_string_alloc_printf("\e#Delete %s?\e#", strchr(file_path, '/') != NULL ? &strrchr(file_path, '/')[1] : file_path);
15-
furi_string_replace(temp_str, ".txt", "");
13+
FuriString* file_name = furi_string_alloc();
14+
path_extract_filename_no_ext(furi_string_get_cstr(nfc_playlist->settings.playlist_path), file_name);
15+
FuriString* temp_str = furi_string_alloc_printf("\e#Delete %s?\e#", furi_string_get_cstr(file_name));
1616

1717
widget_add_text_box_element(nfc_playlist->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, furi_string_get_cstr(temp_str), false);
1818
widget_add_button_element(nfc_playlist->widget, GuiButtonTypeLeft, "Cancel", nfc_playlist_confirm_delete_menu_callback, nfc_playlist);
1919
widget_add_button_element(nfc_playlist->widget, GuiButtonTypeRight, "Delete", nfc_playlist_confirm_delete_menu_callback, nfc_playlist);
2020

2121
furi_string_free(temp_str);
22+
furi_string_free(file_name);
2223

2324
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Widget);
2425
}

scenes/nfc_playlist_scene_emulation.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
2727
FuriString* line = furi_string_alloc();
2828
FuriString* tmp_header_str = furi_string_alloc();
2929
FuriString* tmp_counter_str = furi_string_alloc();
30+
FuriString* tmp_file_name = furi_string_alloc();
31+
FuriString* tmp_file_ext = furi_string_alloc();
3032

3133
while(stream_read_line(stream, line) && EmulationState == NfcPlaylistEmulationState_Emulating) {
3234

@@ -54,16 +56,17 @@ int32_t nfc_playlist_emulation_task(void* context) {
5456

5557
if(EmulationState != NfcPlaylistEmulationState_Emulating) {break;}
5658

57-
char* file_name = strchr(file_path, '/') != NULL ? &strrchr(file_path, '/')[1] : file_path;
58-
char const* file_ext = &strrchr(file_path, '.')[1];
59+
path_extract_filename(line, tmp_file_name, false);
60+
path_extract_ext_str(line, tmp_file_ext);
61+
5962
int time_counter_ms = (options_emulate_timeout[nfc_playlist->settings.emulate_timeout]*1000);
6063

61-
if(!strcasestr(file_ext, "nfc")) {
64+
if(!furi_string_cmpi_str(tmp_file_ext, "nfc")) {
6265
if(nfc_playlist->settings.skip_error) {
6366
skip_delay = true;
6467
continue;
6568
}
66-
furi_string_printf(tmp_header_str, "ERROR invalid file:\n%s", file_name);
69+
furi_string_printf(tmp_header_str, "ERROR invalid file:\n%s", furi_string_get_cstr(tmp_file_name));
6770
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(tmp_header_str), 64, 10, AlignCenter, AlignTop);
6871
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
6972
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
@@ -77,7 +80,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
7780
skip_delay = true;
7881
continue;
7982
}
80-
furi_string_printf(tmp_header_str, "ERROR not found:\n%s", file_name);
83+
furi_string_printf(tmp_header_str, "ERROR not found:\n%s", furi_string_get_cstr(tmp_file_name));
8184
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(tmp_header_str), 64, 10, AlignCenter, AlignTop);
8285
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
8386
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
@@ -87,7 +90,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
8790
time_counter_ms -= 50;
8891
};
8992
} else {
90-
furi_string_printf(tmp_header_str, "Emulating:\n%s", file_name);
93+
furi_string_printf(tmp_header_str, "Emulating:\n%s", furi_string_get_cstr(tmp_file_name));
9194
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(tmp_header_str), 64, 10, AlignCenter, AlignTop);
9295
nfc_playlist_emulation_worker_set_nfc_data(nfc_playlist->nfc_playlist_emulation_worker, file_path);
9396
nfc_playlist_emulation_worker_start(nfc_playlist->nfc_playlist_emulation_worker);
@@ -115,6 +118,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
115118
furi_string_free(line);
116119
furi_string_free(tmp_header_str);
117120
furi_string_free(tmp_counter_str);
121+
furi_string_free(tmp_file_name);
122+
furi_string_free(tmp_file_ext);
118123
} else {
119124
popup_set_header(nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
120125
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);

scenes/nfc_playlist_scene_name_new_playlist.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ void nfc_playlist_name_new_playlist_menu_callback(void* context) {
2626
void nfc_playlist_name_new_playlist_scene_on_enter(void* context) {
2727
NfcPlaylist* nfc_playlist = context;
2828

29-
name_generator_make_auto(nfc_playlist->text_input_output, MAX_PLAYLIST_NAME_LEN, "playlist-");
29+
nfc_playlist->text_input_output = malloc(MAX_PLAYLIST_NAME_LEN + 1);
3030
text_input_set_header_text(nfc_playlist->text_input, "Enter file name");
3131
text_input_set_minimum_length(nfc_playlist->text_input, 1);
32-
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);
32+
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, false);
3333

3434
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
3535
}

scenes/nfc_playlist_scene_playlist_rename.c

+50-17
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,64 @@
11
#include "../nfc_playlist.h"
22

3-
void nfc_playlist_playlist_rename_menu_callback(void* context) {
3+
int32_t nfc_playlist_playlist_rename_task(void* context) {
44
NfcPlaylist* nfc_playlist = context;
55

66
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+
1213
Storage* storage = furi_record_open(RECORD_STORAGE);
1314

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)) {
1617
furi_string_move(nfc_playlist->settings.playlist_path, new_file_path);
1718
}
1819
}
1920

2021
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);
2251
}
2352

2453
void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
2554
NfcPlaylist* nfc_playlist = context;
2655

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);
3158

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);
3462

3563
text_input_set_header_text(nfc_playlist->text_input, "Enter new file name");
3664
text_input_set_minimum_length(nfc_playlist->text_input, 1);
@@ -40,8 +68,13 @@ void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
4068
}
4169

4270
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+
}
4578
return false;
4679
}
4780

0 commit comments

Comments
 (0)