2022-01-05 16:10:18 +00:00
|
|
|
#include "lfrfid_app_scene_save_name.h"
|
2021-07-25 11:34:54 +00:00
|
|
|
#include <lib/toolbox/random_name.h>
|
2021-06-28 14:42:30 +00:00
|
|
|
|
|
|
|
void LfRfidAppSceneSaveName::on_enter(LfRfidApp* app, bool need_restore) {
|
|
|
|
const char* key_name = app->worker.key.get_name();
|
|
|
|
|
2021-07-25 11:34:54 +00:00
|
|
|
bool key_name_empty = !strcmp(key_name, "");
|
|
|
|
if(key_name_empty) {
|
|
|
|
set_random_name(app->text_store.text, app->text_store.text_size);
|
2021-06-28 14:42:30 +00:00
|
|
|
} else {
|
|
|
|
app->text_store.set("%s", key_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto text_input = app->view_controller.get<TextInputVM>();
|
|
|
|
text_input->set_header_text("Name the card");
|
|
|
|
|
|
|
|
text_input->set_result_callback(
|
2021-07-25 11:34:54 +00:00
|
|
|
save_callback,
|
|
|
|
app,
|
|
|
|
app->text_store.text,
|
|
|
|
app->worker.key.get_name_length(),
|
|
|
|
key_name_empty);
|
2021-06-28 14:42:30 +00:00
|
|
|
|
2022-01-29 09:39:10 +00:00
|
|
|
ValidatorIsFile* validator_is_file =
|
|
|
|
validator_is_file_alloc_init(app->app_folder, app->app_extension);
|
|
|
|
text_input->set_validator(validator_is_file_callback, validator_is_file);
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
app->view_controller.switch_to<TextInputVM>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LfRfidAppSceneSaveName::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
if(event->type == LfRfidApp::EventType::Next) {
|
2021-06-30 12:02:46 +00:00
|
|
|
if(strlen(app->worker.key.get_name())) {
|
|
|
|
app->delete_key(&app->worker.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
app->worker.key.set_name(app->text_store.text);
|
|
|
|
|
|
|
|
if(app->save_key(&app->worker.key)) {
|
|
|
|
app->scene_controller.switch_to_next_scene(LfRfidApp::SceneType::SaveSuccess);
|
2021-06-28 14:42:30 +00:00
|
|
|
} else {
|
2021-06-30 12:02:46 +00:00
|
|
|
app->scene_controller.search_and_switch_to_previous_scene(
|
|
|
|
{LfRfidApp::SceneType::ReadedMenu});
|
|
|
|
}
|
2021-06-28 14:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LfRfidAppSceneSaveName::on_exit(LfRfidApp* app) {
|
2022-01-29 09:39:10 +00:00
|
|
|
void* validator_context =
|
|
|
|
app->view_controller.get<TextInputVM>()->get_validator_callback_context();
|
|
|
|
app->view_controller.get<TextInputVM>()->set_validator(NULL, NULL);
|
|
|
|
validator_is_file_free((ValidatorIsFile*)validator_context);
|
|
|
|
|
2021-06-28 14:42:30 +00:00
|
|
|
app->view_controller.get<TextInputVM>()->clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LfRfidAppSceneSaveName::save_callback(void* context) {
|
|
|
|
LfRfidApp* app = static_cast<LfRfidApp*>(context);
|
|
|
|
LfRfidApp::Event event;
|
|
|
|
event.type = LfRfidApp::EventType::Next;
|
|
|
|
app->view_controller.send_event(&event);
|
|
|
|
}
|