2021-07-02 16:44:10 +03:00
|
|
|
#include "../nfc_i.h"
|
2021-07-25 14:34:54 +03:00
|
|
|
#include <lib/toolbox/random_name.h>
|
2022-01-29 12:39:10 +03:00
|
|
|
#include <gui/modules/validators.h>
|
2022-05-27 14:19:21 +03:00
|
|
|
#include <toolbox/path.h>
|
2021-07-02 16:44:10 +03:00
|
|
|
|
|
|
|
void nfc_scene_save_name_text_input_callback(void* context) {
|
2022-04-19 18:23:58 +03:00
|
|
|
Nfc* nfc = context;
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2022-02-02 22:59:28 +03:00
|
|
|
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventTextInputDone);
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
2021-09-21 12:34:16 +03:00
|
|
|
void nfc_scene_save_name_on_enter(void* context) {
|
2022-04-19 18:23:58 +03:00
|
|
|
Nfc* nfc = context;
|
2021-07-02 16:44:10 +03:00
|
|
|
|
|
|
|
// Setup view
|
|
|
|
TextInput* text_input = nfc->text_input;
|
2021-07-25 14:34:54 +03:00
|
|
|
bool dev_name_empty = false;
|
2021-11-09 00:55:06 +03:00
|
|
|
if(!strcmp(nfc->dev->dev_name, "")) {
|
2021-07-25 14:34:54 +03:00
|
|
|
set_random_name(nfc->text_store, sizeof(nfc->text_store));
|
|
|
|
dev_name_empty = true;
|
|
|
|
} else {
|
2021-11-09 00:55:06 +03:00
|
|
|
nfc_text_store_set(nfc, nfc->dev->dev_name);
|
2021-07-22 09:05:07 +03:00
|
|
|
}
|
2021-07-02 16:44:10 +03:00
|
|
|
text_input_set_header_text(text_input, "Name the card");
|
|
|
|
text_input_set_result_callback(
|
|
|
|
text_input,
|
|
|
|
nfc_scene_save_name_text_input_callback,
|
|
|
|
nfc,
|
|
|
|
nfc->text_store,
|
2021-07-25 14:34:54 +03:00
|
|
|
NFC_DEV_NAME_MAX_LEN,
|
|
|
|
dev_name_empty);
|
2022-01-29 12:39:10 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* folder_path;
|
|
|
|
folder_path = furi_string_alloc();
|
2022-05-27 14:19:21 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
if(furi_string_end_with(nfc->dev->load_path, NFC_APP_EXTENSION)) {
|
|
|
|
path_extract_dirname(furi_string_get_cstr(nfc->dev->load_path), folder_path);
|
2022-05-27 14:19:21 +03:00
|
|
|
} else {
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(folder_path, NFC_APP_FOLDER);
|
2022-05-27 14:19:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(folder_path), NFC_APP_EXTENSION, nfc->dev->dev_name);
|
2022-01-29 12:39:10 +03:00
|
|
|
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
|
|
|
|
|
2021-07-22 09:05:07 +03:00
|
|
|
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextInput);
|
2022-05-27 14:19:21 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(folder_path);
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
2021-09-21 12:34:16 +03:00
|
|
|
bool nfc_scene_save_name_on_event(void* context, SceneManagerEvent event) {
|
2022-04-19 18:23:58 +03:00
|
|
|
Nfc* nfc = context;
|
|
|
|
bool consumed = false;
|
2021-07-02 16:44:10 +03:00
|
|
|
|
2021-07-12 21:56:14 +03:00
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
2022-02-02 22:59:28 +03:00
|
|
|
if(event.event == NfcCustomEventTextInputDone) {
|
2021-11-09 00:55:06 +03:00
|
|
|
if(strcmp(nfc->dev->dev_name, "")) {
|
2022-05-27 14:19:21 +03:00
|
|
|
nfc_device_delete(nfc->dev, true);
|
2021-07-25 14:34:54 +03:00
|
|
|
}
|
2021-09-02 12:28:40 +03:00
|
|
|
if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetUid)) {
|
2021-11-09 00:55:06 +03:00
|
|
|
nfc->dev->dev_data.nfc_data = nfc->dev_edit_data;
|
2021-09-02 12:28:40 +03:00
|
|
|
}
|
2021-11-09 00:55:06 +03:00
|
|
|
strlcpy(nfc->dev->dev_name, nfc->text_store, strlen(nfc->text_store) + 1);
|
|
|
|
if(nfc_device_save(nfc->dev, nfc->text_store)) {
|
2021-07-12 21:56:14 +03:00
|
|
|
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
|
2022-04-19 18:23:58 +03:00
|
|
|
consumed = true;
|
2021-07-12 21:56:14 +03:00
|
|
|
} else {
|
2022-04-19 18:23:58 +03:00
|
|
|
consumed = scene_manager_search_and_switch_to_previous_scene(
|
2021-07-28 17:52:00 +03:00
|
|
|
nfc->scene_manager, NfcSceneStart);
|
2021-07-12 21:56:14 +03:00
|
|
|
}
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-19 18:23:58 +03:00
|
|
|
return consumed;
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|
|
|
|
|
2021-09-21 12:34:16 +03:00
|
|
|
void nfc_scene_save_name_on_exit(void* context) {
|
2022-04-19 18:23:58 +03:00
|
|
|
Nfc* nfc = context;
|
2021-07-02 16:44:10 +03:00
|
|
|
|
|
|
|
// Clear view
|
2022-01-29 12:39:10 +03:00
|
|
|
void* validator_context = text_input_get_validator_callback_context(nfc->text_input);
|
|
|
|
text_input_set_validator(nfc->text_input, NULL, NULL);
|
|
|
|
validator_is_file_free(validator_context);
|
|
|
|
|
2022-01-21 20:32:03 +03:00
|
|
|
text_input_reset(nfc->text_input);
|
2021-07-02 16:44:10 +03:00
|
|
|
}
|