First version
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public enum CalendarMode
|
||||
{
|
||||
Day,
|
||||
Week,
|
||||
Month,
|
||||
Year
|
||||
}
|
||||
|
||||
public partial class CalendarViewModel : ObservableObject
|
||||
{
|
||||
[ObservableProperty]
|
||||
private CalendarMode currentMode = CalendarMode.Month;
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime selectedDate = DateTime.Today;
|
||||
|
||||
public IRelayCommand ShowDayCommand { get; }
|
||||
public IRelayCommand ShowWeekCommand { get; }
|
||||
public IRelayCommand ShowMonthCommand { get; }
|
||||
public IRelayCommand ShowYearCommand { get; }
|
||||
public IRelayCommand TodayCommand { get; }
|
||||
|
||||
public CalendarViewModel()
|
||||
{
|
||||
ShowDayCommand = new RelayCommand(() => CurrentMode = CalendarMode.Day);
|
||||
ShowWeekCommand = new RelayCommand(() => CurrentMode = CalendarMode.Week);
|
||||
ShowMonthCommand = new RelayCommand(() => CurrentMode = CalendarMode.Month);
|
||||
ShowYearCommand = new RelayCommand(() => CurrentMode = CalendarMode.Year);
|
||||
TodayCommand = new RelayCommand(() => SelectedDate = DateTime.Today);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using cfs_tracker.Models;
|
||||
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public class DayEntryItemViewModel
|
||||
{
|
||||
public int Id { get; init; }
|
||||
public DateTime Timestamp { get; init; }
|
||||
|
||||
public string TimeText { get; init; } = "";
|
||||
public string StateText { get; init; } = "";
|
||||
public string SourceText { get; init; } = "";
|
||||
|
||||
public Color BackgroundColor { get; init; } = Colors.Transparent;
|
||||
public Color TextColor { get; init; } = Colors.Black;
|
||||
|
||||
public static DayEntryItemViewModel FromEntry(HealthEntry entry)
|
||||
{
|
||||
var state = entry.State;
|
||||
var background = state switch
|
||||
{
|
||||
HealthState.Green => Color.FromArgb("#4CAF50"),
|
||||
HealthState.Yellow => Color.FromArgb("#FBC02D"),
|
||||
HealthState.Red => Color.FromArgb("#E53935"),
|
||||
HealthState.Gray => Color.FromArgb("#757575"),
|
||||
_ => Colors.Transparent
|
||||
};
|
||||
|
||||
var textColor = state switch
|
||||
{
|
||||
HealthState.Gray => Colors.White,
|
||||
HealthState.Green => Colors.White,
|
||||
_ => Colors.Black
|
||||
};
|
||||
|
||||
return new DayEntryItemViewModel
|
||||
{
|
||||
Id = entry.Id,
|
||||
Timestamp = entry.Timestamp,
|
||||
TimeText = entry.Timestamp.ToString("HH:mm"),
|
||||
StateText = state switch
|
||||
{
|
||||
HealthState.Green => "Gut",
|
||||
HealthState.Yellow => "So lala",
|
||||
HealthState.Red => "Schlecht",
|
||||
HealthState.Gray => "Totalausfall",
|
||||
_ => ""
|
||||
},
|
||||
SourceText = entry.Source switch
|
||||
{
|
||||
EntrySource.App => "App",
|
||||
EntrySource.Notification => "Push",
|
||||
_ => ""
|
||||
},
|
||||
BackgroundColor = background,
|
||||
TextColor = textColor
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
using cfs_tracker.Data;
|
||||
using cfs_tracker.Models;
|
||||
using cfs_tracker.Views;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public partial class DayViewModel : ObservableObject
|
||||
{
|
||||
private readonly HealthEntryRepository _repository;
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime selectedDate = DateTime.Today;
|
||||
|
||||
[ObservableProperty]
|
||||
private string selectedDateText = "";
|
||||
|
||||
[ObservableProperty]
|
||||
private int greenCount;
|
||||
|
||||
[ObservableProperty]
|
||||
private int yellowCount;
|
||||
|
||||
[ObservableProperty]
|
||||
private int redCount;
|
||||
|
||||
[ObservableProperty]
|
||||
private int grayCount;
|
||||
|
||||
public ObservableCollection<DayEntryItemViewModel> Entries { get; } = new();
|
||||
|
||||
public IAsyncRelayCommand PreviousDayCommand { get; }
|
||||
public IAsyncRelayCommand NextDayCommand { get; }
|
||||
public IAsyncRelayCommand TodayCommand { get; }
|
||||
public IAsyncRelayCommand AddEntryCommand { get; }
|
||||
public IAsyncRelayCommand<DayEntryItemViewModel> DeleteEntryCommand { get; }
|
||||
|
||||
public DayViewModel(HealthEntryRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
|
||||
PreviousDayCommand = new AsyncRelayCommand(PreviousDayAsync);
|
||||
NextDayCommand = new AsyncRelayCommand(NextDayAsync);
|
||||
TodayCommand = new AsyncRelayCommand(SetTodayAsync);
|
||||
AddEntryCommand = new AsyncRelayCommand(OpenQuickEntryAsync);
|
||||
DeleteEntryCommand = new AsyncRelayCommand<DayEntryItemViewModel>(DeleteEntryAsync);
|
||||
|
||||
UpdateDateText();
|
||||
}
|
||||
|
||||
partial void OnSelectedDateChanged(DateTime value)
|
||||
{
|
||||
UpdateDateText();
|
||||
}
|
||||
|
||||
public async Task RefreshAsync()
|
||||
{
|
||||
var entries = await _repository.GetEntriesForDateAsync(SelectedDate);
|
||||
|
||||
GreenCount = entries.Count(x => x.State == HealthState.Green);
|
||||
YellowCount = entries.Count(x => x.State == HealthState.Yellow);
|
||||
RedCount = entries.Count(x => x.State == HealthState.Red);
|
||||
GrayCount = entries.Count(x => x.State == HealthState.Gray);
|
||||
|
||||
Entries.Clear();
|
||||
|
||||
foreach (var entry in entries.OrderBy(x => x.Timestamp))
|
||||
Entries.Add(DayEntryItemViewModel.FromEntry(entry));
|
||||
}
|
||||
|
||||
private async Task PreviousDayAsync()
|
||||
{
|
||||
SelectedDate = SelectedDate.AddDays(-1);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task NextDayAsync()
|
||||
{
|
||||
SelectedDate = SelectedDate.AddDays(1);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task SetTodayAsync()
|
||||
{
|
||||
SelectedDate = DateTime.Today;
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private async Task OpenQuickEntryAsync()
|
||||
{
|
||||
string date = SelectedDate.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
|
||||
await Shell.Current.GoToAsync($"{nameof(QuickEntryPage)}?date={date}");
|
||||
}
|
||||
|
||||
private async Task DeleteEntryAsync(DayEntryItemViewModel? item)
|
||||
{
|
||||
if (item is null)
|
||||
return;
|
||||
|
||||
await _repository.DeleteEntryAsync(item.Id);
|
||||
await RefreshAsync();
|
||||
}
|
||||
|
||||
private void UpdateDateText()
|
||||
{
|
||||
SelectedDateText = SelectedDate.ToString("dddd, dd. MMMM yyyy", new CultureInfo("de-DE"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.Maui.Graphics;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public class MonthCellViewModel
|
||||
{
|
||||
public bool IsRealDay { get; init; }
|
||||
public DateTime Date { get; init; }
|
||||
|
||||
public string DayNumberText { get; init; } = "";
|
||||
public string CountText { get; init; } = "";
|
||||
|
||||
public Color BackgroundColor { get; init; } = Colors.Transparent;
|
||||
public Color BorderColor { get; init; } = Colors.Transparent;
|
||||
public Color TextColor { get; init; } = Colors.Black;
|
||||
|
||||
public static MonthCellViewModel Placeholder() => new()
|
||||
{
|
||||
IsRealDay = false,
|
||||
BackgroundColor = Colors.Transparent,
|
||||
BorderColor = Colors.Transparent,
|
||||
TextColor = Colors.Transparent,
|
||||
DayNumberText = "",
|
||||
CountText = ""
|
||||
};
|
||||
}
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using cfs_tracker.Data;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public partial class OptionsViewModel : ObservableObject
|
||||
{
|
||||
private readonly HealthEntryRepository _repository;
|
||||
|
||||
[ObservableProperty]
|
||||
private string deleteConfirmationText = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string statusText = string.Empty;
|
||||
|
||||
public IAsyncRelayCommand DeleteAllDataCommand { get; }
|
||||
|
||||
public OptionsViewModel(HealthEntryRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
DeleteAllDataCommand = new AsyncRelayCommand(DeleteAllDataAsync, CanDeleteAllData);
|
||||
}
|
||||
|
||||
partial void OnDeleteConfirmationTextChanged(string value)
|
||||
{
|
||||
DeleteAllDataCommand.NotifyCanExecuteChanged();
|
||||
}
|
||||
|
||||
private bool CanDeleteAllData()
|
||||
{
|
||||
return string.Equals(
|
||||
DeleteConfirmationText?.Trim(),
|
||||
"löschen",
|
||||
StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private async Task DeleteAllDataAsync()
|
||||
{
|
||||
await _repository.DeleteAllAsync();
|
||||
|
||||
DeleteConfirmationText = string.Empty;
|
||||
StatusText = "Alle gespeicherten Einträge wurden gelöscht.";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using cfs_tracker.Data;
|
||||
using cfs_tracker.Models;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public partial class QuickEntryViewModel : ObservableObject
|
||||
{
|
||||
private readonly HealthEntryRepository _repository;
|
||||
|
||||
[ObservableProperty]
|
||||
private DateTime selectedDate = DateTime.Today;
|
||||
|
||||
[ObservableProperty]
|
||||
private string selectedDateText = "";
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isPastDate;
|
||||
|
||||
[ObservableProperty]
|
||||
private bool isFutureDate;
|
||||
|
||||
[ObservableProperty]
|
||||
private string selectedPartText = "Vormittag";
|
||||
|
||||
public ObservableCollection<string> AvailableParts { get; } = new()
|
||||
{
|
||||
"Vormittag",
|
||||
"Nachmittag"
|
||||
};
|
||||
|
||||
public IAsyncRelayCommand SaveGreenCommand { get; }
|
||||
public IAsyncRelayCommand SaveYellowCommand { get; }
|
||||
public IAsyncRelayCommand SaveRedCommand { get; }
|
||||
public IAsyncRelayCommand SaveGrayCommand { get; }
|
||||
|
||||
public QuickEntryViewModel(HealthEntryRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
|
||||
SaveGreenCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Green));
|
||||
SaveYellowCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Yellow));
|
||||
SaveRedCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Red));
|
||||
SaveGrayCommand = new AsyncRelayCommand(() => SaveAsync(HealthState.Gray));
|
||||
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
public void SetDateFromQuery(string? dateText)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(dateText) &&
|
||||
DateTime.TryParseExact(
|
||||
dateText,
|
||||
"yyyy-MM-dd",
|
||||
CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.None,
|
||||
out var parsed))
|
||||
{
|
||||
SelectedDate = parsed.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
SelectedDate = DateTime.Today;
|
||||
}
|
||||
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
partial void OnSelectedDateChanged(DateTime value)
|
||||
{
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
private async Task SaveAsync(HealthState state)
|
||||
{
|
||||
if (IsFutureDate)
|
||||
return;
|
||||
|
||||
if (IsPastDate)
|
||||
{
|
||||
await _repository.UpsertBackfillAsync(
|
||||
SelectedDate,
|
||||
GetSelectedPart(),
|
||||
state,
|
||||
EntrySource.App);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _repository.AddEntryAsync(
|
||||
state,
|
||||
EntrySource.App,
|
||||
DateTime.Now);
|
||||
}
|
||||
|
||||
await Shell.Current.GoToAsync("..");
|
||||
}
|
||||
|
||||
private DayPart GetSelectedPart()
|
||||
{
|
||||
return SelectedPartText == "Nachmittag"
|
||||
? DayPart.Afternoon
|
||||
: DayPart.Morning;
|
||||
}
|
||||
|
||||
private void UpdateState()
|
||||
{
|
||||
var today = DateTime.Today;
|
||||
|
||||
IsPastDate = SelectedDate.Date < today;
|
||||
IsFutureDate = SelectedDate.Date > today;
|
||||
|
||||
SelectedDateText = SelectedDate.ToString("dddd, dd. MMMM yyyy", new CultureInfo("de-DE"));
|
||||
|
||||
if (!IsPastDate)
|
||||
SelectedPartText = "Vormittag";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.Maui.Graphics;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public class WeekCellViewModel
|
||||
{
|
||||
public DateTime Date { get; init; }
|
||||
|
||||
public string DayText { get; init; } = "";
|
||||
public string DateText { get; init; } = "";
|
||||
|
||||
public string MorningText { get; init; } = "";
|
||||
public string AfternoonText { get; init; } = "";
|
||||
|
||||
public string MorningCountText { get; init; } = "";
|
||||
public string AfternoonCountText { get; init; } = "";
|
||||
|
||||
public Color MorningBackgroundColor { get; init; } = Colors.Transparent;
|
||||
public Color AfternoonBackgroundColor { get; init; } = Colors.Transparent;
|
||||
|
||||
public Color MorningTextColor { get; init; } = Colors.Black;
|
||||
public Color AfternoonTextColor { get; init; } = Colors.Black;
|
||||
|
||||
public bool IsToday { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
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
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace cfs_tracker.ViewModels;
|
||||
|
||||
public class YearMonthViewModel
|
||||
{
|
||||
public int MonthNumber { get; init; }
|
||||
public string MonthText { get; init; } = "";
|
||||
|
||||
public ObservableCollection<MonthCellViewModel> Days { get; } = new();
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user