2024-06-13 17:36:43 +02:00
|
|
|
<script lang="ts">
|
|
|
|
import DatePicker from '$lib/components/ui/date-picker/DatePicker.svelte';
|
|
|
|
import { Input } from '$lib/components/ui/input';
|
|
|
|
import { Label } from '$lib/components/ui/label/index.js';
|
|
|
|
import { Button } from '$lib/components/ui/button';
|
|
|
|
import { Checkbox } from '$lib/components/ui/checkbox';
|
|
|
|
import TimePicker from '$lib/components/ui/time-picker/TimePicker.svelte';
|
2024-06-18 12:35:24 +02:00
|
|
|
import { dbUtils, settings } from '$lib/db';
|
2024-06-13 17:36:43 +02:00
|
|
|
import { gpxStatistics } from '$lib/stores';
|
|
|
|
import {
|
|
|
|
distancePerHourToSecondsPerDistance,
|
|
|
|
getConvertedVelocity,
|
2024-08-09 23:28:59 +02:00
|
|
|
milesToKilometers
|
2024-06-13 17:36:43 +02:00
|
|
|
} from '$lib/units';
|
|
|
|
import { CalendarDate, type DateValue } from '@internationalized/date';
|
2024-06-18 12:35:24 +02:00
|
|
|
import { CalendarClock, CirclePlay, CircleStop, CircleX, Timer, Zap } from 'lucide-svelte';
|
2024-06-13 17:36:43 +02:00
|
|
|
import { tick } from 'svelte';
|
|
|
|
import { _, locale } from 'svelte-i18n';
|
|
|
|
import { get } from 'svelte/store';
|
|
|
|
import { selection } from '$lib/components/file-list/Selection';
|
2024-06-18 12:35:24 +02:00
|
|
|
import {
|
|
|
|
ListFileItem,
|
|
|
|
ListRootItem,
|
|
|
|
ListTrackItem,
|
|
|
|
ListTrackSegmentItem
|
|
|
|
} from '$lib/components/file-list/FileList';
|
2024-06-13 17:36:43 +02:00
|
|
|
import Help from '$lib/components/Help.svelte';
|
|
|
|
|
|
|
|
let startDate: DateValue | undefined = undefined;
|
|
|
|
let startTime: string | undefined = undefined;
|
|
|
|
let endDate: DateValue | undefined = undefined;
|
|
|
|
let endTime: string | undefined = undefined;
|
2024-06-18 12:35:24 +02:00
|
|
|
let movingTime: number | undefined = undefined;
|
2024-06-13 17:36:43 +02:00
|
|
|
let speed: number | undefined = undefined;
|
|
|
|
|
|
|
|
function toCalendarDate(date: Date): CalendarDate {
|
2024-07-04 21:30:23 +02:00
|
|
|
return new CalendarDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
|
2024-06-13 17:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const { velocityUnits, distanceUnits } = settings;
|
|
|
|
|
|
|
|
function setSpeed(value: number) {
|
|
|
|
let speedValue = getConvertedVelocity(value);
|
|
|
|
if ($velocityUnits === 'speed') {
|
|
|
|
speedValue = parseFloat(speedValue.toFixed(2));
|
|
|
|
}
|
|
|
|
speed = speedValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGPXData() {
|
|
|
|
if ($gpxStatistics.global.time.start) {
|
|
|
|
startDate = toCalendarDate($gpxStatistics.global.time.start);
|
|
|
|
startTime = $gpxStatistics.global.time.start.toLocaleTimeString();
|
|
|
|
} else {
|
|
|
|
startDate = undefined;
|
|
|
|
startTime = undefined;
|
|
|
|
}
|
|
|
|
if ($gpxStatistics.global.time.end) {
|
|
|
|
endDate = toCalendarDate($gpxStatistics.global.time.end);
|
|
|
|
endTime = $gpxStatistics.global.time.end.toLocaleTimeString();
|
|
|
|
} else {
|
|
|
|
endDate = undefined;
|
|
|
|
endTime = undefined;
|
|
|
|
}
|
2024-06-18 12:35:24 +02:00
|
|
|
if ($gpxStatistics.global.time.moving) {
|
|
|
|
movingTime = $gpxStatistics.global.time.moving;
|
2024-06-13 17:36:43 +02:00
|
|
|
} else {
|
2024-06-18 12:35:24 +02:00
|
|
|
movingTime = undefined;
|
2024-06-13 17:36:43 +02:00
|
|
|
}
|
2024-06-18 12:35:24 +02:00
|
|
|
if ($gpxStatistics.global.speed.moving) {
|
|
|
|
setSpeed($gpxStatistics.global.speed.moving);
|
2024-06-13 17:36:43 +02:00
|
|
|
} else {
|
|
|
|
speed = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$: if ($gpxStatistics && $velocityUnits && $distanceUnits) {
|
|
|
|
setGPXData();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDate(date: DateValue, time: string): Date {
|
|
|
|
if (date === undefined) {
|
|
|
|
return new Date();
|
|
|
|
}
|
|
|
|
let [hours, minutes, seconds] = time.split(':').map((x) => parseInt(x));
|
2024-07-04 21:30:23 +02:00
|
|
|
return new Date(date.year, date.month - 1, date.day, hours, minutes, seconds);
|
2024-06-13 17:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateEnd() {
|
2024-06-18 12:35:24 +02:00
|
|
|
if (startDate && movingTime !== undefined) {
|
2024-06-13 17:36:43 +02:00
|
|
|
if (startTime === undefined) {
|
|
|
|
startTime = '00:00:00';
|
|
|
|
}
|
|
|
|
let start = getDate(startDate, startTime);
|
2024-06-18 12:35:24 +02:00
|
|
|
let ratio =
|
|
|
|
$gpxStatistics.global.time.moving > 0
|
|
|
|
? $gpxStatistics.global.time.total / $gpxStatistics.global.time.moving
|
|
|
|
: 1;
|
|
|
|
let end = new Date(start.getTime() + ratio * movingTime * 1000);
|
2024-06-13 17:36:43 +02:00
|
|
|
endDate = toCalendarDate(end);
|
|
|
|
endTime = end.toLocaleTimeString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateStart() {
|
2024-06-18 12:35:24 +02:00
|
|
|
if (endDate && movingTime !== undefined) {
|
2024-06-13 17:36:43 +02:00
|
|
|
if (endTime === undefined) {
|
|
|
|
endTime = '00:00:00';
|
|
|
|
}
|
|
|
|
let end = getDate(endDate, endTime);
|
2024-06-18 12:35:24 +02:00
|
|
|
let ratio =
|
|
|
|
$gpxStatistics.global.time.moving > 0
|
|
|
|
? $gpxStatistics.global.time.total / $gpxStatistics.global.time.moving
|
|
|
|
: 1;
|
|
|
|
let start = new Date(end.getTime() - ratio * movingTime * 1000);
|
2024-06-13 17:36:43 +02:00
|
|
|
startDate = toCalendarDate(start);
|
|
|
|
startTime = start.toLocaleTimeString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 12:35:24 +02:00
|
|
|
function getSpeed() {
|
2024-06-13 17:36:43 +02:00
|
|
|
if (speed === undefined) {
|
2024-06-18 12:35:24 +02:00
|
|
|
return undefined;
|
2024-06-13 17:36:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let speedValue = speed;
|
|
|
|
if ($velocityUnits === 'pace') {
|
|
|
|
speedValue = distancePerHourToSecondsPerDistance(speed);
|
|
|
|
}
|
|
|
|
if ($distanceUnits === 'imperial') {
|
2024-08-09 23:28:59 +02:00
|
|
|
speedValue = milesToKilometers(speedValue);
|
2024-06-13 17:36:43 +02:00
|
|
|
}
|
2024-06-18 12:35:24 +02:00
|
|
|
return speedValue;
|
|
|
|
}
|
2024-06-13 17:36:43 +02:00
|
|
|
|
2024-06-18 12:35:24 +02:00
|
|
|
function updateDataFromSpeed() {
|
|
|
|
let speedValue = getSpeed();
|
|
|
|
if (speedValue === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let distance =
|
|
|
|
$gpxStatistics.global.distance.moving > 0
|
|
|
|
? $gpxStatistics.global.distance.moving
|
|
|
|
: $gpxStatistics.global.distance.total;
|
|
|
|
movingTime = (distance / speedValue) * 3600;
|
2024-06-13 17:36:43 +02:00
|
|
|
|
|
|
|
updateEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDataFromTotalTime() {
|
2024-06-18 12:35:24 +02:00
|
|
|
if (movingTime === undefined) {
|
2024-06-13 17:36:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-06-18 12:35:24 +02:00
|
|
|
setSpeed($gpxStatistics.global.distance.moving / (movingTime / 3600));
|
2024-06-13 17:36:43 +02:00
|
|
|
updateEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
$: canUpdate =
|
|
|
|
$selection.size === 1 && $selection.hasAnyChildren(new ListRootItem(), true, ['waypoints']);
|
|
|
|
</script>
|
|
|
|
|
2024-07-10 15:09:52 +02:00
|
|
|
<div class="flex flex-col gap-3 w-full max-w-80 {$$props.class ?? ''}">
|
2024-06-13 17:36:43 +02:00
|
|
|
<fieldset class="flex flex-col gap-2">
|
|
|
|
<div class="flex flex-row gap-2 justify-center">
|
2024-07-10 15:09:52 +02:00
|
|
|
<div class="flex flex-col gap-2 grow">
|
2024-06-13 17:36:43 +02:00
|
|
|
<Label for="speed" class="flex flex-row">
|
|
|
|
<Zap size="16" class="mr-1" />
|
|
|
|
{#if $velocityUnits === 'speed'}
|
|
|
|
{$_('quantities.speed')}
|
|
|
|
{:else}
|
|
|
|
{$_('quantities.pace')}
|
|
|
|
{/if}
|
|
|
|
</Label>
|
|
|
|
<div class="flex flex-row gap-1 items-center">
|
|
|
|
{#if $velocityUnits === 'speed'}
|
|
|
|
<Input
|
|
|
|
id="speed"
|
|
|
|
type="number"
|
|
|
|
step={0.01}
|
2024-06-18 12:35:24 +02:00
|
|
|
min={0.01}
|
2024-06-13 17:36:43 +02:00
|
|
|
disabled={!canUpdate}
|
|
|
|
bind:value={speed}
|
|
|
|
on:change={updateDataFromSpeed}
|
|
|
|
/>
|
|
|
|
<span class="text-sm shrink-0">
|
|
|
|
{#if $distanceUnits === 'imperial'}
|
|
|
|
{$_('units.miles_per_hour')}
|
|
|
|
{:else}
|
|
|
|
{$_('units.kilometers_per_hour')}
|
|
|
|
{/if}
|
|
|
|
</span>
|
|
|
|
{:else}
|
|
|
|
<TimePicker
|
|
|
|
bind:value={speed}
|
|
|
|
showHours={false}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
on:change={updateDataFromSpeed}
|
|
|
|
/>
|
|
|
|
<span class="text-sm shrink-0">
|
|
|
|
{#if $distanceUnits === 'imperial'}
|
|
|
|
{$_('units.minutes_per_mile')}
|
|
|
|
{:else}
|
|
|
|
{$_('units.minutes_per_kilometer')}
|
|
|
|
{/if}
|
|
|
|
</span>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-07-10 15:09:52 +02:00
|
|
|
<div class="flex flex-col gap-2 grow">
|
2024-06-13 17:36:43 +02:00
|
|
|
<Label for="duration" class="flex flex-row">
|
|
|
|
<Timer size="16" class="mr-1" />
|
|
|
|
{$_('toolbar.time.total_time')}
|
|
|
|
</Label>
|
|
|
|
<TimePicker
|
2024-06-18 12:35:24 +02:00
|
|
|
bind:value={movingTime}
|
2024-06-13 17:36:43 +02:00
|
|
|
disabled={!canUpdate}
|
|
|
|
on:change={updateDataFromTotalTime}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Label class="flex flex-row">
|
|
|
|
<CirclePlay size="16" class="mr-1" />
|
|
|
|
{$_('toolbar.time.start')}
|
|
|
|
</Label>
|
|
|
|
<div class="flex flex-row gap-2">
|
|
|
|
<DatePicker
|
|
|
|
bind:value={startDate}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
locale={get(locale) ?? 'en'}
|
|
|
|
placeholder={$_('toolbar.time.pick_date')}
|
2024-07-10 15:09:52 +02:00
|
|
|
class="w-fit grow"
|
2024-06-13 17:36:43 +02:00
|
|
|
onValueChange={async () => {
|
|
|
|
await tick();
|
|
|
|
updateEnd();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
type="time"
|
|
|
|
step={1}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
bind:value={startTime}
|
2024-07-10 15:09:52 +02:00
|
|
|
class="w-fit"
|
2024-06-13 17:36:43 +02:00
|
|
|
on:input={updateEnd}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Label class="flex flex-row">
|
|
|
|
<CircleStop size="16" class="mr-1" />
|
|
|
|
{$_('toolbar.time.end')}
|
|
|
|
</Label>
|
|
|
|
<div class="flex flex-row gap-2">
|
|
|
|
<DatePicker
|
|
|
|
bind:value={endDate}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
locale={get(locale) ?? 'en'}
|
|
|
|
placeholder={$_('toolbar.time.pick_date')}
|
2024-07-10 15:09:52 +02:00
|
|
|
class="w-fit grow"
|
2024-06-13 17:36:43 +02:00
|
|
|
onValueChange={async () => {
|
|
|
|
await tick();
|
|
|
|
updateStart();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
type="time"
|
|
|
|
step={1}
|
|
|
|
disabled={!canUpdate}
|
|
|
|
bind:value={endTime}
|
2024-07-10 15:09:52 +02:00
|
|
|
class="w-fit"
|
2024-06-13 17:36:43 +02:00
|
|
|
on:change={updateStart}
|
|
|
|
/>
|
|
|
|
</div>
|
2024-06-18 12:35:24 +02:00
|
|
|
{#if $gpxStatistics.global.time.moving === 0 || $gpxStatistics.global.time.moving === undefined}
|
2024-07-12 12:19:03 +02:00
|
|
|
<div class="mt-0.5 flex flex-row gap-1 items-center hidden">
|
|
|
|
<Checkbox id="artificial-time" disabled={!canUpdate} />
|
|
|
|
<Label for="artificial-time">
|
|
|
|
{$_('toolbar.time.artificial')}
|
|
|
|
</Label>
|
|
|
|
</div>
|
2024-06-13 17:36:43 +02:00
|
|
|
{/if}
|
|
|
|
</fieldset>
|
|
|
|
<div class="flex flex-row gap-2">
|
2024-06-18 12:35:24 +02:00
|
|
|
<Button
|
|
|
|
variant="outline"
|
|
|
|
disabled={!canUpdate}
|
|
|
|
class="grow"
|
|
|
|
on:click={() => {
|
|
|
|
let effectiveSpeed = getSpeed();
|
|
|
|
if (startDate === undefined || startTime === undefined || effectiveSpeed === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.abs(effectiveSpeed - $gpxStatistics.global.speed.moving) < 0.01) {
|
|
|
|
effectiveSpeed = $gpxStatistics.global.speed.moving;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ratio = 1;
|
|
|
|
if (
|
|
|
|
$gpxStatistics.global.speed.moving > 0 &&
|
|
|
|
$gpxStatistics.global.speed.moving !== effectiveSpeed
|
|
|
|
) {
|
|
|
|
ratio = $gpxStatistics.global.speed.moving / effectiveSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
let item = $selection.getSelected()[0];
|
|
|
|
let fileId = item.getFileId();
|
|
|
|
dbUtils.applyToFile(fileId, (file) => {
|
|
|
|
if (item instanceof ListFileItem) {
|
2024-07-04 02:17:50 +02:00
|
|
|
file.changeTimestamps(getDate(startDate, startTime), effectiveSpeed, ratio);
|
2024-06-18 12:35:24 +02:00
|
|
|
} else if (item instanceof ListTrackItem) {
|
2024-07-04 02:17:50 +02:00
|
|
|
file.changeTimestamps(
|
2024-06-18 12:35:24 +02:00
|
|
|
getDate(startDate, startTime),
|
|
|
|
effectiveSpeed,
|
|
|
|
ratio,
|
|
|
|
item.getTrackIndex()
|
|
|
|
);
|
|
|
|
} else if (item instanceof ListTrackSegmentItem) {
|
2024-07-04 02:17:50 +02:00
|
|
|
file.changeTimestamps(
|
2024-06-18 12:35:24 +02:00
|
|
|
getDate(startDate, startTime),
|
|
|
|
effectiveSpeed,
|
|
|
|
ratio,
|
|
|
|
item.getTrackIndex(),
|
|
|
|
item.getSegmentIndex()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<CalendarClock size="16" class="mr-1" />
|
2024-06-13 17:36:43 +02:00
|
|
|
{$_('toolbar.time.update')}
|
|
|
|
</Button>
|
|
|
|
<Button variant="outline" on:click={setGPXData}>
|
|
|
|
<CircleX size="16" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
2024-08-07 16:54:48 +02:00
|
|
|
<Help link="./help/toolbar/time">
|
2024-06-13 17:36:43 +02:00
|
|
|
{#if canUpdate}
|
|
|
|
{$_('toolbar.time.help')}
|
|
|
|
{:else}
|
|
|
|
{$_('toolbar.time.help_invalid_selection')}
|
|
|
|
{/if}
|
|
|
|
</Help>
|
|
|
|
</div>
|