diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/dive.h | 1 | ||||
-rw-r--r-- | core/divelist.c | 44 | ||||
-rw-r--r-- | core/divelist.h | 1 |
3 files changed, 35 insertions, 11 deletions
diff --git a/core/dive.h b/core/dive.h index f80412a03..2d3edbc5c 100644 --- a/core/dive.h +++ b/core/dive.h @@ -418,6 +418,7 @@ extern void add_dive_to_trip(struct dive *, dive_trip_t *); struct dive *unregister_dive(int idx); extern void delete_single_dive(int idx); +extern int dive_get_insertion_index(struct dive *dive); extern void add_single_dive(int idx, struct dive *dive); extern void insert_trip(dive_trip_t **trip); diff --git a/core/divelist.c b/core/divelist.c index 07c9ea87d..a25927e90 100644 --- a/core/divelist.c +++ b/core/divelist.c @@ -987,25 +987,29 @@ struct dive **grow_dive_table(struct dive_table *table) return dives; } +/* get the index where we want to insert the dive so that everything stays + * ordered reverse-chronologically */ +int dive_get_insertion_index(struct dive *dive) +{ + for (int i = 0; i < dive_table.nr; i++) { + if (dive->when <= dive_table.dives[i]->when) + return i; + } + return dive_table.nr; +} + +/* add a dive at the given index. if the index is negative, the dive will + * be added according to reverse chronological order */ void add_single_dive(int idx, struct dive *dive) { int i; + if (idx < 0) + idx = dive_get_insertion_index(dive); grow_dive_table(&dive_table); dive_table.nr++; if (dive->selected) amount_selected++; - if (idx < 0) { - // convert an idx of -1 so we do insert-in-chronological-order - idx = dive_table.nr - 1; - for (int i = 0; i < dive_table.nr - 1; i++) { - if (dive->when <= dive_table.dives[i]->when) { - idx = i; - break; - } - } - } - for (i = idx; i < dive_table.nr; i++) { struct dive *tmp = dive_table.dives[i]; dive_table.dives[i] = dive; @@ -1453,6 +1457,24 @@ void process_imported_dives(struct dive_table *import_table, bool prefer_importe mark_divelist_changed(true); } +/* return the number a dive gets when inserted at the given index. + * this function is supposed to be called *before* a dive was added. + * this returns: + * - 1 for an empty log + * - last_nr+1 for addition at end of log (if last dive had a number) + * - 0 for all other cases + */ +int get_dive_nr_at_idx(int idx) +{ + if (dive_table.nr == 0) + return 1; + if (idx >= dive_table.nr) { + struct dive *last_dive = get_dive(dive_table.nr - 1); + return last_dive->number ? last_dive->number + 1 : 0; + } + return 0; +} + void set_dive_nr_for_current_dive() { if (dive_table.nr == 1) diff --git a/core/divelist.h b/core/divelist.h index 3687b0881..2b2ee07a5 100644 --- a/core/divelist.h +++ b/core/divelist.h @@ -42,6 +42,7 @@ extern void find_new_trip_start_time(dive_trip_t *trip); extern struct dive *first_selected_dive(); extern struct dive *last_selected_dive(); extern bool is_trip_before_after(const struct dive *dive, bool before); +extern int get_dive_nr_at_idx(int idx); extern void set_dive_nr_for_current_dive(); extern timestamp_t get_surface_interval(timestamp_t when); extern void delete_dive_from_table(struct dive_table *table, int idx); |