From 1d7c581957bc14c9feee22938997e29b5e236158 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 12 Apr 2026 10:55:00 +0300 Subject: fix(c2): keep history chronological after edit saves --- src/c/fastforge.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/c/fastforge.c') diff --git a/src/c/fastforge.c b/src/c/fastforge.c index 8df5b33..add9578 100644 --- a/src/c/fastforge.c +++ b/src/c/fastforge.c @@ -1,4 +1,5 @@ #include +#include #include #include "fastforge.h" @@ -265,6 +266,31 @@ static int history_index_for_row(int row) { return history_count - 1 - row; } +static int compare_history_entries_by_end_time(const void *a, const void *b) { + const FastEntry *entry_a = a; + const FastEntry *entry_b = b; + if (entry_a->end_time < entry_b->end_time) { + return -1; + } + if (entry_a->end_time > entry_b->end_time) { + return 1; + } + if (entry_a->start_time < entry_b->start_time) { + return -1; + } + if (entry_a->start_time > entry_b->start_time) { + return 1; + } + return 0; +} + +static void sort_history_by_end_time(void) { + if (history_count <= 1) { + return; + } + qsort(history, (size_t)history_count, sizeof(FastEntry), compare_history_entries_by_end_time); +} + static void format_history_row(int row, char *title, size_t title_size, char *subtitle, size_t subtitle_size) { if (!title || !subtitle || title_size == 0 || subtitle_size == 0) { return; @@ -856,6 +882,7 @@ static void history_edit_save_click_handler(ClickRecognizerRef recognizer, void } s_history_edit_draft.max_stage_reached = stage_level_for_elapsed(entry_duration_seconds(&s_history_edit_draft)); history[s_history_edit_index] = s_history_edit_draft; + sort_history_by_end_time(); save_all_data(); s_history_edit_dirty = false; refresh_history_row_cache(); -- cgit v1.2.3