Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
acegoal07 committed May 30, 2024
1 parent 0e7428b commit 5c277a9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 29 deletions.
2 changes: 1 addition & 1 deletion nfc_playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include <toolbox/stream/stream.h>
#include <toolbox/stream/file_stream.h>
#include <toolbox/name_generator.h>
#include <toolbox/path.h>

#include "lib/emulation_worker/nfc_playlist_emulation_worker.h"

Expand Down
7 changes: 4 additions & 3 deletions scenes/nfc_playlist_scene_confirm_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ void nfc_playlist_confirm_delete_menu_callback(GuiButtonType result, InputType t
void nfc_playlist_confirm_delete_scene_on_enter(void* context) {
NfcPlaylist* nfc_playlist = context;

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

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

furi_string_free(temp_str);
furi_string_free(file_name);

view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Widget);
}
Expand Down
17 changes: 11 additions & 6 deletions scenes/nfc_playlist_scene_emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
FuriString* line = furi_string_alloc();
FuriString* tmp_header_str = furi_string_alloc();
FuriString* tmp_counter_str = furi_string_alloc();
FuriString* tmp_file_name = furi_string_alloc();
FuriString* tmp_file_ext = furi_string_alloc();

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

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

if(EmulationState != NfcPlaylistEmulationState_Emulating) {break;}

char* file_name = strchr(file_path, '/') != NULL ? &strrchr(file_path, '/')[1] : file_path;
char const* file_ext = &strrchr(file_path, '.')[1];
path_extract_filename(line, tmp_file_name, false);
path_extract_ext_str(line, tmp_file_ext);

int time_counter_ms = (options_emulate_timeout[nfc_playlist->settings.emulate_timeout]*1000);

if(!strcasestr(file_ext, "nfc")) {
if(!furi_string_cmpi_str(tmp_file_ext, "nfc")) {
if(nfc_playlist->settings.skip_error) {
skip_delay = true;
continue;
}
furi_string_printf(tmp_header_str, "ERROR invalid file:\n%s", file_name);
furi_string_printf(tmp_header_str, "ERROR invalid file:\n%s", furi_string_get_cstr(tmp_file_name));
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(tmp_header_str), 64, 10, AlignCenter, AlignTop);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
Expand All @@ -77,7 +80,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
skip_delay = true;
continue;
}
furi_string_printf(tmp_header_str, "ERROR not found:\n%s", file_name);
furi_string_printf(tmp_header_str, "ERROR not found:\n%s", furi_string_get_cstr(tmp_file_name));
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(tmp_header_str), 64, 10, AlignCenter, AlignTop);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
Expand All @@ -87,7 +90,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
time_counter_ms -= 50;
};
} else {
furi_string_printf(tmp_header_str, "Emulating:\n%s", file_name);
furi_string_printf(tmp_header_str, "Emulating:\n%s", furi_string_get_cstr(tmp_file_name));
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(tmp_header_str), 64, 10, AlignCenter, AlignTop);
nfc_playlist_emulation_worker_set_nfc_data(nfc_playlist->nfc_playlist_emulation_worker, file_path);
nfc_playlist_emulation_worker_start(nfc_playlist->nfc_playlist_emulation_worker);
Expand Down Expand Up @@ -115,6 +118,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
furi_string_free(line);
furi_string_free(tmp_header_str);
furi_string_free(tmp_counter_str);
furi_string_free(tmp_file_name);
furi_string_free(tmp_file_ext);
} else {
popup_set_header(nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
Expand Down
4 changes: 2 additions & 2 deletions scenes/nfc_playlist_scene_name_new_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ void nfc_playlist_name_new_playlist_menu_callback(void* context) {
void nfc_playlist_name_new_playlist_scene_on_enter(void* context) {
NfcPlaylist* nfc_playlist = context;

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

view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
}
Expand Down
67 changes: 50 additions & 17 deletions scenes/nfc_playlist_scene_playlist_rename.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
#include "../nfc_playlist.h"

void nfc_playlist_playlist_rename_menu_callback(void* context) {
int32_t nfc_playlist_playlist_rename_task(void* context) {
NfcPlaylist* nfc_playlist = context;

char const* old_file_path = furi_string_get_cstr(nfc_playlist->settings.playlist_path);
char const* old_file_name = strchr(old_file_path, '/') != NULL ? &strrchr(old_file_path, '/')[1] : old_file_path;
FuriString* new_file_path = furi_string_alloc_set_str(old_file_path);
furi_string_replace(new_file_path, old_file_name, nfc_playlist->text_input_output);
furi_string_cat_str(new_file_path, ".txt");


FuriString* new_file_path = furi_string_alloc();
path_extract_dirname(old_file_path, new_file_path);
furi_string_cat_printf(new_file_path, "/%s.txt", nfc_playlist->text_input_output);
char const* new_file_path_cstr = furi_string_get_cstr(new_file_path);

Storage* storage = furi_record_open(RECORD_STORAGE);

if (!storage_file_exists(storage, furi_string_get_cstr(new_file_path))) {
if (storage_common_rename(storage, old_file_path, furi_string_get_cstr(new_file_path)) == 0) {
if (!storage_file_exists(storage, new_file_path_cstr)) {
if (storage_common_rename(storage, old_file_path, new_file_path_cstr)) {
furi_string_move(nfc_playlist->settings.playlist_path, new_file_path);
}
}

furi_record_close(RECORD_STORAGE);
scene_manager_search_and_switch_to_previous_scene(nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);

return 0;
}

void nfc_playlist_playlist_rename_free(NfcPlaylist* nfc_playlist) {
furi_assert(nfc_playlist);
furi_thread_free(nfc_playlist->thread);
}

void nfc_playlist_playlist_rename_stop(NfcPlaylist* nfc_playlist) {
furi_assert(nfc_playlist);
furi_thread_join(nfc_playlist->thread);
}

void nfc_playlist_playlist_rename_thread_state_callback(FuriThreadState state, void* context) {
if (state == FuriThreadStateStopped) {
NfcPlaylist* nfc_playlist = context;
scene_manager_handle_custom_event(nfc_playlist->scene_manager, 0);
}
}

void nfc_playlist_playlist_rename_menu_callback(void* context) {
NfcPlaylist* nfc_playlist = context;
nfc_playlist->thread = furi_thread_alloc_ex("NfcPlaylistRenamer", 8192, nfc_playlist_playlist_rename_task, nfc_playlist);
// 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
// once fixed this will also be applied to new playlist creation to fix similar view port issues
// furi_thread_set_state_context(nfc_playlist->thread, nfc_playlist);
// furi_thread_set_state_callback(nfc_playlist->thread, nfc_playlist_playlist_rename_thread_state_callback);
furi_thread_start(nfc_playlist->thread);
}

void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
NfcPlaylist* nfc_playlist = context;

char const* tmp_file_path = furi_string_get_cstr(nfc_playlist->settings.playlist_path);
char const* tmp_file_name = strchr(tmp_file_path, '/') != NULL ? &strrchr(tmp_file_path, '/')[1] : tmp_file_path;
FuriString* tmp_file_name_furi = furi_string_alloc_set_str(tmp_file_name);
furi_string_replace(tmp_file_name_furi, ".txt", "");
FuriString* tmp_str = furi_string_alloc();
path_extract_filename(nfc_playlist->settings.playlist_path, tmp_str, true);

nfc_playlist->text_input_output = strdup(furi_string_get_cstr(tmp_file_name_furi));
furi_string_free(tmp_file_name_furi);
nfc_playlist->text_input_output = malloc(MAX_PLAYLIST_NAME_LEN + 1);
strcpy(nfc_playlist->text_input_output, furi_string_get_cstr(tmp_str));
furi_string_free(tmp_str);

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

bool nfc_playlist_playlist_rename_scene_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
NfcPlaylist* nfc_playlist = context;
if(event.type == SceneManagerEventTypeCustom && event.event == 0) {
nfc_playlist_playlist_rename_stop(nfc_playlist);
nfc_playlist_playlist_rename_free(nfc_playlist);
scene_manager_search_and_switch_to_previous_scene(nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
return true;
}
return false;
}

Expand Down

0 comments on commit 5c277a9

Please sign in to comment.