Files
cfs-Tracker/ViewModels/WeekViewModel.cs
T
2026-03-19 23:11:13 +01:00

140 lines
4.7 KiB
C#

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using cfs_tracker.Data;
using cfs_tracker.Models;
using cfs_tracker.Services;
using cfs_tracker.ViewModels;
using cfs_tracker.Views;
using Microsoft.Maui.Graphics;
using System.Collections.ObjectModel;
using System.Globalization;
namespace cfs_tracker.ViewModels;
public partial class WeekViewModel : ObservableObject
{
private readonly HealthEntryRepository _repository;
private readonly AggregationService _aggregationService;
[ObservableProperty]
private DateTime anchorDate = DateTime.Today;
[ObservableProperty]
private string weekText = "";
public ObservableCollection<WeekCellViewModel> Days { get; } = new();
public IAsyncRelayCommand PreviousWeekCommand { get; }
public IAsyncRelayCommand NextWeekCommand { get; }
public IAsyncRelayCommand TodayCommand { get; }
public IAsyncRelayCommand<WeekCellViewModel> OpenDayCommand { get; }
public WeekViewModel(
HealthEntryRepository repository,
AggregationService aggregationService)
{
_repository = repository;
_aggregationService = aggregationService;
PreviousWeekCommand = new AsyncRelayCommand(PreviousWeekAsync);
NextWeekCommand = new AsyncRelayCommand(NextWeekAsync);
TodayCommand = new AsyncRelayCommand(SetTodayAsync);
OpenDayCommand = new AsyncRelayCommand<WeekCellViewModel>(OpenDayAsync);
}
public async Task RefreshAsync()
{
var weekStart = StartOfWeek(AnchorDate);
var weekEnd = weekStart.AddDays(7);
WeekText = $"{weekStart:dd.MM.yyyy} - {weekEnd.AddDays(-1):dd.MM.yyyy}";
var entries = await _repository.GetEntriesBetweenAsync(weekStart, weekEnd);
var summaries = _aggregationService.BuildWeekSummary(weekStart, entries);
Days.Clear();
foreach (var summary in summaries)
Days.Add(BuildCell(summary));
}
private WeekCellViewModel BuildCell(WeekDaySummary summary)
{
var morningState = _aggregationService.ToDisplayState(summary.MorningAverage);
var afternoonState = _aggregationService.ToDisplayState(summary.AfternoonAverage);
return new WeekCellViewModel
{
Date = summary.Date,
DayText = summary.Date.ToString("ddd", new CultureInfo("de-DE")),
DateText = summary.Date.ToString("dd.MM"),
MorningText = summary.MorningAverage.HasValue ? StateLabel(morningState) : "—",
AfternoonText = summary.AfternoonAverage.HasValue ? StateLabel(afternoonState) : "—",
MorningCountText = summary.MorningCount > 0 ? $"{summary.MorningCount}x" : "",
AfternoonCountText = summary.AfternoonCount > 0 ? $"{summary.AfternoonCount}x" : "",
MorningBackgroundColor = StateColor(morningState),
AfternoonBackgroundColor = StateColor(afternoonState),
MorningTextColor = StateTextColor(morningState),
AfternoonTextColor = StateTextColor(afternoonState),
IsToday = summary.Date.Date == DateTime.Today.Date
};
}
private async Task PreviousWeekAsync()
{
AnchorDate = AnchorDate.AddDays(-7);
await RefreshAsync();
}
private async Task NextWeekAsync()
{
AnchorDate = AnchorDate.AddDays(7);
await RefreshAsync();
}
private async Task SetTodayAsync()
{
AnchorDate = DateTime.Today;
await RefreshAsync();
}
private async Task OpenDayAsync(WeekCellViewModel? cell)
{
if (cell is null)
return;
string date = cell.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
await Shell.Current.GoToAsync($"{nameof(QuickEntryPage)}?date={date}");
}
private static DateTime StartOfWeek(DateTime date)
{
int diff = ((int)date.DayOfWeek + 6) % 7;
return date.Date.AddDays(-diff);
}
private static string StateLabel(HealthState? state) => state switch
{
HealthState.Green => "Gut",
HealthState.Yellow => "So lala",
HealthState.Red => "Schlecht",
HealthState.Gray => "Ausfall",
_ => "—"
};
private static Color StateColor(HealthState? state) => state switch
{
HealthState.Green => Color.FromArgb("#4CAF50"),
HealthState.Yellow => Color.FromArgb("#FBC02D"),
HealthState.Red => Color.FromArgb("#E53935"),
HealthState.Gray => Color.FromArgb("#757575"),
_ => Color.FromArgb("#F2F2F2")
};
private static Color StateTextColor(HealthState? state) => state switch
{
HealthState.Green => Colors.White,
HealthState.Gray => Colors.White,
_ => Colors.Black
};
}