Farbpalette vorladen lassen
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
This commit is contained in:
@@ -1,114 +1,129 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using cfs_tracker.Data;
|
||||
using cfs_tracker.Helpers;
|
||||
using cfs_tracker.Models;
|
||||
using cfs_tracker.Services;
|
||||
using Microsoft.Maui.Storage;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public partial class MonthViewModel : ObservableObject
|
||||
{
|
||||
private readonly HealthEntryRepository _repository;
|
||||
private readonly HealthEntryRepository _healthEntryRepository;
|
||||
private readonly AppearanceService _appearanceService;
|
||||
private readonly AggregationService _aggregationService;
|
||||
private readonly CalendarStateService _calendarState;
|
||||
private readonly CultureInfo _culture = new("de-DE");
|
||||
|
||||
private DateTime _currentMonth = new(DateTime.Today.Year, DateTime.Today.Month, 1);
|
||||
private readonly CalendarStateService _calendarStateService;
|
||||
private readonly IPdfExportService _pdfExportService;
|
||||
|
||||
[ObservableProperty]
|
||||
private string selectedMonthText = string.Empty;
|
||||
private DateTime selectedDate;
|
||||
|
||||
public ObservableCollection<MonthCellViewModel> Days { get; } = new();
|
||||
[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 repository,
|
||||
HealthEntryRepository healthEntryRepository,
|
||||
AppearanceService appearanceService,
|
||||
AggregationService aggregationService,
|
||||
CalendarStateService calendarState)
|
||||
CalendarStateService calendarStateService,
|
||||
IPdfExportService pdfExportService)
|
||||
{
|
||||
_repository = repository;
|
||||
_healthEntryRepository = healthEntryRepository;
|
||||
_appearanceService = appearanceService;
|
||||
_aggregationService = aggregationService;
|
||||
_calendarState = calendarState;
|
||||
_calendarStateService = calendarStateService;
|
||||
_pdfExportService = pdfExportService;
|
||||
|
||||
PreviousMonthCommand = new AsyncRelayCommand(PreviousMonthAsync);
|
||||
NextMonthCommand = new AsyncRelayCommand(NextMonthAsync);
|
||||
TodayCommand = new AsyncRelayCommand(SetTodayAsync);
|
||||
TodayCommand = new AsyncRelayCommand(TodayAsync);
|
||||
ExportPdfCommand = new AsyncRelayCommand(ExportAsync);
|
||||
OpenDayCommand = new AsyncRelayCommand<MonthCellViewModel>(OpenDayAsync);
|
||||
|
||||
_calendarStateService.SelectedDateChanged += HandleSharedDateChanged;
|
||||
SelectedDate = _calendarStateService.SelectedDate;
|
||||
}
|
||||
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
_currentMonth = new DateTime(_currentMonth.Year, _currentMonth.Month, 1);
|
||||
SelectedMonthText = _currentMonth.ToString("MMMM yyyy", _culture);
|
||||
SelectedDate = _calendarStateService.SelectedDate;
|
||||
var month = new DateTime(SelectedDate.Year, SelectedDate.Month, 1);
|
||||
HeaderText = DateHelper.ToGermanMonthYear(month);
|
||||
|
||||
var monthStart = _currentMonth;
|
||||
var monthEnd = monthStart.AddMonths(1);
|
||||
var monthEnd = month.AddMonths(1);
|
||||
|
||||
var entries = await _repository.GetEntriesBetweenAsync(monthStart, monthEnd);
|
||||
var summaries = _aggregationService.BuildMonthSummary(monthStart, entries);
|
||||
var entries = await _healthEntryRepository.GetEntriesBetweenAsync(month, monthEnd);
|
||||
Cells.Clear();
|
||||
|
||||
Days.Clear();
|
||||
var daysInMonth = DateTime.DaysInMonth(month.Year, month.Month);
|
||||
|
||||
int leadingEmptyDays = ((int)monthStart.DayOfWeek + 6) % 7;
|
||||
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);
|
||||
|
||||
for (int i = 0; i < leadingEmptyDays; i++)
|
||||
Days.Add(MonthCellViewModel.Placeholder());
|
||||
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;
|
||||
|
||||
foreach (var summary in summaries)
|
||||
Days.Add(BuildCell(summary));
|
||||
|
||||
while (Days.Count % 7 != 0)
|
||||
Days.Add(MonthCellViewModel.Placeholder());
|
||||
_calendarStateService.SelectedDate = item.Date;
|
||||
await Shell.Current.GoToAsync("//day");
|
||||
}
|
||||
|
||||
private MonthCellViewModel BuildCell(CalendarDaySummary summary)
|
||||
private async Task ExportAsync()
|
||||
{
|
||||
var colorKey = summary.DisplayState?.ToColorKey() ?? ThemeKeys.PanelBackground2;
|
||||
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" : string.Empty,
|
||||
BackgroundColor = ThemeResourceHelper.GetColor(colorKey, "#203041"),
|
||||
BorderColor = isToday
|
||||
? ThemeResourceHelper.GetColor(ThemeKeys.TextPrimary, "#D9E6F2")
|
||||
: ThemeResourceHelper.GetColor(ThemeKeys.PanelBorder, "#2B3E52"),
|
||||
TextColor = ThemeResourceHelper.GetColor(ThemeKeys.TextPrimary, "#D9E6F2")
|
||||
};
|
||||
var result = await _pdfExportService.ExportMonthAsync(SelectedDate);
|
||||
await ExportUiHelper.ShowExportSavedActionsAsync(result);
|
||||
}
|
||||
|
||||
private async Task PreviousMonthAsync()
|
||||
{
|
||||
_currentMonth = _currentMonth.AddMonths(-1);
|
||||
_calendarStateService.SelectedDate = new DateTime(SelectedDate.Year, SelectedDate.Month, 1).AddMonths(-1);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task NextMonthAsync()
|
||||
{
|
||||
_currentMonth = _currentMonth.AddMonths(1);
|
||||
_calendarStateService.SelectedDate = new DateTime(SelectedDate.Year, SelectedDate.Month, 1).AddMonths(1);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task SetTodayAsync()
|
||||
private async Task TodayAsync()
|
||||
{
|
||||
await _calendarState.ShowDayTabAsync(DateTime.Today);
|
||||
_calendarStateService.ResetToToday();
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task OpenDayAsync(MonthCellViewModel? cell)
|
||||
private async void HandleSharedDateChanged(DateTime date)
|
||||
{
|
||||
if (cell is null || !cell.IsRealDay)
|
||||
return;
|
||||
|
||||
await _calendarState.ShowDayTabAsync(cell.Date);
|
||||
SelectedDate = date;
|
||||
await RefreshAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user