Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,14 @@ static void nfc_protocol_support_scene_saved_menu_on_enter(NfcApp* instance) {
nfc_protocol_support_common_submenu_callback,
instance);

// TODO: Implement restore from shadow file
if(nfc_has_shadow_file(instance)) {
submenu_add_item(
submenu,
"Restore Data Changes",
SubmenuIndexCommonRestore,
nfc_protocol_support_common_submenu_callback,
instance);
}

submenu_set_selected_item(
instance->submenu,
Expand All @@ -360,9 +367,10 @@ static bool
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(instance->scene_manager, NfcSceneSavedMenu, event.event);

// TODO: Implement restore from shadow file

if(event.event == SubmenuIndexCommonInfo) {
if(event.event == SubmenuIndexCommonRestore) {
scene_manager_next_scene(instance->scene_manager, NfcSceneRestoreOriginalConfirm);
consumed = true;
} else if(event.event == SubmenuIndexCommonInfo) {
scene_manager_next_scene(instance->scene_manager, NfcSceneSupportedCard);
consumed = true;
} else if(event.event == SubmenuIndexCommonRename) {
Expand Down Expand Up @@ -489,6 +497,19 @@ static bool

static void nfc_protocol_support_scene_emulate_on_exit(NfcApp* instance) {
nfc_listener_stop(instance->listener);

NfcDevice* nfc_stub = nfc_device_alloc();
NfcProtocol protocol = nfc_device_get_protocol(instance->nfc_device);
const NfcDeviceData* data = nfc_listener_get_data(instance->listener, protocol);
nfc_device_set_data(nfc_stub, protocol, data);

//TODO: think about nfc_device_is_equal(NfcDevice*,NfcDeviceData*);
if(!nfc_device_is_equal(nfc_stub, instance->nfc_device)) {
nfc_device_set_data(instance->nfc_device, protocol, data);
nfc_save_shadow_file(instance);
}
nfc_device_free(nfc_stub);

nfc_listener_free(instance->listener);

// Clear view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ enum {
SubmenuIndexCommonInfo,
SubmenuIndexCommonRename,
SubmenuIndexCommonDelete,
SubmenuIndexCommonRestore,
SubmenuIndexCommonMax,
};

Expand Down
27 changes: 19 additions & 8 deletions applications/main/nfc/nfc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ static bool nfc_set_shadow_file_path(FuriString* file_path, FuriString* shadow_f
furi_assert(shadow_file_path);

bool shadow_file_path_set = false;
if(furi_string_end_with(file_path, NFC_APP_EXTENSION)) {
if(furi_string_end_with(file_path, NFC_APP_SHADOW_EXTENSION)) {
furi_string_set(shadow_file_path, file_path);
shadow_file_path_set = true;
} else if(furi_string_end_with(file_path, NFC_APP_EXTENSION)) {
size_t path_len = furi_string_size(file_path);
// Cut .nfc
furi_string_set_n(shadow_file_path, file_path, 0, path_len - 4);
Expand Down Expand Up @@ -347,6 +350,10 @@ bool nfc_load_file(NfcApp* instance, FuriString* path, bool show_dialog) {
FuriString* load_path = furi_string_alloc();
if(nfc_has_shadow_file_internal(instance, path)) {
nfc_set_shadow_file_path(path, load_path);
} else if(furi_string_end_with(path, NFC_APP_SHADOW_EXTENSION)) {
size_t path_len = furi_string_size(path);
furi_string_set_n(load_path, path, 0, path_len - 4);
furi_string_cat_printf(load_path, "%s", NFC_APP_EXTENSION);
} else {
furi_string_set(load_path, path);
}
Expand All @@ -369,18 +376,22 @@ bool nfc_load_file(NfcApp* instance, FuriString* path, bool show_dialog) {
bool nfc_delete(NfcApp* instance) {
furi_assert(instance);

bool result = false;
FuriString* shadow_file_path = furi_string_alloc();

if(nfc_has_shadow_file(instance)) {
nfc_set_shadow_file_path(instance->file_path, shadow_file_path);
storage_simply_remove(instance->storage, furi_string_get_cstr(shadow_file_path));
nfc_delete_shadow_file(instance);
}

result = storage_simply_remove(instance->storage, furi_string_get_cstr(instance->file_path));
return storage_simply_remove(instance->storage, furi_string_get_cstr(instance->file_path));
}

furi_string_free(shadow_file_path);
bool nfc_delete_shadow_file(NfcApp* instance) {
furi_assert(instance);

FuriString* shadow_file_path = furi_string_alloc();

bool result = nfc_set_shadow_file_path(instance->file_path, shadow_file_path) &&
storage_simply_remove(instance->storage, furi_string_get_cstr(shadow_file_path));

furi_string_free(shadow_file_path);
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions applications/main/nfc/nfc_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ bool nfc_has_shadow_file(NfcApp* instance);

bool nfc_save_shadow_file(NfcApp* instance);

bool nfc_delete_shadow_file(NfcApp* instance);

bool nfc_save(NfcApp* instance);

bool nfc_delete(NfcApp* instance);
Expand Down
2 changes: 2 additions & 0 deletions applications/main/nfc/scenes/nfc_scene_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ ADD_SCENE(nfc, save_name, SaveName)
ADD_SCENE(nfc, save_success, SaveSuccess)
ADD_SCENE(nfc, delete, Delete)
ADD_SCENE(nfc, delete_success, DeleteSuccess)
ADD_SCENE(nfc, restore_original_confirm, RestoreOriginalConfirm)
ADD_SCENE(nfc, restore_original, RestoreOriginal)

ADD_SCENE(nfc, detect, Detect)
ADD_SCENE(nfc, read, Read)
Expand Down
45 changes: 45 additions & 0 deletions applications/main/nfc/scenes/nfc_scene_restore_original.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "../nfc_app_i.h"

void nfc_scene_restore_original_popup_callback(void* context) {
NfcApp* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
}

void nfc_scene_restore_original_on_enter(void* context) {
NfcApp* nfc = context;

// Setup view
Popup* popup = nfc->popup;
popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
popup_set_header(popup, "Original file\nrestored", 13, 22, AlignLeft, AlignBottom);
popup_set_timeout(popup, 1500);
popup_set_context(popup, nfc);
popup_set_callback(popup, nfc_scene_restore_original_popup_callback);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}

bool nfc_scene_restore_original_on_event(void* context, SceneManagerEvent event) {
NfcApp* nfc = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventViewExit) {
if(nfc_load_file(nfc, nfc->file_path, false)) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneSavedMenu);
} else {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneFileSelect);
}
}
}
return consumed;
}

void nfc_scene_restore_original_on_exit(void* context) {
NfcApp* nfc = context;

// Clear view
popup_reset(nfc->popup);
}
54 changes: 54 additions & 0 deletions applications/main/nfc/scenes/nfc_scene_restore_original_confirm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "../nfc_app_i.h"

void nfc_scene_restore_original_confirm_dialog_callback(DialogExResult result, void* context) {
NfcApp* nfc = context;

view_dispatcher_send_custom_event(nfc->view_dispatcher, result);
}

void nfc_scene_restore_original_confirm_on_enter(void* context) {
NfcApp* nfc = context;
DialogEx* dialog_ex = nfc->dialog_ex;

dialog_ex_set_header(dialog_ex, "Restore Card Data?", 64, 0, AlignCenter, AlignTop);
dialog_ex_set_icon(dialog_ex, 5, 11, &I_ArrowC_1_36x36);
dialog_ex_set_text(
dialog_ex, "It will be returned\nto its original state.", 47, 21, AlignLeft, AlignTop);
dialog_ex_set_left_button_text(dialog_ex, "Cancel");
dialog_ex_set_right_button_text(dialog_ex, "Restore");
dialog_ex_set_context(dialog_ex, nfc);
dialog_ex_set_result_callback(dialog_ex, nfc_scene_restore_original_confirm_dialog_callback);

view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewDialogEx);
}

bool nfc_scene_restore_original_confirm_on_event(void* context, SceneManagerEvent event) {
NfcApp* nfc = context;
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
if(event.event == DialogExResultRight) {
if(nfc_delete_shadow_file(nfc)) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneRestoreOriginal);
} else {
scene_manager_search_and_switch_to_previous_scene( //TODO: maybe some better handling, for example showing 'Error' scene
nfc->scene_manager,
NfcSceneStart);
}
consumed = true;
} else if(event.event == DialogExResultLeft) {
consumed = scene_manager_previous_scene(nfc->scene_manager);
}
} else if(event.type == SceneManagerEventTypeBack) {
consumed = true;
}

return consumed;
}

void nfc_scene_restore_original_confirm_on_exit(void* context) {
NfcApp* nfc = context;

// Clean view
dialog_ex_reset(nfc->dialog_ex);
}