2021-07-02 13:44:10 +00:00
|
|
|
#include "../nfc_i.h"
|
2022-05-27 19:12:40 +00:00
|
|
|
#include "m-string.h"
|
2021-07-25 11:34:54 +00:00
|
|
|
#include <lib/toolbox/random_name.h>
|
2022-01-29 09:39:10 +00:00
|
|
|
#include <gui/modules/validators.h>
|
2022-05-27 19:12:40 +00:00
|
|
|
#include <toolbox/path.h>
|
2021-07-02 13:44:10 +00:00
|
|
|
|
|
|
|
void nfc_scene_save_name_text_input_callback(void* context) {
|
2022-04-19 15:23:58 +00:00
|
|
|
Nfc* nfc = context;
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2022-02-02 19:59:28 +00:00
|
|
|
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventTextInputDone);
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 09:34:16 +00:00
|
|
|
void nfc_scene_save_name_on_enter(void* context) {
|
2022-04-19 15:23:58 +00:00
|
|
|
Nfc* nfc = context;
|
2021-07-02 13:44:10 +00:00
|
|
|
|
|
|
|
// Setup view
|
|
|
|
TextInput* text_input = nfc->text_input;
|
2021-07-25 11:34:54 +00:00
|
|
|
bool dev_name_empty = false;
|
2021-11-08 21:55:06 +00:00
|
|
|
if(!strcmp(nfc->dev->dev_name, "")) {
|
2021-07-25 11:34:54 +00:00
|
|
|
set_random_name(nfc->text_store, sizeof(nfc->text_store));
|
|
|
|
dev_name_empty = true;
|
|
|
|
} else {
|
2021-11-08 21:55:06 +00:00
|
|
|
nfc_text_store_set(nfc, nfc->dev->dev_name);
|
2021-07-22 06:05:07 +00:00
|
|
|
}
|
2021-07-02 13:44:10 +00: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 11:34:54 +00:00
|
|
|
NFC_DEV_NAME_MAX_LEN,
|
|
|
|
dev_name_empty);
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2022-05-27 19:12:40 +00:00
|
|
|
string_t folder_path;
|
|
|
|
string_init(folder_path);
|
|
|
|
|
|
|
|
if(string_end_with_str_p(nfc->dev->load_path, NFC_APP_EXTENSION)) {
|
|
|
|
path_extract_dirname(string_get_cstr(nfc->dev->load_path), folder_path);
|
|
|
|
} else {
|
|
|
|
string_set_str(folder_path, NFC_APP_FOLDER);
|
|
|
|
}
|
|
|
|
|
|
|
|
ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
|
|
|
|
string_get_cstr(folder_path), NFC_APP_EXTENSION, nfc->dev->dev_name);
|
2022-01-29 09:39:10 +00:00
|
|
|
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
|
|
|
|
|
2021-07-22 06:05:07 +00:00
|
|
|
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextInput);
|
2022-05-27 19:12:40 +00:00
|
|
|
|
|
|
|
string_clear(folder_path);
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 09:34:16 +00:00
|
|
|
bool nfc_scene_save_name_on_event(void* context, SceneManagerEvent event) {
|
2022-04-19 15:23:58 +00:00
|
|
|
Nfc* nfc = context;
|
|
|
|
bool consumed = false;
|
2021-07-02 13:44:10 +00:00
|
|
|
|
2021-07-12 18:56:14 +00:00
|
|
|
if(event.type == SceneManagerEventTypeCustom) {
|
2022-02-02 19:59:28 +00:00
|
|
|
if(event.event == NfcCustomEventTextInputDone) {
|
2021-11-08 21:55:06 +00:00
|
|
|
if(strcmp(nfc->dev->dev_name, "")) {
|
2022-05-27 19:12:40 +00:00
|
|
|
nfc_device_delete(nfc->dev, true);
|
2021-07-25 11:34:54 +00:00
|
|
|
}
|
2021-09-02 09:28:40 +00:00
|
|
|
if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetUid)) {
|
2021-11-08 21:55:06 +00:00
|
|
|
nfc->dev->dev_data.nfc_data = nfc->dev_edit_data;
|
2021-09-02 09:28:40 +00:00
|
|
|
}
|
2021-11-08 21:55:06 +00: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 18:56:14 +00:00
|
|
|
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
|
2022-04-19 15:23:58 +00:00
|
|
|
consumed = true;
|
2021-07-12 18:56:14 +00:00
|
|
|
} else {
|
2022-04-19 15:23:58 +00:00
|
|
|
consumed = scene_manager_search_and_switch_to_previous_scene(
|
2021-07-28 14:52:00 +00:00
|
|
|
nfc->scene_manager, NfcSceneStart);
|
2021-07-12 18:56:14 +00:00
|
|
|
}
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-19 15:23:58 +00:00
|
|
|
return consumed;
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 09:34:16 +00:00
|
|
|
void nfc_scene_save_name_on_exit(void* context) {
|
2022-04-19 15:23:58 +00:00
|
|
|
Nfc* nfc = context;
|
2021-07-02 13:44:10 +00:00
|
|
|
|
|
|
|
// Clear view
|
2022-01-29 09:39:10 +00: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 17:32:03 +00:00
|
|
|
text_input_reset(nfc->text_input);
|
2021-07-02 13:44:10 +00:00
|
|
|
}
|