Fix media lightbox and sleep target

This commit is contained in:
2026-05-19 16:07:35 +02:00
parent 3e497a8047
commit 6a5852654b
7 changed files with 92 additions and 18 deletions
+40 -14
View File
@@ -366,7 +366,7 @@ final class App
$metrics = $this->healthMetricsFromPayload($payload);
$workouts = $this->healthWorkoutsFromPayload($payload);
$metricImport = $this->healthEventsFromMetrics($metrics);
$metricImport = $this->healthEventsFromMetrics($metrics, (float) ($settings['sleep']['optimal_hours'] ?? 7.0));
$workoutImport = $this->healthEventsFromWorkouts($workouts, $settings);
$entries = $this->entries->all($username);
@@ -717,7 +717,7 @@ final class App
];
}
private function healthEventsFromMetrics(array $metrics): array
private function healthEventsFromMetrics(array $metrics, float $optimalSleepHours = 7.0): array
{
$steps = [];
$sleepBuckets = [];
@@ -786,7 +786,7 @@ final class App
$sleep = [];
foreach ($sleepBuckets as $date => $bucket) {
$signals = $this->healthSleepSignals($bucket);
$signals = $this->healthSleepSignals($bucket, $optimalSleepHours);
$sleep[$date][] = [
'id' => 'health-sleep-' . $date,
@@ -815,17 +815,19 @@ final class App
];
}
private function healthSleepSignals(array $bucket): array
private function healthSleepSignals(array $bucket, float $optimalSleepHours = 7.0): array
{
$hours = (float) ($bucket['hours'] ?? 0);
$deep = (float) ($bucket['deep'] ?? 0);
$rem = (float) ($bucket['rem'] ?? 0);
$core = (float) ($bucket['core'] ?? 0);
$optimalSleepHours = max(1.0, min(16.0, $optimalSleepHours));
$quality = 0;
$deviation = abs($hours - $optimalSleepHours);
if ($hours >= 7 && $hours <= 9) {
if ($deviation <= 0.75) {
$quality++;
} elseif ($hours < 5 || $hours > 10) {
} elseif ($deviation >= 2.0) {
$quality--;
}
@@ -843,8 +845,8 @@ final class App
return [
'mood' => max(-2, min(2, $quality - 1)),
'energy' => max(-2, min(2, (int) round(($deep + $rem) - 2))),
'stress' => max(-2, min(2, $core >= 3.5 && $hours >= 6 ? -1 : ($hours < 5 ? 1 : 0))),
'energy' => max(-2, min(2, (int) round(($deep + $rem) - 2 - max(0, $deviation - 1)))),
'stress' => max(-2, min(2, $core >= 3.5 && $deviation <= 1.0 ? -1 : ($deviation >= 2.0 ? 1 : 0))),
];
}
@@ -1202,7 +1204,7 @@ final class App
'dayEntry' => $selectedEntry,
'dashboardEventTypes' => day_event_type_options(),
'dashboardSignals' => signal_scale_options(),
'dashboardTimeline' => $this->buildDashboardTimeline($selectedEntry),
'dashboardTimeline' => $this->buildDashboardTimeline($selectedEntry, $settings),
'dashboardCompareDays' => $this->buildDashboardCompareDays($dashboardDate, $entryMap, $settings),
'dashboardWeek' => $this->buildDashboardWeekView($dashboardDate, $entryMap),
'dashboardMonth' => $this->buildDashboardMonthView($dashboardDate, $entryMap),
@@ -1392,7 +1394,7 @@ final class App
]);
}
private function buildDashboardTimeline(array $entry): array
private function buildDashboardTimeline(array $entry, array $settings): array
{
$timeline = [];
@@ -1401,10 +1403,26 @@ final class App
continue;
}
$type = (string) ($event['type'] ?? 'event');
$mood = normalize_signal_value($event['mood'] ?? 0);
$energy = normalize_signal_value($event['energy'] ?? 0);
$stress = normalize_signal_value($event['stress'] ?? 0);
if ($type === 'sleep' && (string) ($event['source'] ?? '') === 'health_auto_export') {
$signals = $this->healthSleepSignals([
'hours' => (float) ($event['value'] ?? 0),
'deep' => (float) ($event['sleep_deep'] ?? 0),
'rem' => (float) ($event['sleep_rem'] ?? 0),
'core' => (float) ($event['sleep_core'] ?? 0),
], (float) ($settings['sleep']['optimal_hours'] ?? 7.0));
$mood = $signals['mood'];
$energy = $signals['energy'];
$stress = $signals['stress'];
}
$timeline[] = [
'kind' => 'event',
'id' => (string) ($event['id'] ?? ''),
'type' => (string) ($event['type'] ?? 'event'),
'type' => $type,
'time' => (string) ($event['time'] ?? ''),
'comment' => (string) ($event['comment'] ?? ''),
'value' => (float) ($event['value'] ?? 0),
@@ -1413,9 +1431,9 @@ final class App
'consumed' => !empty($event['consumed']),
'image' => (string) ($event['image'] ?? ''),
'image_url' => is_string($event['image_url'] ?? null) ? (string) $event['image_url'] : null,
'mood' => normalize_signal_value($event['mood'] ?? 0),
'energy' => normalize_signal_value($event['energy'] ?? 0),
'stress' => normalize_signal_value($event['stress'] ?? 0),
'mood' => $mood,
'energy' => $energy,
'stress' => $stress,
'source' => (string) ($event['source'] ?? ''),
'import_id' => (string) ($event['import_id'] ?? ''),
'duration_label' => (string) ($event['duration_label'] ?? ''),
@@ -2894,6 +2912,9 @@ final class App
$settings['walk'] = [
'mode' => ($input['walk']['mode'] ?? ($settings['walk']['mode'] ?? 'time')) === 'steps' ? 'steps' : 'time',
];
$settings['sleep'] = [
'optimal_hours' => max(1.0, min(16.0, round((float) ($input['sleep']['optimal_hours'] ?? ($settings['sleep']['optimal_hours'] ?? 7.0)), 1))),
];
$settings['scoring']['mood_multiplier'] = max(0, min(10, (int) ($input['scoring']['mood_multiplier'] ?? 3)));
$settings['scoring']['energy_multiplier'] = max(0, min(10, (int) ($input['scoring']['energy_multiplier'] ?? 2)));
@@ -2976,6 +2997,11 @@ final class App
Defaults::settings()['walk'],
is_array($settings['walk'] ?? null) ? $settings['walk'] : []
);
$settings['sleep'] = array_replace(
Defaults::settings()['sleep'],
is_array($settings['sleep'] ?? null) ? $settings['sleep'] : []
);
$settings['sleep']['optimal_hours'] = max(1.0, min(16.0, round((float) ($settings['sleep']['optimal_hours'] ?? 7.0), 1)));
$settings['tracking'] = array_replace(
Defaults::settings()['tracking'],
is_array($settings['tracking'] ?? null) ? $settings['tracking'] : []