First version
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
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 MonthViewModel : ObservableObject
|
||||
{
|
||||
private readonly HealthEntryRepository _repository;
|
||||
private readonly AggregationService _aggregationService;
|
||||
private readonly CultureInfo _culture = new("de-DE");
|
||||
|
||||
private DateTime _currentMonth = new(DateTime.Today.Year, DateTime.Today.Month, 1);
|
||||
|
||||
[ObservableProperty]
|
||||
private string selectedMonthText = "";
|
||||
|
||||
public ObservableCollection<MonthCellViewModel> Days { get; } = new();
|
||||
|
||||
public IAsyncRelayCommand PreviousMonthCommand { get; }
|
||||
public IAsyncRelayCommand NextMonthCommand { get; }
|
||||
public IAsyncRelayCommand TodayCommand { get; }
|
||||
public IAsyncRelayCommand<MonthCellViewModel> OpenDayCommand { get; }
|
||||
|
||||
public MonthViewModel(
|
||||
HealthEntryRepository repository,
|
||||
AggregationService aggregationService)
|
||||
{
|
||||
_repository = repository;
|
||||
_aggregationService = aggregationService;
|
||||
|
||||
PreviousMonthCommand = new AsyncRelayCommand(PreviousMonthAsync);
|
||||
NextMonthCommand = new AsyncRelayCommand(NextMonthAsync);
|
||||
TodayCommand = new AsyncRelayCommand(SetTodayAsync);
|
||||
OpenDayCommand = new AsyncRelayCommand<MonthCellViewModel>(OpenDayAsync);
|
||||
}
|
||||
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
_currentMonth = new DateTime(_currentMonth.Year, _currentMonth.Month, 1);
|
||||
SelectedMonthText = _currentMonth.ToString("MMMM yyyy", _culture);
|
||||
|
||||
var monthStart = _currentMonth;
|
||||
var monthEnd = monthStart.AddMonths(1);
|
||||
|
||||
var entries = await _repository.GetEntriesBetweenAsync(monthStart, monthEnd);
|
||||
var summaries = _aggregationService.BuildMonthSummary(monthStart, entries);
|
||||
|
||||
Days.Clear();
|
||||
|
||||
int leadingEmptyDays = ((int)monthStart.DayOfWeek + 6) % 7;
|
||||
|
||||
for (int i = 0; i < leadingEmptyDays; i++)
|
||||
Days.Add(MonthCellViewModel.Placeholder());
|
||||
|
||||
foreach (var summary in summaries)
|
||||
Days.Add(BuildCell(summary));
|
||||
|
||||
while (Days.Count % 7 != 0)
|
||||
Days.Add(MonthCellViewModel.Placeholder());
|
||||
}
|
||||
|
||||
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 PreviousMonthAsync()
|
||||
{
|
||||
_currentMonth = _currentMonth.AddMonths(-1);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task NextMonthAsync()
|
||||
{
|
||||
_currentMonth = _currentMonth.AddMonths(1);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task SetTodayAsync()
|
||||
{
|
||||
_currentMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
|
||||
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}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user