2022-02-25 15:22:58 +00:00
|
|
|
#include "../infrared_app.h"
|
2022-05-27 11:19:21 +00:00
|
|
|
#include "m-string.h"
|
|
|
|
#include "toolbox/path.h"
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void InfraredAppSceneEditRename::on_enter(InfraredApp* app) {
|
|
|
|
InfraredAppViewManager* view_manager = app->get_view_manager();
|
2021-06-02 15:16:05 +00:00
|
|
|
TextInput* text_input = view_manager->get_text_input();
|
2021-08-19 00:18:42 +00:00
|
|
|
size_t enter_name_length = 0;
|
2021-06-02 15:16:05 +00:00
|
|
|
|
|
|
|
auto remote_manager = app->get_remote_manager();
|
2022-02-25 15:22:58 +00:00
|
|
|
if(app->get_edit_element() == InfraredApp::EditElement::Button) {
|
|
|
|
furi_assert(app->get_current_button() != InfraredApp::ButtonNA);
|
2021-06-09 13:04:49 +00:00
|
|
|
auto button_name = remote_manager->get_button_name(app->get_current_button());
|
2021-08-19 00:18:42 +00:00
|
|
|
char* buffer_str = app->get_text_store(0);
|
2022-02-25 15:22:58 +00:00
|
|
|
size_t max_len = InfraredAppRemoteManager::max_button_name_length;
|
2021-08-19 00:18:42 +00:00
|
|
|
strncpy(buffer_str, button_name.c_str(), max_len);
|
|
|
|
buffer_str[max_len + 1] = 0;
|
|
|
|
enter_name_length = max_len;
|
2022-04-19 11:07:14 +00:00
|
|
|
text_input_set_header_text(text_input, "Name the button");
|
2021-06-02 15:16:05 +00:00
|
|
|
} else {
|
2021-06-09 13:04:49 +00:00
|
|
|
auto remote_name = remote_manager->get_remote_name();
|
2021-06-02 15:16:05 +00:00
|
|
|
strncpy(app->get_text_store(0), remote_name.c_str(), app->get_text_store_size());
|
2022-02-25 15:22:58 +00:00
|
|
|
enter_name_length = InfraredAppRemoteManager::max_remote_name_length;
|
2021-08-19 00:18:42 +00:00
|
|
|
text_input_set_header_text(text_input, "Name the remote");
|
2022-01-29 09:39:10 +00:00
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
string_t folder_path;
|
|
|
|
string_init(folder_path);
|
|
|
|
|
|
|
|
if(string_end_with_str_p(app->file_path, InfraredApp::infrared_extension)) {
|
|
|
|
path_extract_dirname(string_get_cstr(app->file_path), folder_path);
|
|
|
|
}
|
|
|
|
|
2022-04-07 15:00:45 +00:00
|
|
|
ValidatorIsFile* validator_is_file = validator_is_file_alloc_init(
|
2022-05-27 11:19:21 +00:00
|
|
|
string_get_cstr(folder_path), app->infrared_extension, remote_name.c_str());
|
2022-01-29 09:39:10 +00:00
|
|
|
text_input_set_validator(text_input, validator_is_file_callback, validator_is_file);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
string_clear(folder_path);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text_input_set_result_callback(
|
|
|
|
text_input,
|
2022-02-25 15:22:58 +00:00
|
|
|
InfraredApp::text_input_callback,
|
2021-06-02 15:16:05 +00:00
|
|
|
app,
|
|
|
|
app->get_text_store(0),
|
2021-08-19 00:18:42 +00:00
|
|
|
enter_name_length,
|
2022-02-16 11:09:47 +00:00
|
|
|
false);
|
2021-06-02 15:16:05 +00:00
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
view_manager->switch_to(InfraredAppViewManager::ViewId::TextInput);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
bool InfraredAppSceneEditRename::on_event(InfraredApp* app, InfraredAppEvent* event) {
|
2021-06-02 15:16:05 +00:00
|
|
|
bool consumed = false;
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
if(event->type == InfraredAppEvent::Type::TextEditDone) {
|
2021-06-02 15:16:05 +00:00
|
|
|
auto remote_manager = app->get_remote_manager();
|
2021-06-09 13:04:49 +00:00
|
|
|
bool result = false;
|
2022-02-25 15:22:58 +00:00
|
|
|
if(app->get_edit_element() == InfraredApp::EditElement::Button) {
|
2021-06-09 13:04:49 +00:00
|
|
|
result =
|
|
|
|
remote_manager->rename_button(app->get_current_button(), app->get_text_store(0));
|
2022-02-25 15:22:58 +00:00
|
|
|
app->set_current_button(InfraredApp::ButtonNA);
|
2021-06-02 15:16:05 +00:00
|
|
|
} else {
|
2021-06-09 13:04:49 +00:00
|
|
|
result = remote_manager->rename_remote(app->get_text_store(0));
|
|
|
|
}
|
|
|
|
if(!result) {
|
|
|
|
app->search_and_switch_to_previous_scene(
|
2022-02-25 15:22:58 +00:00
|
|
|
{InfraredApp::Scene::Start, InfraredApp::Scene::RemoteList});
|
2021-06-09 13:04:49 +00:00
|
|
|
} else {
|
2022-02-25 15:22:58 +00:00
|
|
|
app->switch_to_next_scene_without_saving(InfraredApp::Scene::EditRenameDone);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:22:58 +00:00
|
|
|
void InfraredAppSceneEditRename::on_exit(InfraredApp* app) {
|
2022-01-29 09:39:10 +00:00
|
|
|
TextInput* text_input = app->get_view_manager()->get_text_input();
|
|
|
|
|
|
|
|
void* validator_context = text_input_get_validator_callback_context(text_input);
|
|
|
|
text_input_set_validator(text_input, NULL, NULL);
|
|
|
|
|
|
|
|
if(validator_context != NULL) validator_is_file_free((ValidatorIsFile*)validator_context);
|
2021-06-02 15:16:05 +00:00
|
|
|
}
|