Add helper to find the dive closest to a given time

If we want to keep the selected dive "close" to where it was before an
operation (whether a delete, or a reload, or something like that), then
the most intuitive thing to do appears to be to select either the same
dive again (if it still exists), or one very close to it in time. This
helper allows us to identify the dive in the current dive list that is
closest to the given time.

We do this in the C code to ensure that we look at all dives in the
dive_table - based on the id that is returned the UI can then figure out
where this dive is currently shown.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2016-04-14 05:34:19 -07:00
parent 176b92776b
commit 590af9ae7d
2 changed files with 28 additions and 0 deletions

View file

@ -1189,6 +1189,33 @@ void report_datafile_version(int version)
min_datafile_version = version;
}
int get_dive_id_closest_to(timestamp_t when)
{
int i;
int nr = dive_table.nr;
// deal with pathological cases
if (nr == 0)
return 0;
else if (nr == 1)
return dive_table.dives[0]->id;
for (i = 0; i < nr && dive_table.dives[i]->when <= when; i++)
; // nothing
// again, capture the two edge cases first
if (i == nr)
return dive_table.dives[i - 1]->id;
else if (i == 0)
return dive_table.dives[0]->id;
if (when - dive_table.dives[i - 1]->when < dive_table.dives[i]->when - when)
return dive_table.dives[i - 1]->id;
else
return dive_table.dives[i]->id;
}
void clear_dive_file_data()
{
while (dive_table.nr)

View file

@ -48,6 +48,7 @@ extern void set_dive_nr_for_current_dive();
int get_min_datafile_version();
void reset_min_datafile_version();
void report_datafile_version(int version);
int get_dive_id_closest_to(timestamp_t when);
void clear_dive_file_data();
#ifdef DEBUG_TRIP