d85f2e0fb3
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich cleanup and rework PDF switch to HTML export fix Colorsystem Farben für knöpfe hinzugefügt als anpassung button color button color primary and secondary change
130 lines
4.7 KiB
C#
130 lines
4.7 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using cfs_tracker.Data;
|
|
using cfs_tracker.Helpers;
|
|
using cfs_tracker.Services;
|
|
using Microsoft.Maui.Storage;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace cfs_tracker.ViewModels;
|
|
|
|
public partial class MonthViewModel : ObservableObject
|
|
{
|
|
private readonly HealthEntryRepository _healthEntryRepository;
|
|
private readonly AppearanceService _appearanceService;
|
|
private readonly AggregationService _aggregationService;
|
|
private readonly CalendarStateService _calendarStateService;
|
|
private readonly IPdfExportService _pdfExportService;
|
|
|
|
[ObservableProperty]
|
|
private DateTime selectedDate;
|
|
|
|
[ObservableProperty]
|
|
private string headerText = string.Empty;
|
|
|
|
public ObservableCollection<MonthCellViewModel> Cells { get; } = new();
|
|
|
|
public IAsyncRelayCommand PreviousMonthCommand { get; }
|
|
public IAsyncRelayCommand NextMonthCommand { get; }
|
|
public IAsyncRelayCommand TodayCommand { get; }
|
|
public IAsyncRelayCommand ExportPdfCommand { get; }
|
|
public IAsyncRelayCommand<MonthCellViewModel> OpenDayCommand { get; }
|
|
|
|
public MonthViewModel(
|
|
HealthEntryRepository healthEntryRepository,
|
|
AppearanceService appearanceService,
|
|
AggregationService aggregationService,
|
|
CalendarStateService calendarStateService,
|
|
IPdfExportService pdfExportService)
|
|
{
|
|
_healthEntryRepository = healthEntryRepository;
|
|
_appearanceService = appearanceService;
|
|
_aggregationService = aggregationService;
|
|
_calendarStateService = calendarStateService;
|
|
_pdfExportService = pdfExportService;
|
|
|
|
PreviousMonthCommand = new AsyncRelayCommand(PreviousMonthAsync);
|
|
NextMonthCommand = new AsyncRelayCommand(NextMonthAsync);
|
|
TodayCommand = new AsyncRelayCommand(TodayAsync);
|
|
ExportPdfCommand = new AsyncRelayCommand(ExportAsync);
|
|
OpenDayCommand = new AsyncRelayCommand<MonthCellViewModel>(OpenDayAsync);
|
|
|
|
_calendarStateService.SelectedDateChanged += HandleSharedDateChanged;
|
|
SelectedDate = _calendarStateService.SelectedDate;
|
|
}
|
|
|
|
public async Task RefreshAsync()
|
|
{
|
|
SelectedDate = _calendarStateService.SelectedDate;
|
|
var month = new DateTime(SelectedDate.Year, SelectedDate.Month, 1);
|
|
HeaderText = DateHelper.ToGermanMonthYear(month);
|
|
|
|
var monthEnd = month.AddMonths(1);
|
|
|
|
var entries = await _healthEntryRepository.GetEntriesBetweenAsync(month, monthEnd);
|
|
Cells.Clear();
|
|
|
|
var daysInMonth = DateTime.DaysInMonth(month.Year, month.Month);
|
|
|
|
for (var day = 1; day <= daysInMonth; day++)
|
|
{
|
|
var date = new DateTime(month.Year, month.Month, day);
|
|
var dayEntries = entries.Where(x => x.EntryDate.Date == date.Date).ToList();
|
|
var average = _aggregationService.CalculateAverage(dayEntries);
|
|
|
|
Cells.Add(new MonthCellViewModel
|
|
{
|
|
Date = date,
|
|
DayNumberText = date.Day.ToString(),
|
|
IsCurrentMonth = true,
|
|
IsToday = date.Date == DateTime.Today,
|
|
HasEntries = dayEntries.Count > 0,
|
|
AverageStateText = _aggregationService.ToDisplayText(average, _appearanceService),
|
|
StateColor = _appearanceService.GetColor(average.State.ToAppearanceColorKey(), "#1F242B"),
|
|
TextColor = _appearanceService.GetColor(AppearanceKeys.TextPrimary, "#DCE7F3"),
|
|
BorderColor = date.Date == DateTime.Today
|
|
? _appearanceService.GetColor(AppearanceKeys.StateYellow, "#B89A42")
|
|
: _appearanceService.GetColor(AppearanceKeys.Border, "#2A3E55")
|
|
});
|
|
}
|
|
}
|
|
private async Task OpenDayAsync(MonthCellViewModel? item)
|
|
{
|
|
if (item is null)
|
|
return;
|
|
|
|
_calendarStateService.SelectedDate = item.Date;
|
|
await Shell.Current.GoToAsync("//day");
|
|
}
|
|
|
|
private async Task ExportAsync()
|
|
{
|
|
var result = await _pdfExportService.ExportMonthAsync(SelectedDate);
|
|
await ExportUiHelper.ShowExportSavedActionsAsync(result);
|
|
}
|
|
|
|
private async Task PreviousMonthAsync()
|
|
{
|
|
_calendarStateService.SelectedDate = new DateTime(SelectedDate.Year, SelectedDate.Month, 1).AddMonths(-1);
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task NextMonthAsync()
|
|
{
|
|
_calendarStateService.SelectedDate = new DateTime(SelectedDate.Year, SelectedDate.Month, 1).AddMonths(1);
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async Task TodayAsync()
|
|
{
|
|
_calendarStateService.ResetToToday();
|
|
await RefreshAsync();
|
|
}
|
|
|
|
private async void HandleSharedDateChanged(DateTime date)
|
|
{
|
|
SelectedDate = date;
|
|
await RefreshAsync();
|
|
}
|
|
}
|