130 lines
4.2 KiB
C#
130 lines
4.2 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.Views;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
|
|
namespace cfs_tracker.ViewModels;
|
|
|
|
public partial class YearViewModel : ObservableObject
|
|
{
|
|
private readonly HealthEntryRepository _repository;
|
|
private readonly AggregationService _aggregationService;
|
|
private readonly CultureInfo _culture = new("de-DE");
|
|
|
|
private int _currentYear = DateTime.Today.Year;
|
|
|
|
[ObservableProperty]
|
|
private string selectedYearText = "";
|
|
|
|
public ObservableCollection<YearMonthViewModel> Months { get; } = new();
|
|
|
|
public IAsyncRelayCommand PreviousYearCommand { get; }
|
|
public IAsyncRelayCommand NextYearCommand { get; }
|
|
public IAsyncRelayCommand TodayCommand { get; }
|
|
public IAsyncRelayCommand<MonthCellViewModel> OpenDayCommand { get; }
|
|
|
|
public YearViewModel(
|
|
HealthEntryRepository repository,
|
|
AggregationService aggregationService)
|
|
{
|
|
_repository = repository;
|
|
_aggregationService = aggregationService;
|
|
|
|
PreviousYearCommand = new AsyncRelayCommand(PreviousYearAsync);
|
|
NextYearCommand = new AsyncRelayCommand(NextYearAsync);
|
|
TodayCommand = new AsyncRelayCommand(SetTodayAsync);
|
|
OpenDayCommand = new AsyncRelayCommand<MonthCellViewModel>(OpenDayAsync);
|
|
}
|
|
|
|
public async Task RefreshAsync()
|
|
{
|
|
SelectedYearText = _currentYear.ToString();
|
|
|
|
var start = new DateTime(_currentYear, 1, 1);
|
|
var end = start.AddYears(1);
|
|
|
|
var entries = await _repository.GetEntriesBetweenAsync(start, end);
|
|
var summaries = _aggregationService.BuildYearSummary(_currentYear, entries);
|
|
|
|
Months.Clear();
|
|
|
|
for (int month = 1; month <= 12; month++)
|
|
{
|
|
var monthVm = new YearMonthViewModel
|
|
{
|
|
MonthNumber = month,
|
|
MonthText = new DateTime(_currentYear, month, 1).ToString("MMMM", _culture)
|
|
};
|
|
|
|
var monthStart = new DateTime(_currentYear, month, 1);
|
|
int leadingEmptyDays = ((int)monthStart.DayOfWeek + 6) % 7;
|
|
|
|
for (int i = 0; i < leadingEmptyDays; i++)
|
|
monthVm.Days.Add(MonthCellViewModel.Placeholder());
|
|
|
|
foreach (var summary in summaries.Where(x => x.Date.Month == month))
|
|
monthVm.Days.Add(BuildCell(summary));
|
|
|
|
while (monthVm.Days.Count % 7 != 0)
|
|
monthVm.Days.Add(MonthCellViewModel.Placeholder());
|
|
|
|
Months.Add(monthVm);
|
|
}
|
|
}
|
|
|
|
private MonthCellViewModel BuildCell(CalendarDaySummary summary)
|
|
{
|
|
Color background = summary.DisplayState switch
|
|
{
|
|
HealthState.Green => Color.FromArgb("#4CAF50"),
|
|
HealthState.Yellow => Color.FromArgb("#FBC02D"),
|
|
HealthState.Red => Color.FromArgb("#E53935"),
|
|
HealthState.Gray => Color.FromArgb("#757575"),
|
|
_ => Color.FromArgb("#F2F2F2")
|
|
};
|
|
|
|
bool isToday = summary.Date.Date == DateTime.Today.Date;
|
|
|
|
return new MonthCellViewModel
|
|
{
|
|
IsRealDay = true,
|
|
Date = summary.Date,
|
|
DayNumberText = summary.Date.Day.ToString(),
|
|
CountText = summary.EntryCount > 0 ? $"{summary.EntryCount}x" : "",
|
|
BackgroundColor = background,
|
|
BorderColor = isToday ? Colors.Black : Color.FromArgb("#D0D0D0"),
|
|
TextColor = summary.DisplayState == HealthState.Gray ? Colors.White : Colors.Black
|
|
};
|
|
}
|
|
|
|
private async Task PreviousYearAsync()
|
|
{
|
|
_currentYear--;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task NextYearAsync()
|
|
{
|
|
_currentYear++;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task SetTodayAsync()
|
|
{
|
|
_currentYear = DateTime.Today.Year;
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task OpenDayAsync(MonthCellViewModel? cell)
|
|
{
|
|
if (cell is null || !cell.IsRealDay)
|
|
return;
|
|
|
|
string date = cell.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
|
await Shell.Current.GoToAsync($"{nameof(QuickEntryPage)}?date={date}");
|
|
}
|
|
} |