Skip to content

Commit

Permalink
Better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gsurkov committed Oct 24, 2023
1 parent f09de18 commit 6476ecb
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
29 changes: 23 additions & 6 deletions applications/main/infrared/infrared.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static const NotificationSequence* infrared_notification_sequences[] = {

static void infrared_make_app_folder(Infrared* infrared) {
if(!storage_simply_mkdir(infrared->storage, INFRARED_APP_FOLDER)) {
dialog_message_show_storage_error(infrared->dialogs, "Cannot create\napp folder");
infrared_show_error_message(infrared, "Cannot create\napp folder");
}
}

Expand Down Expand Up @@ -340,6 +340,11 @@ void infrared_tx_start_button_index(Infrared* infrared, size_t button_index) {

if(infrared_remote_load_signal(infrared->remote, infrared->current_signal, button_index)) {
infrared_tx_start_current(infrared);
} else {
infrared_show_error_message(
infrared,
"Failed to load\n\"%s\"",
infrared_remote_get_signal_name(infrared->remote, button_index));
}
}

Expand Down Expand Up @@ -395,6 +400,17 @@ void infrared_show_loading_popup(Infrared* infrared, bool show) {
}
}

void infrared_show_error_message(Infrared* infrared, const char* fmt, ...) {
va_list args;
va_start(args, fmt);

FuriString* message = furi_string_alloc_vprintf(fmt, args);
dialog_message_show_storage_error(infrared->dialogs, furi_string_get_cstr(message));

furi_string_free(message);
va_end(args);
}

void infrared_signal_received_callback(void* context, InfraredWorkerSignal* received_signal) {
furi_assert(context);
Infrared* infrared = context;
Expand Down Expand Up @@ -449,14 +465,15 @@ int32_t infrared_app(void* p) {
rpc_system_app_send_started(infrared->rpc_ctx);
is_rpc_mode = true;
} else {
furi_string_set(infrared->file_path, (const char*)p);
is_remote_loaded =
infrared_remote_load(infrared->remote, furi_string_get_cstr(infrared->file_path));
const char* file_path = (const char*)p;
is_remote_loaded = infrared_remote_load(infrared->remote, file_path);

if(!is_remote_loaded) {
dialog_message_show_storage_error(
infrared->dialogs, "Failed to load\nselected remote");
infrared_show_error_message(infrared, "Failed to load\n\"%s\"", file_path);
return -1;
}

furi_string_set(infrared->file_path, file_path);
}
}

Expand Down
2 changes: 2 additions & 0 deletions applications/main/infrared/infrared_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ void infrared_text_store_set(Infrared* infrared, uint32_t bank, const char* text
void infrared_text_store_clear(Infrared* infrared, uint32_t bank);
void infrared_play_notification_message(Infrared* infrared, uint32_t message);
void infrared_show_loading_popup(Infrared* infrared, bool show);
void infrared_show_error_message(Infrared* infrared, const char* fmt, ...)
_ATTRIBUTE((__format__(__printf__, 2, 3)));

void infrared_signal_received_callback(void* context, InfraredWorkerSignal* received_signal);
void infrared_text_input_callback(void* context);
Expand Down
17 changes: 14 additions & 3 deletions applications/main/infrared/scenes/infrared_scene_edit_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ void infrared_scene_edit_delete_on_enter(void* context) {

const int32_t current_button_index = infrared->app_state.current_button_index;
furi_check(current_button_index != InfraredButtonIndexNone);
// TODO: Handle situation if signal could not be loaded (load it beforehand?)
infrared_remote_load_signal(remote, infrared->current_signal, current_button_index);

if(!infrared_remote_load_signal(remote, infrared->current_signal, current_button_index)) {
infrared_show_error_message(
infrared,
"Failed to load\n\"%s\"",
infrared_remote_get_signal_name(remote, current_button_index));
scene_manager_previous_scene(infrared->scene_manager);
return;
}

if(infrared_signal_is_raw(infrared->current_signal)) {
const InfraredRawSignal* raw =
Expand Down Expand Up @@ -89,12 +96,16 @@ bool infrared_scene_edit_delete_on_event(void* context, SceneManagerEvent event)
success = infrared_remote_remove(remote);
app_state->current_button_index = InfraredButtonIndexNone;
} else {
furi_assert(0);
furi_crash(NULL);
}

if(success) {
scene_manager_next_scene(scene_manager, InfraredSceneEditDeleteDone);
} else {
infrared_show_error_message(
infrared,
"Failed to\ndelete %s",
edit_target == InfraredEditTargetButton ? "button" : "file");
const uint32_t possible_scenes[] = {InfraredSceneRemoteList, InfraredSceneStart};
scene_manager_search_and_switch_to_previous_scene_one_of(
scene_manager, possible_scenes, COUNT_OF(possible_scenes));
Expand Down
8 changes: 7 additions & 1 deletion applications/main/infrared/scenes/infrared_scene_edit_move.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ bool infrared_scene_edit_move_on_event(void* context, SceneManagerEvent event) {
infrared->remote,
infrared->app_state.prev_button_index,
infrared->app_state.current_button_index)) {
// TODO: Handle error
infrared_show_error_message(
infrared,
"Failed to move\n\"%s\"",
infrared_remote_get_signal_name(
infrared->remote, infrared->app_state.current_button_index));
scene_manager_search_and_switch_to_previous_scene(
infrared->scene_manager, InfraredSceneRemoteList);
}
consumed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,16 @@ bool infrared_scene_edit_rename_on_event(void* context, SceneManagerEvent event)
} else if(edit_target == InfraredEditTargetRemote) {
success = infrared_rename_current_remote(infrared, infrared->text_store[0]);
} else {
furi_assert(0);
furi_crash(NULL);
}

if(success) {
scene_manager_next_scene(scene_manager, InfraredSceneEditRenameDone);
} else {
infrared_show_error_message(
infrared,
"Failed to\nrename %s",
edit_target == InfraredEditTargetButton ? "button" : "file");
scene_manager_search_and_switch_to_previous_scene(
scene_manager, InfraredSceneRemoteList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ bool infrared_scene_learn_enter_name_on_event(void* context, SceneManagerEvent e
scene_manager_next_scene(scene_manager, InfraredSceneLearnDone);
dolphin_deed(DolphinDeedIrSave);
} else {
dialog_message_show_storage_error(infrared->dialogs, "Failed to save file");
infrared_show_error_message(
infrared,
"Failed to\n%s",
infrared->app_state.is_learning_new_remote ? "create file" : "add signal");
const uint32_t possible_scenes[] = {InfraredSceneRemoteList, InfraredSceneStart};
scene_manager_search_and_switch_to_previous_scene_one_of(
scene_manager, possible_scenes, COUNT_OF(possible_scenes));
Expand Down
24 changes: 13 additions & 11 deletions applications/main/infrared/scenes/infrared_scene_remote_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ void infrared_scene_remote_list_on_enter(void* context) {
dialog_file_browser_set_basic_options(&browser_options, INFRARED_APP_EXTENSION, &I_ir_10px);
browser_options.base_path = INFRARED_APP_FOLDER;

bool success = dialog_file_browser_show(
infrared->dialogs, infrared->file_path, infrared->file_path, &browser_options);
if(!dialog_file_browser_show(
infrared->dialogs, infrared->file_path, infrared->file_path, &browser_options)) {
return;
}

if(success) {
view_set_orientation(view_stack_get_view(infrared->view_stack), ViewOrientationVertical);
view_dispatcher_switch_to_view(view_dispatcher, InfraredViewStack);
const char* file_path = furi_string_get_cstr(infrared->file_path);

infrared_show_loading_popup(infrared, true);
success =
infrared_remote_load(infrared->remote, furi_string_get_cstr(infrared->file_path));
infrared_show_loading_popup(infrared, false);
}
view_set_orientation(view_stack_get_view(infrared->view_stack), ViewOrientationVertical);
view_dispatcher_switch_to_view(view_dispatcher, InfraredViewStack);

infrared_show_loading_popup(infrared, true);
const bool remote_loaded = infrared_remote_load(infrared->remote, file_path);
infrared_show_loading_popup(infrared, false);

if(success) {
if(remote_loaded) {
scene_manager_next_scene(scene_manager, InfraredSceneRemote);
} else {
infrared_show_error_message(infrared, "Failed to load\n\"%s\"", file_path);
scene_manager_previous_scene(scene_manager);
}
}
Expand Down

0 comments on commit 6476ecb

Please sign in to comment.