Skip to content

Commit

Permalink
Major Improvements
Browse files Browse the repository at this point in the history
- Adds delete confirmation screen
- Renames renaming screen so it makes more sense
- Changes how text is combined to make it better and more efficient
- Removes unused imports across multiple scenes
  • Loading branch information
acegoal07 committed Apr 4, 2024
1 parent a6f6996 commit 2f513b0
Show file tree
Hide file tree
Showing 17 changed files with 153 additions and 102 deletions.
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ App(
fap_category="NFC",
fap_author="@acegoal07",
fap_weburl="https://github.com/acegoal07/FlipperZero_NFC_Playlist/tree/main",
fap_version="1.4",
fap_version="1.6",
fap_icon="assets/icon.png",
fap_private_libs=[
Lib(
Expand Down
25 changes: 18 additions & 7 deletions nfc_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ static void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = {
nfc_playlist_emulation_scene_on_enter,
nfc_playlist_file_select_scene_on_enter,
nfc_playlist_file_edit_scene_on_enter,
nfc_playlist_text_input_scene_on_enter
nfc_playlist_file_rename_scene_on_enter,
nfc_playlist_confirm_delete_scene_on_enter
};

static bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
Expand All @@ -16,7 +17,8 @@ static bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerE
nfc_playlist_emulation_scene_on_event,
nfc_playlist_file_select_scene_on_event,
nfc_playlist_file_edit_scene_on_event,
nfc_playlist_text_input_scene_on_event
nfc_playlist_file_rename_scene_on_event,
nfc_playlist_confirm_delete_scene_on_event
};

static void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
Expand All @@ -25,7 +27,8 @@ static void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
nfc_playlist_emulation_scene_on_exit,
nfc_playlist_file_select_scene_on_exit,
nfc_playlist_file_edit_scene_on_exit,
nfc_playlist_text_input_scene_on_exit
nfc_playlist_file_rename_scene_on_exit,
nfc_playlist_confirm_delete_scene_on_exit
};

static const SceneManagerHandlers nfc_playlist_scene_manager_handlers = {
Expand Down Expand Up @@ -55,6 +58,7 @@ static NfcPlaylist* nfc_playlist_alloc() {
view_dispatcher_enable_queue(nfc_playlist->view_dispatcher);
nfc_playlist->variable_item_list = variable_item_list_alloc();
nfc_playlist->submenu = submenu_alloc();
nfc_playlist->widget= widget_alloc();

nfc_playlist->settings.base_file_path = furi_string_alloc_set_str("/ext/apps_data/nfc_playlist/");
nfc_playlist->settings.file_path = nfc_playlist->settings.base_file_path;
Expand All @@ -77,7 +81,8 @@ static NfcPlaylist* nfc_playlist_alloc() {
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(nfc_playlist->popup));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileSelect, file_browser_get_view(nfc_playlist->file_browser));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileEdit, submenu_get_view(nfc_playlist->submenu));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput, text_input_get_view(nfc_playlist->text_input));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileRename, text_input_get_view(nfc_playlist->text_input));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_ConfirmDelete, widget_get_view(nfc_playlist->widget));

Storage* storage = furi_record_open(RECORD_STORAGE);
if (!storage_common_exists(storage, "/ext/apps_data/nfc_playlist")) {
Expand All @@ -90,20 +95,26 @@ static NfcPlaylist* nfc_playlist_alloc() {

static void nfc_playlist_free(NfcPlaylist* nfc_playlist) {
furi_assert(nfc_playlist);
scene_manager_free(nfc_playlist->scene_manager);

view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileSelect);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileEdit);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_TextInput);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileRename);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_ConfirmDelete);

scene_manager_free(nfc_playlist->scene_manager);
view_dispatcher_free(nfc_playlist->view_dispatcher);
variable_item_list_free(nfc_playlist->variable_item_list);
submenu_free(nfc_playlist->submenu);
widget_free(nfc_playlist->widget);

furi_record_close(RECORD_NOTIFICATION);
file_browser_free(nfc_playlist->file_browser);
text_input_free(nfc_playlist->text_input);
popup_free(nfc_playlist->popup);
furi_record_close(RECORD_NOTIFICATION);

furi_string_free(nfc_playlist->settings.base_file_path);
furi_string_free(nfc_playlist->settings.file_path);
free(nfc_playlist->playlist_name);
Expand Down
9 changes: 7 additions & 2 deletions nfc_playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <gui/modules/submenu.h>
#include <gui/modules/file_browser.h>
#include <gui/modules/text_input.h>
#include <gui/modules/widget.h>
#include <gui/modules/widget_elements/widget_element.h>
#include <notification/notification_messages.h>
#include <nfc_playlist_worker.h>

Expand All @@ -20,7 +22,8 @@ typedef enum {
NfcPlaylistView_Popup,
NfcPlaylistView_FileSelect,
NfcPlaylistView_FileEdit,
NfcPlaylistView_TextInput
NfcPlaylistView_FileRename,
NfcPlaylistView_ConfirmDelete
} NfcPlayScenesView;

typedef enum {
Expand All @@ -29,7 +32,8 @@ typedef enum {
NfcPlaylistScene_EmulatingPopup,
NfcPlaylistScene_FileSelect,
NfcPlaylistScene_FileEdit,
NfcPlaylistScene_TextInput,
NfcPlaylistScene_FileRename,
NfcPlaylistScene_ConfirmDelete,
NfcPlaylistScene_count
} NfcPlaylistScene;

Expand All @@ -51,6 +55,7 @@ typedef struct {
TextInput* text_input;
Submenu* submenu;
Popup* popup;
Widget* widget;
NotificationApp* notification;
FuriThread* thread;
NfcPlaylistWorker* nfc_playlist_worker;
Expand Down
3 changes: 2 additions & 1 deletion nfc_playlist_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
#include "scences/emulation.h"
#include "scences/file_select.h"
#include "scences/file_edit.h"
#include "scences/text_input.h"
#include "scences/file_rename.h"
#include "scences/confirm_delete.h"
51 changes: 51 additions & 0 deletions scences/confirm_delete.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "nfc_playlist.h"
#include "scences/confirm_delete.h"

void nfc_playlist_confirm_delete_menu_callback(GuiButtonType result, InputType type, void* context) {
NfcPlaylist* nfc_playlist = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_playlist->view_dispatcher, result);
}
}

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

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

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

view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_ConfirmDelete);
}

bool nfc_playlist_confirm_delete_scene_on_event(void* context, SceneManagerEvent event) {
NfcPlaylist* nfc_playlist = context;
bool consumed = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case GuiButtonTypeRight: {
storage_simply_remove(storage, furi_string_get_cstr(nfc_playlist->settings.file_path));
nfc_playlist->settings.file_selected = false;
nfc_playlist->settings.file_selected_check = false;
nfc_playlist->settings.file_path = nfc_playlist->settings.base_file_path;
break;
}
default:
break;
}
scene_manager_previous_scene(nfc_playlist->scene_manager);
}
return consumed;
}

void nfc_playlist_confirm_delete_scene_on_exit(void* context) {
NfcPlaylist* nfc_playlist = context;
widget_reset(nfc_playlist->widget);
}
10 changes: 10 additions & 0 deletions scences/confirm_delete.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <furi.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/widget.h>
#include <storage/storage.h>

void nfc_playlist_confirm_delete_scene_on_enter(void* context);
bool nfc_playlist_confirm_delete_scene_on_event(void* context, SceneManagerEvent event);
void nfc_playlist_confirm_delete_scene_on_exit(void* context);
42 changes: 18 additions & 24 deletions scences/emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
Storage* storage = furi_record_open(RECORD_STORAGE);
Stream* stream = file_stream_alloc(storage);
FuriString* line = furi_string_alloc();
FuriString* temp_header_str = furi_string_alloc();
FuriString* temp_counter_str = furi_string_alloc();

popup_reset(nfc_playlist->popup);
popup_set_context(nfc_playlist->popup, nfc_playlist);
Expand All @@ -75,9 +77,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->settings.emulate_delay]*1000);
do {
char display_text[10];
snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
furi_string_printf(temp_counter_str, "%ds", (time_counter_delay_ms/1000));
popup_set_text(nfc_playlist->popup, furi_string_get_cstr(temp_counter_str), 64, 50, AlignCenter, AlignTop);
furi_delay_ms(50);
time_counter_delay_ms -= 50;
} while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
Expand All @@ -92,43 +93,34 @@ int32_t nfc_playlist_emulation_task(void* context) {
int time_counter_ms = (options_emulate_timeout[nfc_playlist->settings.emulate_timeout]*1000);

if (storage_file_exists(storage, file_path) == false) {
int popup_header_text_size = strlen(file_name) + 18;
char popup_header_text[popup_header_text_size];
snprintf(popup_header_text, popup_header_text_size, "%s\n%s", "ERROR not found:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
furi_string_printf(temp_header_str, "ERROR not found:\n %s", file_name);
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(temp_header_str), 64, 10, AlignCenter, AlignTop);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
do {
char popup_text[10];
snprintf(popup_text, 10, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_string_printf(temp_counter_str, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, furi_string_get_cstr(temp_counter_str), 64, 50, AlignCenter, AlignTop);
furi_delay_ms(50);
time_counter_ms -= 50;
} while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
} else if (strcasestr(file_ext, "nfc") == NULL) {
int popup_header_text_size = strlen(file_name) + 21;
char popup_header_text[popup_header_text_size];
snprintf(popup_header_text, popup_header_text_size, "%s\n%s", "ERROR invalid file:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
furi_string_printf(temp_header_str, "ERROR invalid file:\n %s", file_name);
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(temp_header_str), 64, 10, AlignCenter, AlignTop);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
do {
char popup_text[10];
snprintf(popup_text, 10, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_string_printf(temp_counter_str, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, furi_string_get_cstr(temp_counter_str), 64, 50, AlignCenter, AlignTop);
furi_delay_ms(50);
time_counter_ms -= 50;
} while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
} else {
int popup_header_text_size = strlen(file_name) + 12;
char popup_header_text[popup_header_text_size];
snprintf(popup_header_text, popup_header_text_size, "%s\n%s", "Emulating:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
furi_string_printf(temp_header_str, "Emulating:\n %s", file_name);
popup_set_header(nfc_playlist->popup, furi_string_get_cstr(temp_header_str), 64, 10, AlignCenter, AlignTop);
nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
start_blink(nfc_playlist, NfcPlaylistLedState_Normal);
do {
char popup_text[10];
snprintf(popup_text, 10, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_string_printf(temp_counter_str, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, furi_string_get_cstr(temp_counter_str), 64, 50, AlignCenter, AlignTop);
furi_delay_ms(50);
time_counter_ms -= 50;
} while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
Expand All @@ -148,6 +140,8 @@ int32_t nfc_playlist_emulation_task(void* context) {
}

furi_string_free(line);
furi_string_free(temp_header_str);
furi_string_free(temp_counter_str);
file_stream_close(stream);
furi_record_close(RECORD_STORAGE);
stream_free(stream);
Expand Down
1 change: 0 additions & 1 deletion scences/emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <toolbox/stream/file_stream.h>
#include <lib/worker/nfc_playlist_worker.h>
#include <lib/led/nfc_playlist_led.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/popup.h>
Expand Down
15 changes: 4 additions & 11 deletions scences/file_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,23 @@ typedef enum {

void nfc_playlist_file_edit_menu_callback(void* context, uint32_t index) {
NfcPlaylist* nfc_playlist = context;
Storage* storage = furi_record_open(RECORD_STORAGE);
switch(index) {
case NfcPlaylistMenuSelection_DeletePlaylist: {
storage_simply_remove(storage, furi_string_get_cstr(nfc_playlist->settings.file_path));
nfc_playlist->settings.file_selected = false;
nfc_playlist->settings.file_selected_check = false;
nfc_playlist->settings.file_path = nfc_playlist->settings.base_file_path;
scene_manager_previous_scene(nfc_playlist->scene_manager);
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_ConfirmDelete);
break;
}
case NfcPlaylistMenuSelection_RenamePlaylist: {
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_TextInput);
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_FileRename);
break;
}
default: {
default:
break;
}
}
furi_record_close(RECORD_STORAGE);
}

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

submenu_set_header(nfc_playlist->submenu, "Edit Playlist");

submenu_add_lockable_item(
Expand Down
4 changes: 0 additions & 4 deletions scences/file_edit.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#pragma once
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/submenu.h>
#include <storage/storage.h>
#include <toolbox/stream/stream.h>
#include <toolbox/stream/file_stream.h>

void nfc_playlist_file_edit_scene_on_enter(void* context);
bool nfc_playlist_file_edit_scene_on_event(void* context, SceneManagerEvent event);
Expand Down
Loading

0 comments on commit 2f513b0

Please sign in to comment.