111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
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"));
|
|
}
|
|
} |