Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
More efficient subghz history delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Dec 14, 2023
1 parent ffc510b commit c40755f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 62 deletions.
10 changes: 2 additions & 8 deletions applications/main/subghz/scenes/subghz_scene_decode_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,18 @@ static void subghz_scene_add_to_history_callback(
if(subghz->remove_duplicates) {
// Look in history for signal hash
uint8_t hash_data = subghz_protocol_decoder_base_get_hash_data(decoder_base);
uint16_t menu_idx = subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
subghz_view_receiver_disable_draw_callback(subghz->subghz_receiver);
for(uint16_t i = idx; i > 0; i--) {
i--; // Iterating in reverse with off by one
if(subghz_history_get_hash_data(subghz->history, i) == hash_data) {
// Remove previous instance and update menu index
subghz_history_delete_item(subghz->history, i);
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, i);
subghz_view_receiver_delete_element_callback(subghz->subghz_receiver);
if(menu_idx > i) {
menu_idx--;
idx--;
}
subghz_view_receiver_delete_item(subghz->subghz_receiver, i);
idx--;
}
i++;
}
// Restore ui state
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, menu_idx);
subghz->idx_menu_chosen = subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
subghz_view_receiver_enable_draw_callback(subghz->subghz_receiver);
if(subghz_history_get_last_index(subghz->history) == 0) {
Expand Down
14 changes: 5 additions & 9 deletions applications/main/subghz/scenes/subghz_scene_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,18 @@ static void subghz_scene_add_to_history_callback(
if(subghz->remove_duplicates) {
// Look in history for signal hash
uint8_t hash_data = subghz_protocol_decoder_base_get_hash_data(decoder_base);
uint16_t menu_idx = subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
subghz_view_receiver_disable_draw_callback(subghz->subghz_receiver);
for(uint16_t i = idx; i > 0; i--) {
i--; // Iterating in reverse with off by one
if(subghz_history_get_hash_data(subghz->history, i) == hash_data) {
// Remove previous instance and update menu index
subghz_history_delete_item(subghz->history, i);
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, i);
subghz_view_receiver_delete_element_callback(subghz->subghz_receiver);
if(menu_idx > i) {
menu_idx--;
idx--;
}
subghz_view_receiver_delete_item(subghz->subghz_receiver, i);
idx--;
}
i++;
}
// Restore ui state
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, menu_idx);
subghz->idx_menu_chosen =
subghz_view_receiver_get_idx_menu(subghz->subghz_receiver);
subghz_view_receiver_enable_draw_callback(subghz->subghz_receiver);
Expand Down Expand Up @@ -334,7 +328,9 @@ bool subghz_scene_receiver_on_event(void* context, SceneManagerEvent event) {
subghz_view_receiver_disable_draw_callback(subghz->subghz_receiver);

subghz_history_delete_item(subghz->history, subghz->idx_menu_chosen);
subghz_view_receiver_delete_element_callback(subghz->subghz_receiver);
subghz_view_receiver_delete_item(
subghz->subghz_receiver,
subghz_view_receiver_get_idx_menu(subghz->subghz_receiver));
subghz_view_receiver_enable_draw_callback(subghz->subghz_receiver);

subghz_scene_receiver_update_statusbar(subghz);
Expand Down
28 changes: 10 additions & 18 deletions applications/main/subghz/subghz_history.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,19 @@ void subghz_history_reset(SubGhzHistory* instance) {
instance->code_last_hash_data = 0;
}

void subghz_history_delete_item(SubGhzHistory* instance, uint16_t item_id) {
void subghz_history_delete_item(SubGhzHistory* instance, uint16_t idx) {
furi_assert(instance);

SubGhzHistoryItemArray_it_t it;
//SubGhzHistoryItem* target_item = SubGhzHistoryItemArray_get(instance->history->data, item_id);
SubGhzHistoryItemArray_it_last(it, instance->history->data);
while(!SubGhzHistoryItemArray_end_p(it)) {
SubGhzHistoryItem* item = SubGhzHistoryItemArray_ref(it);

if(it->index == (size_t)(item_id)) {
furi_string_free(item->item_str);
furi_string_free(item->preset->name);
free(item->preset);
flipper_format_free(item->flipper_string);
item->type = 0;
SubGhzHistoryItemArray_remove(instance->history->data, it);
break;
}
SubGhzHistoryItemArray_previous(it);
if(idx < SubGhzHistoryItemArray_size(instance->history->data)) {
SubGhzHistoryItem* item = SubGhzHistoryItemArray_get(instance->history->data, idx);
furi_string_free(item->item_str);
furi_string_free(item->preset->name);
free(item->preset);
flipper_format_free(item->flipper_string);
item->type = 0;
SubGhzHistoryItemArray_remove_v(instance->history->data, idx, idx + 1);
instance->last_index_write--;
}
instance->last_index_write--;
}

uint16_t subghz_history_get_item(SubGhzHistory* instance) {
Expand Down
2 changes: 1 addition & 1 deletion applications/main/subghz/subghz_history.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void subghz_history_free(SubGhzHistory* instance);
*/
void subghz_history_reset(SubGhzHistory* instance);

void subghz_history_delete_item(SubGhzHistory* instance, uint16_t item_id);
void subghz_history_delete_item(SubGhzHistory* instance, uint16_t idx);

/** Get hash data to history[idx]
*
Expand Down
42 changes: 17 additions & 25 deletions applications/main/subghz/views/receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,41 +707,33 @@ uint16_t subghz_view_receiver_get_idx_menu(SubGhzViewReceiver* subghz_receiver)
return idx;
}

void subghz_view_receiver_delete_element_callback(SubGhzViewReceiver* subghz_receiver) {
void subghz_view_receiver_delete_item(SubGhzViewReceiver* subghz_receiver, uint16_t idx) {
furi_assert(subghz_receiver);

with_view_model(
subghz_receiver->view,
SubGhzViewReceiverModel * model,
{
SubGhzReceiverMenuItemArray_it_t it;
// SubGhzReceiverMenuItem* target_item =
// SubGhzReceiverMenuItemArray_get(model->history->data, model->idx);
SubGhzReceiverMenuItemArray_it_last(it, model->history->data);
while(!SubGhzReceiverMenuItemArray_end_p(it)) {
SubGhzReceiverMenuItem* item = SubGhzReceiverMenuItemArray_ref(it);

if(it->index == (size_t)(model->idx)) {
furi_string_free(item->item_str);
furi_string_free(item->time);
item->type = 0;
item->repeats = 0;
SubGhzReceiverMenuItemArray_remove(model->history->data, it);
if(idx < SubGhzReceiverMenuItemArray_size(model->history->data)) {
SubGhzReceiverMenuItem* item =
SubGhzReceiverMenuItemArray_get(model->history->data, idx);
furi_string_free(item->item_str);
furi_string_free(item->time);
item->type = 0;
item->repeats = 0;
SubGhzReceiverMenuItemArray_remove_v(model->history->data, idx, idx + 1);

if(model->history_item == 5) {
if(model->idx >= 2) {
model->idx = model->history_item - 1;
}
}
model->history_item--;

SubGhzReceiverMenuItemArray_previous(it);
}

if(model->history_item == 5) {
if(model->idx >= 2) {
model->idx = model->history_item - 1;
if(model->idx && (model->idx > idx || model->idx == model->history_item)) {
model->idx--;
}
}
model->history_item--;

if(model->idx != 0) {
model->idx--;
}
},
true);
}
Expand Down
2 changes: 1 addition & 1 deletion applications/main/subghz/views/receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ uint16_t subghz_view_receiver_get_idx_menu(SubGhzViewReceiver* subghz_receiver);

void subghz_view_receiver_set_idx_menu(SubGhzViewReceiver* subghz_receiver, uint16_t idx);

void subghz_view_receiver_delete_element_callback(SubGhzViewReceiver* subghz_receiver);
void subghz_view_receiver_delete_item(SubGhzViewReceiver* subghz_receiver, uint16_t idx);

void subghz_view_receiver_enable_draw_callback(SubGhzViewReceiver* subghz_receiver);

Expand Down

0 comments on commit c40755f

Please sign in to comment.